Class: ActiveJob::Scheduler::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/active_job/scheduler/cli.rb

Overview

Controller for the schedule via the CLI.

Constant Summary collapse

USAGE =

A short doc explaining the CLI.

<<-EOF.gsub(/ {6}/, '')
  Usage: activejob-scheduler [options]

  Runs an active_job scheduler process directly (rather than via rake).

EOF
OPTIONS =

The various options our CLI takes.

[
  {
    args: ['-n', '--app-name [APP_NAME]',
           'Application name for procline'],
    callback: ->(options) { ->(n) { options[:app_name] = n } }
  },
  {
    args: ['-B', '--background', 'Run in the background [BACKGROUND]'],
    callback: ->(options) { ->(b) { options[:background] = b } }
  },
  {
    args: ['-D', '--dynamic-schedule',
           'Enable dynamic scheduling [DYNAMIC_SCHEDULE]'],
    callback: ->(options) { ->(d) { options[:dynamic] = d } }
  },
  {
    args: ['-E', '--environment [RAILS_ENV]', 'Environment name'],
    callback: ->(options) { ->(e) { options[:env] = e } }
  },
  {
    args: ['-I', '--initializer-path [INITIALIZER_PATH]',
           'Path to optional initializer ruby file'],
    callback: ->(options) { ->(i) { options[:initializer_path] = i } }
  },
  {
    args: ['-i', '--interval [RESQUE_SCHEDULER_INTERVAL]',
           'Interval for checking if a scheduled job must run'],
    callback: ->(options) { ->(i) { options[:poll_sleep_amount] = i } }
  },
  {
    args: ['-l', '--logfile [LOGFILE]', 'Log file name'],
    callback: ->(options) { ->(l) { options[:logfile] = l } }
  },
  {
    args: ['-F', '--logformat [LOGFORMAT]', 'Log output format'],
    callback: ->(options) { ->(f) { options[:logformat] = f } }
  },
  {
    args: ['-P', '--pidfile [PIDFILE]', 'PID file name'],
    callback: ->(options) { ->(p) { options[:pidfile] = p } }
  },
  {
    args: ['-q', '--quiet', 'Run with minimal output [QUIET]'],
    callback: ->(options) { ->(q) { options[:quiet] = q } }
  },
  {
    args: ['-v', '--verbose', 'Run with verbose output [VERBOSE]'],
    callback: ->(options) { ->(v) { options[:verbose] = v } }
  }
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, env) ⇒ Cli

Instantiate a new CLI handler with the given args.



69
70
71
72
73
# File 'lib/active_job/scheduler/cli.rb', line 69

def initialize(argv, env)
  @env = env
  @args = argv
  @options = option_parser.parse! argv
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/active_job/scheduler/cli.rb', line 6

def env
  @env
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/active_job/scheduler/cli.rb', line 6

def options
  @options
end

Class Method Details

.run(argv, env) ⇒ Object

Start the scheduler CLI immediately from given command-line arguments.



77
78
79
# File 'lib/active_job/scheduler/cli.rb', line 77

def self.run(argv, env)
  new(argv, env).run
end

Instance Method Details

#runObject

Now that we have args, actually begin running the schedule by loading each Job into Rufus::Scheduler. Rufus uses methods like ‘every` and `cron` to determine what kind of job you’re pushing into it, so we use send() to give the Job object the power to make that choice.



86
87
88
89
90
91
92
# File 'lib/active_job/scheduler/cli.rb', line 86

def run
  jobs.each do |job|
    rufus.send job.interval, *job.rufus_params
  end

  rufus.start
end