Class: Ask::Agent::Scheduler

Inherits:
Object
  • Object
show all
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.

Examples:

Ask::Agent.configure do |c|
  c.scheduler.every "5 minutes", name: "health-check" do
    Ask::Agent::Session.new(model: "gpt-4o").run("Check server health")
  end

  c.scheduler.cron "0 9 * * 1-5", name: "morning-report" do
    Ask::Agent::Session.new(model: "gpt-4o").run("Generate daily report")
  end
end

Ask::Agent::Scheduler.start   # background thread loop
Ask::Agent::Scheduler.running? # => true
Ask::Agent::Scheduler.jobs     # => list of rufus jobs
Ask::Agent::Scheduler.stop    # graceful shutdown

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

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

.instanceScheduler

Returns the singleton instance.

Returns:



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.

Parameters:

  • name (String)

    the job name

Returns:

  • (Rufus::Scheduler::Job, nil)


69
70
71
# File 'lib/ask/agent/scheduler.rb', line 69

def job_by_name(name)
  jobs.find { |j| j.name == name }
end

.jobsArray<Rufus::Scheduler::Job>

Returns currently scheduled jobs.

Returns:

  • (Array<Rufus::Scheduler::Job>)

    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.

Returns:

  • (Boolean)

    whether the scheduler loop is active



57
58
59
# File 'lib/ask/agent/scheduler.rb', line 57

def running?
  instance.up?
end

.startScheduler

Start the scheduler background thread. All configured tasks begin running on their schedules.

Returns:



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

.stopObject

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

#jobsArray<Rufus::Scheduler::Job>

Returns currently scheduled jobs.

Returns:

  • (Array<Rufus::Scheduler::Job>)

    currently scheduled jobs



113
114
115
# File 'lib/ask/agent/scheduler.rb', line 113

def jobs
  @rufus&.jobs || []
end

#startself

Start the rufus-scheduler loop and register all configured tasks.

Returns:

  • (self)


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

#stopObject

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.

Returns:

  • (Boolean)

    true if the rufus scheduler is active



108
109
110
# File 'lib/ask/agent/scheduler.rb', line 108

def up?
  @rufus&.up? || false
end