Skip to content

Commit

Permalink
Create global GoodJob.configuration object
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Jul 26, 2022
1 parent d67d677 commit 07a7311
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
11 changes: 8 additions & 3 deletions lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ module GoodJob
# @return [Proc, nil]
mattr_accessor :on_thread_error, default: nil

# @!attribute [rw] configuration
# @!scope class
# Global configuration object for GoodJob.
# @return [GoodJob::Configuration, nil]
mattr_accessor :configuration, default: GoodJob::Configuration.new

# Called with exception when a GoodJob thread raises an exception
# @param exception [Exception] Exception that was raised
# @return [void]
Expand Down Expand Up @@ -135,10 +141,9 @@ def self._shutdown_all(executables, method_name = :shutdown, timeout: -1)
# @params older_than [nil,Numeric,ActiveSupport::Duration] Jobs older than this will be destroyed (default: +86400+).
# @return [Integer] Number of jobs that were destroyed.
def self.cleanup_preserved_jobs(older_than: nil)
configuration = GoodJob::Configuration.new({})
older_than ||= configuration.cleanup_preserved_jobs_before_seconds_ago
older_than ||= GoodJob.configuration.cleanup_preserved_jobs_before_seconds_ago
timestamp = Time.current - older_than
include_discarded = configuration.cleanup_discarded_jobs?
include_discarded = GoodJob.configuration.cleanup_discarded_jobs?

ActiveSupport::Notifications.instrument("cleanup_preserved_jobs.good_job", { older_than: older_than, timestamp: timestamp }) do |payload|
old_jobs = GoodJob::Job.where('finished_at <= ?', timestamp)
Expand Down
23 changes: 12 additions & 11 deletions lib/good_job/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Adapter
# @return [Array<GoodJob::Adapter>, nil]
cattr_reader :instances, default: [], instance_reader: false

attr_reader :execution_mode

# @param execution_mode [Symbol, nil] specifies how and where jobs should be executed. You can also set this with the environment variable +GOOD_JOB_EXECUTION_MODE+.
#
# - +:inline+ executes jobs immediately in whatever process queued them (usually the web server process). This should only be used in test and development environments.
Expand All @@ -25,8 +27,7 @@ class Adapter
# - +production+ and all other environments: +:external+
#
def initialize(execution_mode: nil)
@configuration = GoodJob::Configuration.new({ execution_mode: execution_mode })
@configuration.validate!
@execution_mode = (execution_mode || GoodJob::Configuration.execution_mode).to_sym
self.class.instances << self

start_async if GoodJob.async_ready?
Expand Down Expand Up @@ -82,7 +83,7 @@ def enqueue_at(active_job, timestamp)
# @return [void]
def shutdown(timeout: :default)
timeout = if timeout == :default
@configuration.shutdown_timeout
GoodJob.configuration.shutdown_timeout
else
timeout
end
Expand All @@ -95,21 +96,21 @@ def shutdown(timeout: :default)
# Whether in +:async+ execution mode.
# @return [Boolean]
def execute_async?
@configuration.execution_mode == :async_all ||
(@configuration.execution_mode.in?([:async, :async_server]) && in_server_process?)
execution_mode == :async_all ||
(execution_mode.in?([:async, :async_server]) && in_server_process?)
end

# Whether in +:external+ execution mode.
# @return [Boolean]
def execute_externally?
@configuration.execution_mode == :external ||
(@configuration.execution_mode.in?([:async, :async_server]) && !in_server_process?)
execution_mode == :external ||
(execution_mode.in?([:async, :async_server]) && !in_server_process?)
end

# Whether in +:inline+ execution mode.
# @return [Boolean]
def execute_inline?
@configuration.execution_mode == :inline
execution_mode == :inline
end

# Start async executors
Expand All @@ -118,12 +119,12 @@ def start_async
return unless execute_async?

@notifier = GoodJob::Notifier.new
@poller = GoodJob::Poller.new(poll_interval: @configuration.poll_interval)
@scheduler = GoodJob::Scheduler.from_configuration(@configuration, warm_cache_on_initialize: true)
@poller = GoodJob::Poller.new(poll_interval: GoodJob.configuration.poll_interval)
@scheduler = GoodJob::Scheduler.from_configuration(GoodJob.configuration, warm_cache_on_initialize: true)
@notifier.recipients << [@scheduler, :create_thread]
@poller.recipients << [@scheduler, :create_thread]

@cron_manager = GoodJob::CronManager.new(@configuration.cron_entries, start_on_initialize: true) if @configuration.enable_cron?
@cron_manager = GoodJob::CronManager.new(GoodJob.configuration.cron_entries, start_on_initialize: true) if GoodJob.configuration.enable_cron?

@_async_started = true
end
Expand Down
8 changes: 4 additions & 4 deletions lib/good_job/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def exit_on_failure?

def start
set_up_application!
configuration = GoodJob::Configuration.new(options)
GoodJob.configuration.options.merge!(options)
configuration = GoodJob.configuration

Daemon.new(pidfile: configuration.pidfile).daemonize if configuration.daemonize?

Expand Down Expand Up @@ -142,10 +143,9 @@ def start

def cleanup_preserved_jobs
set_up_application!
GoodJob.configuration.options.merge!(options)

configuration = GoodJob::Configuration.new(options)

GoodJob.cleanup_preserved_jobs(older_than: configuration.cleanup_preserved_jobs_before_seconds_ago)
GoodJob.cleanup_preserved_jobs(older_than: GoodJob.configuration.cleanup_preserved_jobs_before_seconds_ago)
end

no_commands do
Expand Down
4 changes: 2 additions & 2 deletions lib/good_job/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Configuration

# The options that were explicitly set when initializing +Configuration+.
# @return [Hash]
attr_reader :options
attr_accessor :options

# The environment from which to read GoodJob's environment variables. By
# default, this is the current process's environment, but it can be set
Expand All @@ -41,7 +41,7 @@ class Configuration
# use. Keys are symbols that match the various methods on this class.
# @param env [Hash] A +Hash+ from which to read environment variables that
# might specify additional configuration values.
def initialize(options, env: ENV)
def initialize(options = {}, env: ENV)
@options = options
@env = env
end
Expand Down
2 changes: 1 addition & 1 deletion lib/models/good_job/cron_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CronEntry
attr_reader :params

def self.all(configuration: nil)
configuration ||= GoodJob::Configuration.new({})
configuration ||= GoodJob.configuration
configuration.cron_entries
end

Expand Down

0 comments on commit 07a7311

Please sign in to comment.