Class: Yoda::Server::Scheduler
- Inherits:
-
Object
- Object
- Yoda::Server::Scheduler
- Defined in:
- lib/yoda/server/scheduler.rb
Instance Attribute Summary collapse
- #future_map ⇒ Concurrent::Map{String => Concurrent::Future} readonly
- #thread_pool ⇒ Concurrent::ThreadPoolExecutor readonly
Class Method Summary collapse
Instance Method Summary collapse
- #async(id:, &block) ⇒ Concurrent::Future
- #cancel(id) ⇒ Object
- #cancel_all ⇒ Object
-
#initialize(thread_pool: nil) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #wait_for_termination(timeout:) ⇒ Object
Constructor Details
#initialize(thread_pool: nil) ⇒ Scheduler
Returns a new instance of Scheduler.
18 19 20 21 |
# File 'lib/yoda/server/scheduler.rb', line 18 def initialize(thread_pool: nil) @thread_pool = thread_pool || self.class.default_thread_pool @future_map = Concurrent::Map.new end |
Instance Attribute Details
#future_map ⇒ Concurrent::Map{String => Concurrent::Future} (readonly)
10 11 12 |
# File 'lib/yoda/server/scheduler.rb', line 10 def future_map @future_map end |
#thread_pool ⇒ Concurrent::ThreadPoolExecutor (readonly)
7 8 9 |
# File 'lib/yoda/server/scheduler.rb', line 7 def thread_pool @thread_pool end |
Class Method Details
.default_thread_pool ⇒ Concurrent::ThreadPoolExecutor
13 14 15 |
# File 'lib/yoda/server/scheduler.rb', line 13 def self.default_thread_pool Concurrent.global_fast_executor end |
Instance Method Details
#async(id:, &block) ⇒ Concurrent::Future
25 26 27 28 29 30 31 |
# File 'lib/yoda/server/scheduler.rb', line 25 def async(id:, &block) future = Concurrent::Future.new(executor: thread_pool) { block.call } future.add_observer { |_time, value, reason| future_map.delete(id) } future_map.put_if_absent(id, future) future.execute future end |
#cancel(id) ⇒ Object
34 35 36 |
# File 'lib/yoda/server/scheduler.rb', line 34 def cancel(id) future_map[id]&.cancel end |
#cancel_all ⇒ Object
44 45 46 |
# File 'lib/yoda/server/scheduler.rb', line 44 def cancel_all future_map.each_value { |future| future&.cancel } end |
#wait_for_termination(timeout:) ⇒ Object
39 40 41 42 |
# File 'lib/yoda/server/scheduler.rb', line 39 def wait_for_termination(timeout:) thread_pool.shutdown thread_pool.wait_for_termination(timeout) end |