Class: Ask::Agent::Scheduler
- Inherits:
-
Object
- Object
- Ask::Agent::Scheduler
- Defined in:
- lib/ask/agent/scheduler.rb
Overview
Schedules recurring agent runs.
Configure tasks through Configuration#scheduler, then
start the scheduler loop. Tasks run in background threads managed by
rufus-scheduler.
Class Method Summary collapse
-
.instance ⇒ Scheduler
The singleton instance.
-
.job_by_name(name) ⇒ Rufus::Scheduler::Job?
Find a scheduled job by its name.
-
.jobs ⇒ Array<Rufus::Scheduler::Job>
Currently scheduled jobs.
-
.running? ⇒ Boolean
Whether the scheduler loop is active.
-
.start ⇒ Scheduler
Start the scheduler background thread.
-
.stop ⇒ Object
Gracefully stop the scheduler and wait for running tasks.
Instance Method Summary collapse
-
#initialize ⇒ Scheduler
constructor
A new instance of Scheduler.
-
#jobs ⇒ Array<Rufus::Scheduler::Job>
Currently scheduled jobs.
-
#start ⇒ self
Start the rufus-scheduler loop and register all configured tasks.
-
#stop ⇒ Object
Stop the scheduler and wait for running jobs to finish.
-
#up? ⇒ Boolean
True if the rufus scheduler is active.
Constructor Details
#initialize ⇒ Scheduler
Returns a new instance of Scheduler.
74 75 76 77 |
# File 'lib/ask/agent/scheduler.rb', line 74 def initialize @rufus = nil @own_mutex = Mutex.new end |
Class Method Details
.instance ⇒ Scheduler
Returns the singleton instance.
32 33 34 |
# File 'lib/ask/agent/scheduler.rb', line 32 def instance @instance ||= new end |
.job_by_name(name) ⇒ Rufus::Scheduler::Job?
Find a scheduled job by its name.
69 70 71 |
# File 'lib/ask/agent/scheduler.rb', line 69 def job_by_name(name) jobs.find { |j| j.name == name } end |
.jobs ⇒ Array<Rufus::Scheduler::Job>
Returns currently scheduled jobs.
62 63 64 |
# File 'lib/ask/agent/scheduler.rb', line 62 def jobs instance.jobs end |
.running? ⇒ Boolean
Returns whether the scheduler loop is active.
57 58 59 |
# File 'lib/ask/agent/scheduler.rb', line 57 def running? instance.up? end |
.start ⇒ Scheduler
Start the scheduler background thread. All configured tasks begin running on their schedules.
39 40 41 42 43 44 45 46 |
# File 'lib/ask/agent/scheduler.rb', line 39 def start @mutex.synchronize do return instance if instance.up? instance.start end instance end |
.stop ⇒ Object
Gracefully stop the scheduler and wait for running tasks.
49 50 51 52 53 54 |
# File 'lib/ask/agent/scheduler.rb', line 49 def stop @mutex.synchronize do instance.stop @instance = nil end end |
Instance Method Details
#jobs ⇒ Array<Rufus::Scheduler::Job>
Returns currently scheduled jobs.
113 114 115 |
# File 'lib/ask/agent/scheduler.rb', line 113 def jobs @rufus&.jobs || [] end |
#start ⇒ self
Start the rufus-scheduler loop and register all configured tasks.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ask/agent/scheduler.rb', line 81 def start @own_mutex.synchronize do return self if @rufus&.up? require "rufus-scheduler" @rufus = Rufus::Scheduler.new config = Ask::Agent.configuration # Register tasks that were configured statically config.scheduler.each_task do |task| schedule_task(task) end end self end |
#stop ⇒ Object
Stop the scheduler and wait for running jobs to finish.
100 101 102 103 104 105 |
# File 'lib/ask/agent/scheduler.rb', line 100 def stop @own_mutex.synchronize do @rufus&.shutdown(:wait) @rufus = nil end end |
#up? ⇒ Boolean
Returns true if the rufus scheduler is active.
108 109 110 |
# File 'lib/ask/agent/scheduler.rb', line 108 def up? @rufus&.up? || false end |