Class: Lowdown::Threading::Consumer
- Inherits:
-
Object
- Object
- Lowdown::Threading::Consumer
- Defined in:
- lib/lowdown/threading.rb
Overview
This class performs jobs on a private thread, provides lifecycle callbacks, and sends exceptions onto its parent thread.
Instance Attribute Summary collapse
-
#parent_thread ⇒ Thread
readonly
protected
The thread to send exceptions to.
-
#queue ⇒ Thread::Queue
readonly
protected
The jobs queue.
-
#thread ⇒ Thread
readonly
protected
The private thread.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Whether or not the private thread is still alive.
-
#empty? ⇒ Boolean
Whether or not there are any scheduled jobs left in the queue.
-
#enqueue(&job) ⇒ void
Schedules a job to be performed.
-
#initialize(queue: Thread::Queue.new, parent_thread: Thread.current) ⇒ Consumer
constructor
A new instance of Consumer.
-
#kill ⇒ void
Kills the private thread.
-
#main ⇒ void
protected
This represents the full lifecycle of the consumer thread.
- #perform_job(non_block:, arguments: nil) ⇒ void protected
-
#post_runloop ⇒ void
protected
Ran when the thread is killed or an uncaught exception occurred.
-
#pre_runloop ⇒ void
protected
Ran before any jobs are performed.
-
#runloop ⇒ void
protected
The loop that performs scheduled jobs.
Constructor Details
#initialize(queue: Thread::Queue.new, parent_thread: Thread.current) ⇒ Consumer
Returns a new instance of Consumer.
17 18 19 20 |
# File 'lib/lowdown/threading.rb', line 17 def initialize(queue: Thread::Queue.new, parent_thread: Thread.current) @queue, @parent_thread = queue, parent_thread @thread = Thread.new(&method(:main)) end |
Instance Attribute Details
#parent_thread ⇒ Thread (readonly, protected)
Returns the thread to send exceptions to.
62 63 64 |
# File 'lib/lowdown/threading.rb', line 62 def parent_thread @parent_thread end |
#queue ⇒ Thread::Queue (readonly, protected)
Returns the jobs queue.
67 68 69 |
# File 'lib/lowdown/threading.rb', line 67 def queue @queue end |
#thread ⇒ Thread (readonly, protected)
Returns the private thread.
57 58 59 |
# File 'lib/lowdown/threading.rb', line 57 def thread @thread end |
Instance Method Details
#alive? ⇒ Boolean
Returns whether or not the private thread is still alive.
41 42 43 |
# File 'lib/lowdown/threading.rb', line 41 def alive? thread.alive? end |
#empty? ⇒ Boolean
Returns whether or not there are any scheduled jobs left in the queue.
48 49 50 |
# File 'lib/lowdown/threading.rb', line 48 def empty? queue.empty? end |
#enqueue(&job) ⇒ void
This method returns an undefined value.
Schedules a job to be performed.
26 27 28 |
# File 'lib/lowdown/threading.rb', line 26 def enqueue(&job) queue << job end |
#kill ⇒ void
This method returns an undefined value.
Kills the private thread.
34 35 36 |
# File 'lib/lowdown/threading.rb', line 34 def kill thread.kill end |
#main ⇒ void (protected)
This method is ran on the private thread.
This method returns an undefined value.
This represents the full lifecycle of the consumer thread. It performs the individual events, catches uncaught exceptions and sends those to the parent thread, and performs cleanup.
Subclasses should override the individual events.
78 79 80 81 82 83 84 85 |
# File 'lib/lowdown/threading.rb', line 78 def main pre_runloop runloop rescue Exception => exception parent_thread.raise(exception) ensure post_runloop end |
#perform_job(non_block:, arguments: nil) ⇒ void (protected)
This method returns an undefined value.
127 128 129 130 |
# File 'lib/lowdown/threading.rb', line 127 def perform_job(non_block:, arguments: nil) queue.pop(non_block).call(*arguments) rescue ThreadError end |
#post_runloop ⇒ void (protected)
This method is ran on the private thread.
This method returns an undefined value.
Ran when the thread is killed or an uncaught exception occurred.
This kills the consumer thread, which means that any cleanup you need to perform should be done before calling
this super implementation.
115 116 117 |
# File 'lib/lowdown/threading.rb', line 115 def post_runloop thread.kill end |
#pre_runloop ⇒ void (protected)
This method is ran on the private thread.
This method returns an undefined value.
Ran before any jobs are performed.
93 94 |
# File 'lib/lowdown/threading.rb', line 93 def pre_runloop end |
#runloop ⇒ void (protected)
This method is ran on the private thread.
This method returns an undefined value.
The loop that performs scheduled jobs.
102 103 104 |
# File 'lib/lowdown/threading.rb', line 102 def runloop loop { perform_job(non_block: false) } end |