module GoodJob

GoodJob is a multithreaded, Postgres-based, ActiveJob backend for Ruby on Rails.

GoodJob is the top-level namespace and exposes configuration attributes.

Constants

VERSION

GoodJob gem version.

Public Class Methods

_executables() click to toggle source
    # File lib/good_job.rb
138 def self._executables
139   [].concat(
140     CronManager.instances,
141     Notifier.instances,
142     Poller.instances,
143     Scheduler.instances
144   )
145 end
_shutdown_all(executables, method_name = :shutdown, timeout: -1) click to toggle source

Sends #shutdown or #restart to executable objects ({GoodJob::Notifier}, {GoodJob::Poller}, {GoodJob::Scheduler}, {GoodJob::MultiScheduler}, {GoodJob::CronManager}) @param executables [Array<Notifier, Poller, Scheduler, MultiScheduler, CronManager>] Objects to shut down. @param method_name [:symbol] Method to call, e.g. :shutdown or :restart. @param timeout [nil,Numeric] @return [void]

    # File lib/good_job.rb
107 def self._shutdown_all(executables, method_name = :shutdown, timeout: -1)
108   if timeout.is_a?(Numeric) && timeout.positive?
109     executables.each { |executable| executable.send(method_name, timeout: nil) }
110 
111     stop_at = Time.current + timeout
112     executables.each { |executable| executable.send(method_name, timeout: [stop_at - Time.current, 0].max) }
113   else
114     executables.each { |executable| executable.send(method_name, timeout: timeout) }
115   end
116 end
cleanup_preserved_jobs(older_than: nil) click to toggle source

Deletes preserved job records. By default, GoodJob deletes job records when the job is performed and this method is not necessary. However, when `GoodJob.preserve_job_records = true`, the jobs will be preserved in the database. This is useful when wanting to analyze or inspect job performance. If you are preserving job records this way, use this method regularly to delete old records and preserve space in your database. @params older_than [nil,Numeric,ActiveSupport::Duration] Jobs olders than this will be deleted (default: 86400). @return [Integer] Number of jobs that were deleted.

    # File lib/good_job.rb
127 def self.cleanup_preserved_jobs(older_than: nil)
128   older_than ||= GoodJob::Configuration.new({}).cleanup_preserved_jobs_before_seconds_ago
129   timestamp = Time.current - older_than
130 
131   ActiveSupport::Notifications.instrument("cleanup_preserved_jobs.good_job", { older_than: older_than, timestamp: timestamp }) do |payload|
132     deleted_records_count = GoodJob::Job.finished(timestamp).delete_all
133 
134     payload[:deleted_records_count] = deleted_records_count
135   end
136 end
restart(timeout: -1) click to toggle source

Stops and restarts executing jobs. GoodJob does its work in pools of background threads. When forking processes you should shut down these background threads before forking, and restart them after forking. For example, you should use shutdown and restart when using async execution mode with Puma. See the {file:README.md#executing-jobs-async–in-process} for more explanation and examples. @param timeout [Numeric, nil] Seconds to wait for active threads to finish. @return [void]

    # File lib/good_job.rb
 98 def self.restart(timeout: -1)
 99   _shutdown_all(_executables, :restart, timeout: timeout)
100 end
shutdown(timeout: -1) click to toggle source

Stop executing jobs. GoodJob does its work in pools of background threads. When forking processes you should shut down these background threads before forking, and restart them after forking. For example, you should use shutdown and restart when using async execution mode with Puma. See the {file:README.md#executing-jobs-async–in-process} for more explanation and examples. @param timeout [nil, Numeric] Seconds to wait for actively executing jobs to finish

* +nil+, the scheduler will trigger a shutdown but not wait for it to complete.
* +-1+, the scheduler will wait until the shutdown is complete.
* +0+, the scheduler will immediately shutdown and stop any active tasks.
* +1..+, the scheduler will wait that many seconds before stopping any remaining active tasks.

@param wait [Boolean] whether to wait for shutdown @return [void]

   # File lib/good_job.rb
81 def self.shutdown(timeout: -1)
82   _shutdown_all(_executables, timeout: timeout)
83 end
shutdown?() click to toggle source

Tests whether jobs have stopped executing. @return [Boolean] whether background threads are shut down

   # File lib/good_job.rb
87 def self.shutdown?
88   _executables.all?(&:shutdown?)
89 end