Class: Arj::Worker
- Inherits:
-
Object
- Object
- Arj::Worker
- Defined in:
- lib/arj/worker.rb
Overview
Used to retrieve and execute jobs.
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
A description for this worker, used in logs.
-
#logger ⇒ Object
Used to log worker progress.
-
#sleep_delay ⇒ Object
readonly
Duration to sleep in #start after executing all available jobs, before retying.
Instance Method Summary collapse
-
#execute_next ⇒ Boolean
Executes the next available job.
-
#initialize(description: 'Arj::Worker(*)', source: -> { Arj.available.first }, sleep_delay: DEFAULT_SLEEP_DELAY, logger: ActiveJob::Base.logger) ⇒ Worker
constructor
A new instance of Worker.
-
#start ⇒ Object
Executes jobs as they become available.
-
#work_off ⇒ Boolean
Executes any available jobs.
Constructor Details
#initialize(description: 'Arj::Worker(*)', source: -> { Arj.available.first }, sleep_delay: DEFAULT_SLEEP_DELAY, logger: ActiveJob::Base.logger) ⇒ Worker
Returns a new instance of Worker.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/arj/worker.rb', line 26 def initialize( description: 'Arj::Worker(*)', source: -> { Arj.available.first }, sleep_delay: DEFAULT_SLEEP_DELAY, logger: ActiveJob::Base.logger ) @description = description @source = source @sleep_delay = sleep_delay @logger = logger end |
Instance Attribute Details
#description ⇒ Object (readonly)
A description for this worker, used in logs.
14 15 16 |
# File 'lib/arj/worker.rb', line 14 def description @description end |
#logger ⇒ Object
Used to log worker progress.
20 21 22 |
# File 'lib/arj/worker.rb', line 20 def logger @logger end |
#sleep_delay ⇒ Object (readonly)
Duration to sleep in #start after executing all available jobs, before retying.
17 18 19 |
# File 'lib/arj/worker.rb', line 17 def sleep_delay @sleep_delay end |
Instance Method Details
#execute_next ⇒ Boolean
Executes the next available job. Returns true
if a job was executed, false
otherwise.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/arj/worker.rb', line 57 def execute_next @logger.info("#{description} - Looking for the next available job") if (job = @source.call) job.perform_now rescue nil true else @logger.info("#{description} - No available jobs found") false end end |
#start ⇒ Object
Executes jobs as they become available.
39 40 41 42 43 44 |
# File 'lib/arj/worker.rb', line 39 def start loop do work_off sleep @sleep_delay.in_seconds end end |
#work_off ⇒ Boolean
Executes any available jobs. Returns true
if any jobs were executed, false
otherwise.
49 50 51 52 |
# File 'lib/arj/worker.rb', line 49 def work_off result = true while execute_next result || false end |