pboling/celluloid-io-pg-listener

View on GitHub
bin/supervisor

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# Run the supervisor from a command prompt like this:
#
#    bin/supervisor --config bin/settings.multiple.yml &
#
require "erb"
require "uri"
require "yaml"
require "optparse"
require "bundler/setup"
require "celluloid/current"
require "celluloid/supervision"
require "celluloid-io-pg-listener"
require "celluloid-io-pg-listener/supervision_configuration_signature"

# These are referenced in bin/settings.yml, so they have to be required:
require "celluloid-io-pg-listener/examples/client"
require "celluloid-io-pg-listener/examples/listener_client_by_inheritance"

$supervisor_options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: bin/supervisor [options]"

  opts.on("-c", "--config FILE", "Path to YAML configuration file") do |file|
    $supervisor_options[:config_path] = file
  end
  opts.on("-d", "--debug", "set $CELLULOID_DEBUG = true") do |file|
    $supervisor_options[:debug] = true
    $CELLULOID_DEBUG = true
  end
  opts.on("-t", "--threaded", "set Celluloid.task_class = Celluloid::Task::Threaded") do |file|
    $supervisor_options[:threaded] = true
    Celluloid.task_class = Celluloid::Task::Threaded
  end
end.parse!
puts "$supervisor_options: #{$supervisor_options.inspect}" if $supervisor_options[:debug]

module CelluloidIOPGListener
  class InsertClient
    include CelluloidIOPGListener.client(:unlisten_wrapper_without_termination)
    def insert_callback(channel, payload)
      # <-- within the unlisten_wrapper's block if :insert_callback is the callback_method
      debug "#{self.class} channel is #{channel}"
      debug "#{self.class} payload is #{payload}"
    end
  end
end

class ListeningGroup < Celluloid::Supervision::Container
  signatures = CelluloidIOPGListener::SupervisionConfigurationSignature.new(path_to_yaml: $supervisor_options[:config_path]).signature
  supervise **signatures[0] if signatures[0]
  supervise **signatures[1] if signatures[1]
  supervise **signatures[2] if signatures[2]
end

ListeningGroup.run

# Not sure why this is not working
# config_signature = CelluloidIOPGListener::SupervisionConfigurationSignature.new(path_to_yaml: $supervisor_options[:config_path]).signature
# supervision_config = Celluloid::Supervision::Configuration.define(config_signature)
# supervision_config.deploy