Class: GoodJob::Adapter
- Inherits:
-
Object
- Object
- GoodJob::Adapter
- Defined in:
- lib/good_job/adapter.rb
Overview
ActiveJob Adapter.
Direct Known Subclasses
Constant Summary collapse
- EXECUTION_MODES =
Valid execution modes.
[:async, :external, :inline].freeze
Instance Method Summary collapse
-
#enqueue(active_job) ⇒ GoodJob::Job
Enqueues the ActiveJob job to be performed.
-
#enqueue_at(active_job, timestamp) ⇒ GoodJob::Job
Enqueues an ActiveJob job to be run at a specific time.
-
#execute_async? ⇒ Boolean
Whether in
:async
execution mode. -
#execute_externally? ⇒ Boolean
Whether in
:external
execution mode. -
#execute_inline? ⇒ Boolean
Whether in
:inline
execution mode. -
#initialize(execution_mode: nil, queues: nil, max_threads: nil, poll_interval: nil, scheduler: nil, notifier: nil, inline: false) ⇒ Adapter
constructor
A new instance of Adapter.
-
#inline? ⇒ Boolean
(deprecated) Whether in
:inline
execution mode. -
#shutdown(wait: true) ⇒ void
Gracefully stop processing jobs.
Constructor Details
#initialize(execution_mode: nil, queues: nil, max_threads: nil, poll_interval: nil, scheduler: nil, notifier: nil, inline: false) ⇒ Adapter
Returns a new instance of Adapter.
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 51 |
# File 'lib/good_job/adapter.rb', line 26 def initialize(execution_mode: nil, queues: nil, max_threads: nil, poll_interval: nil, scheduler: nil, notifier: nil, inline: false) if inline && execution_mode.nil? ActiveSupport::Deprecation.warn('GoodJob::Adapter#new(inline: true) is deprecated; use GoodJob::Adapter.new(execution_mode: :inline) instead') execution_mode = :inline end configuration = GoodJob::Configuration.new( { execution_mode: execution_mode, queues: queues, max_threads: max_threads, poll_interval: poll_interval, } ) @execution_mode = configuration.execution_mode raise ArgumentError, "execution_mode: must be one of #{EXECUTION_MODES.join(', ')}." unless EXECUTION_MODES.include?(@execution_mode) if @execution_mode == :async # rubocop:disable Style/GuardClause @notifier = notifier || GoodJob::Notifier.new @poller = GoodJob::Poller.new(poll_interval: configuration.poll_interval) @scheduler = scheduler || GoodJob::Scheduler.from_configuration(configuration) @notifier.recipients << [@scheduler, :create_thread] @poller.recipients << [@scheduler, :create_thread] end end |
Instance Method Details
#enqueue(active_job) ⇒ GoodJob::Job
Enqueues the ActiveJob job to be performed. For use by Rails; you should generally not call this directly.
57 58 59 |
# File 'lib/good_job/adapter.rb', line 57 def enqueue(active_job) enqueue_at(active_job, nil) end |
#enqueue_at(active_job, timestamp) ⇒ GoodJob::Job
Enqueues an ActiveJob job to be run at a specific time. For use by Rails; you should generally not call this directly.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/good_job/adapter.rb', line 66 def enqueue_at(active_job, ) good_job = GoodJob::Job.enqueue( active_job, scheduled_at: ? Time.zone.at() : nil, create_with_advisory_lock: execute_inline? ) if execute_inline? begin good_job.perform ensure good_job.advisory_unlock end end executed_locally = execute_async? && @scheduler.create_thread(queue_name: good_job.queue_name) Notifier.notify(queue_name: good_job.queue_name) unless executed_locally good_job end |
#execute_async? ⇒ Boolean
Whether in :async
execution mode.
98 99 100 |
# File 'lib/good_job/adapter.rb', line 98 def execute_async? @execution_mode == :async end |
#execute_externally? ⇒ Boolean
Whether in :external
execution mode.
103 104 105 |
# File 'lib/good_job/adapter.rb', line 103 def execute_externally? @execution_mode == :external end |
#execute_inline? ⇒ Boolean
Whether in :inline
execution mode.
108 109 110 |
# File 'lib/good_job/adapter.rb', line 108 def execute_inline? @execution_mode == :inline end |
#inline? ⇒ Boolean
(deprecated) Whether in :inline
execution mode.
113 114 115 116 |
# File 'lib/good_job/adapter.rb', line 113 def inline? ActiveSupport::Deprecation.warn('GoodJob::Adapter::inline? is deprecated; use GoodJob::Adapter::execute_inline? instead') execute_inline? end |
#shutdown(wait: true) ⇒ void
This method returns an undefined value.
Gracefully stop processing jobs. Waits for termination by default.
91 92 93 94 95 |
# File 'lib/good_job/adapter.rb', line 91 def shutdown(wait: true) @notifier&.shutdown(wait: wait) @poller&.shutdown(wait: wait) @scheduler&.shutdown(wait: wait) end |