Class: Cron::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/app/jobs/cron/command.rb

Overview

Provide a friendly command structure for managing the cron server

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Command

Initialize the command



20
21
22
23
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
# File 'lib/app/jobs/cron/command.rb', line 20

def initialize(args)
  @options = { pid_dir: "#{root}/tmp/pids", log_dir: "#{root}/log", monitor: false }

  opts = OptionParser.new do |opt|
    opt.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options] start|stop|restart|run"

    opt.on('-h', '--help', 'Show this message') do
      warn opt
      exit 1
    end
    opt.on('-e', '--environment=NAME', 'Specifies the environment to run this delayed jobs under (test/development/production).') do
      warn 'The -e/--environment option has been deprecated and has no effect. Use RAILS_ENV and see http://github.com/collectiveidea/delayed_job/issues/7'
    end
    opt.on('--pid-dir=DIR', 'Specifies an alternate directory in which to store the process ids.') do |dir|
      @options[:pid_dir] = dir
    end
    opt.on('--log-dir=DIR', 'Specifies an alternate directory in which to store the delayed_job log.') do |dir|
      @options[:log_dir] = dir
    end
    opt.on('-m', '--monitor', 'Start monitor process.') do
      @options[:monitor] = true
    end
    opt.on('--exit-on-complete', 'Exit when no more jobs are available to run. This will exit if all jobs are scheduled to run in the future.') do
      @options[:exit_on_complete] = true
    end
    opt.on('--daemon-options a, b, c', Array, 'options to be passed through to daemons gem') do |daemon_options|
      @daemon_options = daemon_options
    end
  end
  @args = opts.parse!(args) + (@daemon_options || [])
end

Instance Method Details

#daemonizeObject

Start the background loop



55
56
57
58
59
60
61
62
63
# File 'lib/app/jobs/cron/command.rb', line 55

def daemonize
  dir = @options[:pid_dir]
  FileUtils.mkdir_p(dir) unless File.exist?(dir)

  Cron::JobTab.ensure_cron_tabs
  Daemons.run_proc('cron_server', dir: @options[:pid_dir], monitor: @options[:monitor], ARGV: @args) do
    loop { sleep Cron::Server.find_or_create_server.execute }
  end
end