Class: Delayed::Pool

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ Pool

Returns a new instance of Pool.



13
14
15
16
17
18
19
20
21
22
# File 'lib/delayed/pool.rb', line 13

def initialize(args = ARGV)
  @args = args
  @workers = {}
  @config = { :workers => [] }
  @options = {
    :config_file => Settings.default_worker_config_name,
    :pid_folder => Settings.expand_rails_path("tmp/pids"),
    :tail_logs => true, # only in FG mode
  }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/delayed/pool.rb', line 11

def options
  @options
end

#workersObject (readonly)

Returns the value of attribute workers.



11
12
13
# File 'lib/delayed/pool.rb', line 11

def workers
  @workers
end

Instance Method Details

#parse_cli_options!Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/delayed/pool.rb', line 65

def parse_cli_options!
  op = OptionParser.new do |opts|
    opts.banner = "Usage #{$0} <command> <options>"
    opts.separator %{\nWhere <command> is one of:
start      start the jobs daemon
stop       stop the jobs daemon
run        start and run in the foreground
restart    stop and then start the jobs daemon
status     show daemon status
}

    opts.separator "\n<options>"
    opts.on("-c", "--config [CONFIG_PATH]", "Use alternate config file (default #{options[:config_file]})") { |c| options[:config_file] = c }
    opts.on("-p", "--pid", "Use alternate folder for PID files (default #{options[:pid_folder]})") { |p| options[:pid_folder] = p }
    opts.on("--no-tail", "Don't tail the logs (only affects non-daemon mode)") { options[:tail_logs] = false }
    opts.on("--with-prejudice", "When stopping, interrupt jobs in progress, instead of letting them drain") { options[:kill] ||= true }
    opts.on("--with-extreme-prejudice", "When stopping, immediately kill jobs in progress, instead of letting them drain") { options[:kill] = 9 }
    opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
  end
  op.parse!(@args)
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/delayed/pool.rb', line 24

def run
  parse_cli_options!

  read_config(options[:config_file])

  command = @args.shift
  case command
  when 'start'
    exit 1 if status(print: :alive) == :running
    daemonize
    start
  when 'stop'
    stop(kill: options[:kill])
  when 'run'
    start
  when 'status'
    if status
      exit 0
    else
      exit 1
    end
  when 'restart'
    pid = self.pid
    alive = status(pid: pid, print: false)
    if alive == :running || (options[:kill] && alive == :draining)
      stop(pid: pid, kill: options[:kill])
      if options[:kill]
        sleep(0.5) while status(pid: pid, print: false)
      else
        sleep(0.5) while status(pid: pid, print: false) == :running
      end
    end
    daemonize
    start
  when nil
    puts op
  else
    raise("Unknown command: #{command.inspect}")
  end
end