Class: WorklingDaemon

Inherits:
Object
  • Object
show all
Defined in:
lib/workling_daemon.rb

Class Method Summary collapse

Class Method Details

.boot_with(options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/workling_daemon.rb', line 77

def self.boot_with(options)
  if options[: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"] = options[:rails_env]
    puts "=> Loading Rails with #{ENV["RAILS_ENV"]} environment..."
    require options[: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



55
56
57
58
59
60
61
# File 'lib/workling_daemon.rb', line 55

def self.extract_options(options)
  result = {}
  result[:client] = options[:client] if options[:client]
  result[:routing] = options[:routing] if options[:routing]
  result[:invoker] = options[:invoker] if options[:invoker]
  result
end

.initialize_workling(options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/workling_daemon.rb', line 63

def self.initialize_workling(options)
  Workling.load_path = options[:load_path] if options[:load_path]
  Workling::Discovery.discover!

  if options[:config_path]
    Workling.config_path = options[:config_path]
    Workling.config
  else
    Workling.config = extract_options options
  end

  Workling.select_and_build_invoker
end

.parse_daemon_options(argv) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/workling_daemon.rb', line 22

def self.parse_daemon_options(argv)
  options = {}
  pass_through, args = partition_daemons_options argv
  opts = OptionParser.new do |opts|
    opts.banner = 'Usage: myapp [options]'
    opts.separator ''
    opts.on('-a', '--app-name APP_NAME', String,"specify the process name") { |v| options[:app_name] = v }
    opts.on('-d', '--dir DIR', String, "the directory to run in") { |v| options[:dir] = v }
    opts.on('-m', '--monitor',"specify the process name") { |v| options[:monitor] = true }
    opts.on('-t', '--ontop') { |k, v| pass_through << v  }
  end
  opts.parse!(partition_options(args).first)
  options.merge(:ARGV => pass_through)
end

.parse_workling_options(args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/workling_daemon.rb', line 37

def self.parse_workling_options(args)
  options = {}
  opts = OptionParser.new do |opts|
    opts.banner = 'Usage: myapp [options]'
    opts.separator ''
    opts.on('-n', '--no_rails', "do not load Rails") { |v| options[:no_rails] = true }
    opts.on('-c', '--client CLIENT', String, "specify the client class") { |v| options[:client] = v }
    opts.on('-i', '--invoker INVOKER', String, "specify the invoker class") { |v| options[:invoker] = v }
    opts.on('-r', '--routing ROUTING', String, "specify the routing class") { |v| options[:routing] = v }
    opts.on('-l', '--load-path LOADPATH', String, "specify the load_path for the workers") { |v| options[:load_path] = v }
    opts.on('-f', '--config-path CONFIGPATH', String, "specify the path to the workling.yml file") { |v| options[:config_path] = v }
    opts.on('-e', '--environment ENVIRONMENT', String, "specify the environment") { |v| options[:rails_env] = v }
    opts.on('-p', '--prefix PREFIX', String, "specify the prefix for queues") { |v| options[:prefix] = v }
  end
  opts.parse!(partition_options(args).last)
  options
end

.partition_daemons_options(args) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/workling_daemon.rb', line 14

def self.partition_daemons_options(args)
  standard_options = %W{start stop restart run zap -t --ontop -f --force -h --help --version}
  pass_through = args.select { |a| standard_options.include? a }
  custom_options = args.reject { |a| standard_options.include? a }

  [pass_through, custom_options]
end

.partition_options(args) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/workling_daemon.rb', line 5

def self.partition_options(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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/workling_daemon.rb', line 94

def self.run(options)
  boot_with options
  poller = initialize_workling(options)

  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.empty?
    puts '** Exiting'
  end
end