Class: Rufus::Scheduler::SchedulerCore

Inherits:
Object
  • Object
show all
Includes:
LegacyMethods
Defined in:
lib/rufus/sc/scheduler.rb

Overview

The core of a rufus-scheduler. See implementations like Rufus::Scheduler::PlainScheduler and Rufus::Scheduler::EmScheduler for directly usable stuff.

Direct Known Subclasses

EmScheduler, PlainScheduler, SignalScheduler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LegacyMethods

#at_job_count, #cron_job_count, #every_job_count, #find_jobs, #pending_job_count, #precision

Constructor Details

#initialize(opts = {}) ⇒ SchedulerCore

Instantiates a Rufus::Scheduler.



100
101
102
103
104
105
106
107
108
# File 'lib/rufus/sc/scheduler.rb', line 100

def initialize (opts={})

  @options = opts

  @jobs = get_queue(:at, opts)
  @cron_jobs = get_queue(:cron, opts)

  @frequency = @options[:frequency] || 0.330
end

Instance Attribute Details

#optionsObject (readonly)

classical options hash



96
97
98
# File 'lib/rufus/sc/scheduler.rb', line 96

def options
  @options
end

Class Method Details

.start_new(opts = {}) ⇒ Object

Instantiates and starts a new Rufus::Scheduler.



112
113
114
115
116
117
# File 'lib/rufus/sc/scheduler.rb', line 112

def self.start_new (opts={})

  s = self.new(opts)
  s.start
  s
end

Instance Method Details

#all_jobsObject

Returns a map job_id => job of all the jobs currently in the scheduler



232
233
234
235
# File 'lib/rufus/sc/scheduler.rb', line 232

def all_jobs

  jobs.merge(cron_jobs)
end

#at(t, s = nil, opts = {}, &block) ⇒ Object Also known as: schedule_at

Schedules a job at a given point in time.

scheduler.at 'Thu Mar 26 19:30:00 2009' do
  puts 'order pizza'
end

pizza is for Thursday at 2000 (if the shop brochure is right).



145
146
147
148
# File 'lib/rufus/sc/scheduler.rb', line 145

def at (t, s=nil, opts={}, &block)

  add_job(AtJob.new(self, t, combine_opts(s, opts), &block))
end

#cron(cronstring, s = nil, opts = {}, &block) ⇒ Object Also known as: schedule

Schedules a job given a cron string.

scheduler.cron '0 22 * * 1-5' do
  # every day of the week at 00:22
  puts 'activate security system'
end


172
173
174
175
# File 'lib/rufus/sc/scheduler.rb', line 172

def cron (cronstring, s=nil, opts={}, &block)

  add_cron_job(CronJob.new(self, cronstring, combine_opts(s, opts), &block))
end

#cron_jobsObject

Returns a map job_id => job for cron jobs



225
226
227
228
# File 'lib/rufus/sc/scheduler.rb', line 225

def cron_jobs

  @cron_jobs.to_h
end

#every(t, s = nil, opts = {}, &block) ⇒ Object Also known as: schedule_every

Schedules a recurring job every t.

scheduler.every '5m1w' do
  puts 'check blood pressure'
end

checking blood pressure every 5 months and 1 week.



159
160
161
162
# File 'lib/rufus/sc/scheduler.rb', line 159

def every (t, s=nil, opts={}, &block)

  add_job(EveryJob.new(self, t, combine_opts(s, opts), &block))
end

#find_by_tag(tag) ⇒ Object

Returns a list of jobs with the given tag



239
240
241
242
# File 'lib/rufus/sc/scheduler.rb', line 239

def find_by_tag (tag)

  all_jobs.values.select { |j| j.tags.include?(tag) }
end

#handle_exception(job, exception) ⇒ Object

Feel free to override this method. The default implementation simply outputs the error message to STDOUT



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rufus/sc/scheduler.rb', line 194

def handle_exception (job, exception)

  if self.respond_to?(:log_exception)
    #
    # some kind of backward compatibility

    log_exception(exception)

  else

    puts '=' * 80
    puts "scheduler caught exception :"
    puts exception
    exception.backtrace.each { |l| puts l }
    puts '=' * 80
  end
end

#in(t, s = nil, opts = {}, &block) ⇒ Object Also known as: schedule_in

Schedules a job in a given amount of time.

scheduler.in '20m' do
  puts "order ristretto"
end

will order an espresso (well sort of) in 20 minutes.



131
132
133
134
# File 'lib/rufus/sc/scheduler.rb', line 131

def in (t, s=nil, opts={}, &block)

  add_job(InJob.new(self, t, combine_opts(s, opts), &block))
end

#jobsObject

Returns a map job_id => job for at/in/every jobs



218
219
220
221
# File 'lib/rufus/sc/scheduler.rb', line 218

def jobs

  @jobs.to_h
end

#trigger_threadsObject

Returns the current list of trigger threads (threads) dedicated to the execution of jobs.



247
248
249
250
251
252
# File 'lib/rufus/sc/scheduler.rb', line 247

def trigger_threads

  Thread.list.select { |t|
    t["rufus_scheduler__trigger_thread__#{self.object_id}"] == true
  }
end

#unschedule(job_id) ⇒ Object

Unschedules a job (cron or at/every/in job) given its id.

Returns the job that got unscheduled.



182
183
184
185
# File 'lib/rufus/sc/scheduler.rb', line 182

def unschedule (job_id)

  @jobs.unschedule(job_id) || @cron_jobs.unschedule(job_id)
end