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
109
110
# 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

  @mutexes = {}
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.



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

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



279
280
281
282
# File 'lib/rufus/sc/scheduler.rb', line 279

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).



147
148
149
150
# File 'lib/rufus/sc/scheduler.rb', line 147

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


174
175
176
177
# File 'lib/rufus/sc/scheduler.rb', line 174

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



272
273
274
275
# File 'lib/rufus/sc/scheduler.rb', line 272

def cron_jobs

  @cron_jobs.to_h
end

#do_handle_exception(job, exception) ⇒ Object

Determines if there is #log_exception, #handle_exception or #on_exception method. If yes, hands the exception to it, else defaults to outputting details to $stderr.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rufus/sc/scheduler.rb', line 225

def do_handle_exception(job, exception)

  begin

    [ :log_exception, :handle_exception, :on_exception ].each do |m|

      next unless self.respond_to?(m)

      if method(m).arity == 1
        self.send(m, exception)
      else
        self.send(m, job, exception)
      end

      return
        # exception was handled successfully
    end

  rescue Exception => e

    $stderr.puts '*' * 80
    $stderr.puts 'the exception handling method itself had an issue:'
    $stderr.puts e
    $stderr.puts *e.backtrace
    $stderr.puts '*' * 80
  end

  $stderr.puts '=' * 80
  $stderr.puts 'scheduler caught exception:'
  $stderr.puts exception
  $stderr.puts *exception.backtrace
  $stderr.puts '=' * 80
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.



161
162
163
164
# File 'lib/rufus/sc/scheduler.rb', line 161

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

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

#find(job_or_id) ⇒ Object

Mostly used to find a job given its id. If the argument is a job, will simply return it.

If the argument is an id, and no job with that id is found, it will raise an ArgumentError.

Raises:

  • (ArgumentError)


297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/rufus/sc/scheduler.rb', line 297

def find(job_or_id)

  return job_or_id if job_or_id.respond_to?(:job_id)

  job = all_jobs[job_or_id]

  raise ArgumentError.new(
    "couldn't find job #{job_or_id.inspect}"
  ) unless job

  job
end

#find_by_tag(tag) ⇒ Object

Returns a list of jobs with the given tag



286
287
288
289
# File 'lib/rufus/sc/scheduler.rb', line 286

def find_by_tag(tag)

  all_jobs.values.select { |j| j.tags.include?(tag) }
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.



133
134
135
136
# File 'lib/rufus/sc/scheduler.rb', line 133

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



265
266
267
268
# File 'lib/rufus/sc/scheduler.rb', line 265

def jobs

  @jobs.to_h
end

#pause(job_or_id) ⇒ Object

Pauses a given job. If the argument is an id (String) and the corresponding job cannot be found, an ArgumentError will get raised.



204
205
206
207
# File 'lib/rufus/sc/scheduler.rb', line 204

def pause(job_or_id)

  find(job_or_id).pause
end

#resume(job_or_id) ⇒ Object

Resumes a given job. If the argument is an id (String) and the corresponding job cannot be found, an ArgumentError will get raised.



212
213
214
215
# File 'lib/rufus/sc/scheduler.rb', line 212

def resume(job_or_id)

  find(job_or_id).resume
end

#running_jobsObject

Returns the list of the currently running jobs (jobs that just got triggered and are executing).



323
324
325
326
327
328
# File 'lib/rufus/sc/scheduler.rb', line 323

def running_jobs

  Thread.list.collect { |t|
    t["rufus_scheduler__trigger_thread__#{self.object_id}"]
  }.compact
end

#trigger_threadsObject

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



313
314
315
316
317
318
# File 'lib/rufus/sc/scheduler.rb', line 313

def trigger_threads

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

#unschedule(job_or_id) ⇒ Object

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

Returns the job that got unscheduled.



184
185
186
187
188
189
# File 'lib/rufus/sc/scheduler.rb', line 184

def unschedule(job_or_id)

  job_id = job_or_id.respond_to?(:job_id) ? job_or_id.job_id : job_or_id

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

#unschedule_by_tag(tag) ⇒ Object

Given a tag, unschedules all the jobs that bear that tag.



193
194
195
196
197
198
199
# File 'lib/rufus/sc/scheduler.rb', line 193

def unschedule_by_tag(tag)

  jobs = find_by_tag(tag)
  jobs.each { |job| unschedule(job.job_id) }

  jobs
end