Class: Timberline::Worker
- Inherits:
-
Object
- Object
- Timberline::Worker
- Defined in:
- lib/timberline/worker.rb
Overview
Worker is the base class for Timberline Workers. It defines the basics for processing items off of a queue; the idea is that creating your own worker is as easy as extending Worker and implementing #process_item. You can also override #keep_watching? and #initialize to provide your own custom behavior easily, although this is not necessary.
Direct Known Subclasses
Instance Method Summary collapse
-
#error_item(item) ⇒ Object
Given an item this worker is processing, have the queue mark it as fatally errored and raise an ItemErrored exception so that the watch loop can process it correctly.
-
#executing_job? ⇒ boolean
Whether the worker is currently executing a job.
-
#handle_process_exception(exception, item) ⇒ Object
Called when process_item has resulted in an exception.
-
#keep_watching? ⇒ boolean
Determine whether or not the worker loop in #watch should continue executing.
-
#process_item(item) ⇒ Object
Given an item off of the queue, process it appropriately.
-
#retry_item(item) ⇒ Object
Given an item this worker is processing, have the queue mark it attempt to retry it and raise an ItemRetried exception so that the watch loop can process it correctly.
-
#watch(queue_name) ⇒ Object
Run the watch loop for this worker.
Instance Method Details
#error_item(item) ⇒ Object
Given an item this worker is processing, have the queue mark it as fatally errored and raise an ItemErrored exception so that the watch loop can process it correctly
82 83 84 85 |
# File 'lib/timberline/worker.rb', line 82 def error_item(item) @queue.error_item(item) raise Timberline::ItemErrored end |
#executing_job? ⇒ boolean
Whether the worker is currently executing a job.
62 63 64 |
# File 'lib/timberline/worker.rb', line 62 def executing_job? @executing_job == true end |
#handle_process_exception(exception, item) ⇒ Object
Called when process_item has resulted in an exception. Exceptions are handled by default by reraising. This method is available to allow subclasses to override or extend how exceptions are handled. A subclass may, for example, want to log errors, handle specific error types in specific ways, or call error_item by default on all errors.
73 74 75 |
# File 'lib/timberline/worker.rb', line 73 def handle_process_exception(exception, item) raise exception end |
#keep_watching? ⇒ boolean
Determine whether or not the worker loop in #watch should continue executing. By default this is always true.
55 56 57 |
# File 'lib/timberline/worker.rb', line 55 def keep_watching? true end |
#process_item(item) ⇒ Object
Given an item off of the queue, process it appropriately. Not implemented in Worker, as Worker is just a base class.
47 48 49 |
# File 'lib/timberline/worker.rb', line 47 def process_item(item) raise NotImplementedError end |
#retry_item(item) ⇒ Object
Given an item this worker is processing, have the queue mark it attempt to retry it and raise an ItemRetried exception so that the watch loop can process it correctly
92 93 94 95 |
# File 'lib/timberline/worker.rb', line 92 def retry_item(item) @queue.retry_item(item) raise Timberline::ItemRetried end |
#watch(queue_name) ⇒ Object
Run the watch loop for this worker. As long as #keep_watching? returns true, this will pop items off the queue and process them with #process_item. This method is also responsible for managing some extra timberline metadata (tracking when processing starts and stops, for example) and shouldn’t typically be overridden when you define your own worker.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/timberline/worker.rb', line 18 def watch(queue_name) @queue = Queue.new(queue_name) while(keep_watching?) item = @queue.pop @executing_job = true item.started_processing_at = Time.now.to_f begin begin process_item(item) rescue StandardError => e handle_process_exception(e, item) end item.finished_processing_at = Time.now.to_f @queue.add_success_stat(item) rescue ItemRetried, ItemErrored next ensure @executing_job = false end end end |