class GoodJob::CLI

Implements the good_job command-line tool, which executes jobs and provides other utilities. The actual entry point is in exe/good_job, but it just sets up and calls this class.

The good_job command-line tool is based on Thor, a CLI framework for Ruby. For more on general usage, see whatisthor.com/ and github.com/erikhuda/thor/wiki.

Constants

RAILS_ENVIRONMENT_RB

Path to the local Rails application's environment configuration. Requiring this loads the application's configuration and classes.

Attributes

log_to_stdout[RW]

Whether to log to STDOUT @return [Boolean, nil]

log_to_stdout?[RW]

Whether to log to STDOUT @return [Boolean, nil]

within_exe[RW]

Whether the CLI is running from the executable @return [Boolean, nil]

within_exe?[RW]

Whether the CLI is running from the executable @return [Boolean, nil]

Public Class Methods

exit_on_failure?() click to toggle source

@!visibility private

   # File lib/good_job/cli.rb
31 def exit_on_failure?
32   true
33 end

Public Instance Methods

cleanup_preserved_jobs() click to toggle source
    # File lib/good_job/cli.rb
132 def cleanup_preserved_jobs
133   set_up_application!
134 
135   configuration = GoodJob::Configuration.new(options)
136 
137   GoodJob.cleanup_preserved_jobs(older_than: configuration.cleanup_preserved_jobs_before_seconds_ago)
138 end
set_up_application!() click to toggle source

Load the current Rails application and configuration that the good_job command-line tool should be working within.

GoodJob components that need access to constants, classes, etc. from Rails or from the application can be set up here.

    # File lib/good_job/cli.rb
146 def set_up_application!
147   require RAILS_ENVIRONMENT_RB
148   return unless GoodJob::CLI.log_to_stdout? && !ActiveSupport::Logger.logger_outputs_to?(GoodJob.logger, $stdout)
149 
150   GoodJob::LogSubscriber.loggers << ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new($stdout))
151   GoodJob::LogSubscriber.reset_logger
152 end
start() click to toggle source
    # File lib/good_job/cli.rb
 83 def start
 84   set_up_application!
 85   configuration = GoodJob::Configuration.new(options)
 86 
 87   Daemon.new(pidfile: configuration.pidfile).daemonize if configuration.daemonize?
 88 
 89   notifier = GoodJob::Notifier.new
 90   poller = GoodJob::Poller.new(poll_interval: configuration.poll_interval)
 91   scheduler = GoodJob::Scheduler.from_configuration(configuration, warm_cache_on_initialize: true)
 92   notifier.recipients << [scheduler, :create_thread]
 93   poller.recipients << [scheduler, :create_thread]
 94   cron_manager = GoodJob::CronManager.new(configuration.cron, start_on_initialize: true) if configuration.enable_cron?
 95   @stop_good_job_executable = false
 96   %w[INT TERM].each do |signal|
 97     trap(signal) { @stop_good_job_executable = true }
 98   end
 99 
100   Kernel.loop do
101     sleep 0.1
102     break if @stop_good_job_executable || scheduler.shutdown? || notifier.shutdown?
103   end
104 
105   executors = [notifier, poller, cron_manager, scheduler].compact
106   GoodJob._shutdown_all(executors, timeout: configuration.shutdown_timeout)
107 end