Class: WorklingDaemon
- Inherits:
-
Object
- Object
- WorklingDaemon
- Defined in:
- lib/workling_daemon.rb
Class Method Summary collapse
- .boot_with(options) ⇒ Object
- .extract_options(options) ⇒ Object
- .initialize_workling(options) ⇒ Object
- .parse_daemon_options(argv) ⇒ Object
- .parse_workling_options(args) ⇒ Object
- .partition_daemons_options(args) ⇒ Object
- .partition_options(args) ⇒ Object
- .run(options) ⇒ Object
- .run_as_daemon! ⇒ Object
- .running_as_daemon? ⇒ Boolean
Class Method Details
.boot_with(options) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/workling_daemon.rb', line 86 def self.boot_with() if [:no_rails] # if rails is not booted we need to pull in the workling requires manually require File.join(File.dirname(__FILE__), "workling") else ENV["RAILS_ENV"] = [:rails_env] puts "=> Loading Rails with #{ENV["RAILS_ENV"]} environment..." require [:rails_root] + '/config/environment' ActiveRecord::Base.logger = Workling::Base.logger ActionController::Base.logger = Workling::Base.logger puts '** Rails loaded.' end end |
.extract_options(options) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/workling_daemon.rb', line 64 def self.() result = {} result[:client] = [:client] if [:client] result[:routing] = [:routing] if [:routing] result[:invoker] = [:invoker] if [:invoker] result end |
.initialize_workling(options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/workling_daemon.rb', line 72 def self.initialize_workling() Workling.load_path = [:load_path] if [:load_path] Workling::Discovery.discover! if [:config_path] Workling.config_path = [:config_path] Workling.config else Workling.config = end Workling.select_and_build_invoker end |
.parse_daemon_options(argv) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/workling_daemon.rb', line 31 def self.(argv) = {} pass_through, args = argv opts = OptionParser.new do |opts| opts. = 'Usage: myapp [options]' opts.separator '' opts.on('-a', '--app-name APP_NAME', String,"specify the process name") { |v| [:app_name] = v } opts.on('-d', '--dir DIR', String, "the directory to run in") { |v| [:dir] = v } opts.on('-m', '--monitor',"specify the process name") { |v| [:monitor] = true } opts.on('-t', '--ontop') { |k, v| pass_through << v } end opts.parse!((args).first) .merge(:ARGV => pass_through) end |
.parse_workling_options(args) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/workling_daemon.rb', line 46 def self.(args) = {} opts = OptionParser.new do |opts| opts. = 'Usage: myapp [options]' opts.separator '' opts.on('-n', '--no_rails', "do not load Rails") { |v| [:no_rails] = true } opts.on('-c', '--client CLIENT', String, "specify the client class") { |v| [:client] = v } opts.on('-i', '--invoker INVOKER', String, "specify the invoker class") { |v| [:invoker] = v } opts.on('-r', '--routing ROUTING', String, "specify the routing class") { |v| [:routing] = v } opts.on('-l', '--load-path LOADPATH', String, "specify the load_path for the workers") { |v| [:load_path] = v } opts.on('-f', '--config-path CONFIGPATH', String, "specify the path to the workling.yml file") { |v| [:config_path] = v } opts.on('-e', '--environment ENVIRONMENT', String, "specify the environment") { |v| [:rails_env] = v } opts.on('-p', '--prefix PREFIX', String, "specify the prefix for queues") { |v| [:prefix] = v } end opts.parse!((args).last) end |
.partition_daemons_options(args) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/workling_daemon.rb', line 23 def self.(args) = %W{start stop restart run zap -t --ontop -f --force -h --help --version} pass_through = args.select { |a| .include? a } = args.reject { |a| .include? a } [pass_through, ] end |
.partition_options(args) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/workling_daemon.rb', line 14 def self.(args) daemon = [] workling = [] split_point = args.index("--") || args.size daemon = args[0...split_point] if split_point > 0 workling = args[(split_point+1)..-1] if split_point and split_point < args.size [daemon, workling] end |
.run(options) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/workling_daemon.rb', line 103 def self.run() WorklingDaemon.run_as_daemon! boot_with poller = initialize_workling() puts "** Starting #{ poller.class }..." puts '** Use CTRL-C to stop.' trap(:INT) { poller.stop; exit } begin poller.listen ensure puts '** No Worklings found.' if Workling::Discovery.discovered_workers.empty? puts '** Exiting' end end |
.run_as_daemon! ⇒ Object
9 10 11 |
# File 'lib/workling_daemon.rb', line 9 def self.run_as_daemon! @running_as_daemon = true end |
.running_as_daemon? ⇒ Boolean
5 6 7 |
# File 'lib/workling_daemon.rb', line 5 def self.running_as_daemon? !!@running_as_daemon end |