Class: Karafka::Pro::Processing::Schedulers::Base
- Inherits:
-
Object
- Object
- Karafka::Pro::Processing::Schedulers::Base
- Defined in:
- lib/karafka/pro/processing/schedulers/base.rb
Overview
All the ‘on_` methods can be redefined with a non-thread-safe versions without locks if needed, however when doing so, ensure that your scheduler is stateless.
Base for all the Pro custom schedulers
It wraps the Scheduler API with mutex to ensure, that during scheduling we do not start scheduling other work that could impact the decision making in between multiple subscription groups running in separate threads.
Direct Known Subclasses
Instance Method Summary collapse
-
#clear(_group_id) ⇒ Object
By default schedulers are stateless, so nothing to clear.
-
#initialize(queue) ⇒ Base
constructor
A new instance of Base.
-
#manage ⇒ Object
Should manage scheduling on jobs state changes.
-
#on_clear(group_id) ⇒ Object
Runs clearing under mutex.
-
#on_manage ⇒ Object
Runs the manage tick under mutex.
-
#on_schedule_consumption(jobs_array) ⇒ Object
Runs the consumption jobs scheduling flow under a mutex.
-
#on_schedule_idle(jobs_array) ⇒ Object
Runs the idle jobs scheduling flow under a mutex.
-
#on_schedule_periodic(jobs_array) ⇒ Object
Runs the periodic jobs scheduling flow under a mutex.
-
#on_schedule_revocation(jobs_array) ⇒ Object
Runs the revocation jobs scheduling flow under a mutex.
-
#on_schedule_shutdown(jobs_array) ⇒ Object
Runs the shutdown jobs scheduling flow under a mutex.
-
#schedule_consumption(_jobs_array) ⇒ Object
Should schedule the consumption jobs.
-
#schedule_fifo(jobs_array) ⇒ Object
(also: #schedule_revocation, #schedule_shutdown, #schedule_idle, #schedule_periodic)
Schedules any jobs provided in a fifo order.
Constructor Details
#initialize(queue) ⇒ Base
Returns a new instance of Base.
46 47 48 49 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 46 def initialize(queue) @queue = queue @mutex = Mutex.new end |
Instance Method Details
#clear(_group_id) ⇒ Object
By default schedulers are stateless, so nothing to clear.
147 148 149 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 147 def clear(_group_id) nil end |
#manage ⇒ Object
Should manage scheduling on jobs state changes
By default does nothing as default schedulers are stateless
133 134 135 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 133 def manage nil end |
#on_clear(group_id) ⇒ Object
Runs clearing under mutex
140 141 142 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 140 def on_clear(group_id) @mutex.synchronize { clear(group_id) } end |
#on_manage ⇒ Object
Runs the manage tick under mutex
126 127 128 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 126 def on_manage @mutex.synchronize { manage } end |
#on_schedule_consumption(jobs_array) ⇒ Object
Runs the consumption jobs scheduling flow under a mutex
64 65 66 67 68 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 64 def on_schedule_consumption(jobs_array) @mutex.synchronize do schedule_consumption(jobs_array) end end |
#on_schedule_idle(jobs_array) ⇒ Object
Runs the idle jobs scheduling flow under a mutex
102 103 104 105 106 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 102 def on_schedule_idle(jobs_array) @mutex.synchronize do schedule_idle(jobs_array) end end |
#on_schedule_periodic(jobs_array) ⇒ Object
Runs the periodic jobs scheduling flow under a mutex
113 114 115 116 117 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 113 def on_schedule_periodic(jobs_array) @mutex.synchronize do schedule_periodic(jobs_array) end end |
#on_schedule_revocation(jobs_array) ⇒ Object
Runs the revocation jobs scheduling flow under a mutex
84 85 86 87 88 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 84 def on_schedule_revocation(jobs_array) @mutex.synchronize do schedule_revocation(jobs_array) end end |
#on_schedule_shutdown(jobs_array) ⇒ Object
Runs the shutdown jobs scheduling flow under a mutex
93 94 95 96 97 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 93 def on_schedule_shutdown(jobs_array) @mutex.synchronize do schedule_shutdown(jobs_array) end end |
#schedule_consumption(_jobs_array) ⇒ Object
Should schedule the consumption jobs
75 76 77 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 75 def schedule_consumption(_jobs_array) raise NotImplementedError, "Implement in a subclass" end |
#schedule_fifo(jobs_array) ⇒ Object Also known as: schedule_revocation, schedule_shutdown, schedule_idle, schedule_periodic
Schedules any jobs provided in a fifo order
53 54 55 56 57 |
# File 'lib/karafka/pro/processing/schedulers/base.rb', line 53 def schedule_fifo(jobs_array) jobs_array.each do |job| @queue << job end end |