Class Rufus::Scheduler
In: lib/rufus/scheduler.rb
lib/rufus/scheduler/locks.rb
lib/rufus/scheduler/jobs.rb
lib/rufus/scheduler/cronline.rb
lib/rufus/scheduler/job_array.rb
lib/rufus/scheduler/zotime.rb
lib/rufus/scheduler/util.rb
Parent: Object

Methods

Classes and Modules

Class Rufus::Scheduler::AtJob
Class Rufus::Scheduler::CronJob
Class Rufus::Scheduler::CronLine
Class Rufus::Scheduler::D
Class Rufus::Scheduler::Error
Class Rufus::Scheduler::EvInJob
Class Rufus::Scheduler::EveryJob
Class Rufus::Scheduler::FileLock
Class Rufus::Scheduler::InJob
Class Rufus::Scheduler::IntervalJob
Class Rufus::Scheduler::Job
Class Rufus::Scheduler::JobArray
Class Rufus::Scheduler::NotRunningError
Class Rufus::Scheduler::NullLock
Class Rufus::Scheduler::OneTimeJob
Class Rufus::Scheduler::RepeatJob
Class Rufus::Scheduler::TimeoutError
Class Rufus::Scheduler::ZoTime

Constants

VERSION = '3.3.4'
MAX_WORK_THREADS = 28   MIN_WORK_THREADS = 3
DURATIONS2M = [ [ 'y', 365 * 24 * 3600 ], [ 'M', 30 * 24 * 3600 ], [ 'w', 7 * 24 * 3600 ], [ 'd', 24 * 3600 ], [ 'h', 3600 ], [ 'm', 60 ], [ 's', 1 ]
DURATIONS2 = DURATIONS2M.dup
DURATIONS = DURATIONS2M.inject({}) { |r, (k, v)| r[k] = v;
DURATION_LETTERS = DURATIONS.keys.join
DU_KEYS = DURATIONS2M.collect { |k, v| k.to_sym }

External Aliases

parse_duration -> parse_duration_string
  -
 for compatibility with rufus-scheduler 2.x

+

parse_duration -> parse_time_string
to_duration -> to_duration_string
  -
 for compatibility with rufus-scheduler 2.x

+

to_duration -> to_time_string

Attributes

frequency  [RW] 
max_work_threads  [RW]  attr_accessor :min_work_threads
mutexes  [R] 
started_at  [R] 
stderr  [RW] 
thread  [R] 
thread_key  [R] 
work_queue  [R] 

Public Class methods

Produces a hour/min/sec/milli string representation of Time instance

Turns a string like ‘1m10s’ into a float like ‘70.0’, more formally, turns a time duration expressed as a string into a Float instance (millisecond count).

w -> week d -> day h -> hour m -> minute s -> second M -> month y -> year ‘nada’ -> millisecond

Some examples:

  Rufus::Scheduler.parse_duration "0.5"    # => 0.5
  Rufus::Scheduler.parse_duration "500"    # => 0.5
  Rufus::Scheduler.parse_duration "1000"   # => 1.0
  Rufus::Scheduler.parse_duration "1h"     # => 3600.0
  Rufus::Scheduler.parse_duration "1h10s"  # => 3610.0
  Rufus::Scheduler.parse_duration "1w2d"   # => 777600.0

Negative time strings are OK (Thanks Danny Fullerton):

  Rufus::Scheduler.parse_duration "-0.5"   # => -0.5
  Rufus::Scheduler.parse_duration "-1h"    # => -3600.0

Releasing the gem would probably require redirecting .start_new to .new and emit a simple deprecation message.

For now, let‘s assume the people pointing at rufus-scheduler/master on GitHub know what they do…

Turns a number of seconds into a a time string

  Rufus.to_duration 0                    # => '0s'
  Rufus.to_duration 60                   # => '1m'
  Rufus.to_duration 3661                 # => '1h1m1s'
  Rufus.to_duration 7 * 24 * 3600        # => '1w'
  Rufus.to_duration 30 * 24 * 3600 + 1   # => "4w2d1s"

It goes from seconds to the year. Months are not counted (as they are of variable length). Weeks are counted.

For 30 days months to be counted, the second parameter of this method can be set to true.

  Rufus.to_duration 30 * 24 * 3600 + 1, true   # => "1M1s"

If a Float value is passed, milliseconds will be displayed without ‘marker‘

  Rufus.to_duration 0.051                       # => "51"
  Rufus.to_duration 7.051                       # => "7s51"
  Rufus.to_duration 0.120 + 30 * 24 * 3600 + 1  # => "4w2d1s120"

(this behaviour mirrors the one found for parse_time_string()).

Options are :

  • :months, if set to true, months (M) of 30 days will be taken into account when building up the result
  • :drop_seconds, if set to true, seconds and milliseconds will be trimmed from the result

Turns a number of seconds (integer or Float) into a hash like in :

  Rufus.to_duration_hash 0.051
    # => { :ms => "51" }
  Rufus.to_duration_hash 7.051
    # => { :s => 7, :ms => "51" }
  Rufus.to_duration_hash 0.120 + 30 * 24 * 3600 + 1
    # => { :w => 4, :d => 2, :s => 1, :ms => "120" }

This method is used by to_duration behind the scenes.

Options are :

  • :months, if set to true, months (M) of 30 days will be taken into account when building up the result
  • :drop_seconds, if set to true, seconds and milliseconds will be trimmed from the result

Produces the UTC string representation of a Time instance

like "2009/11/23 11:11:50.947109 UTC"

Public Instance methods

Callback called when a job is triggered. If the lock cannot be acquired, the job won‘t run (though it‘ll still be scheduled to run again if necessary).

Returns all the scheduled jobs (even those right before re-schedule).

Returns true if the scheduler has acquired the [exclusive] lock and thus may run.

Most of the time, a scheduler is run alone and this method should return true. It is useful in cases where among a group of applications only one of them should run the scheduler. For schedulers that should not run, the method should return false.

Out of the box, rufus-scheduler proposes the :lockfile => ‘path/to/lock/file’ scheduler start option. It makes it easy for schedulers on the same machine to determine which should run (the first to write the lockfile and lock it). It uses "man 2 flock" so it probably won‘t work reliably on distributed file systems.

If one needs to use a special/different locking mechanism, the scheduler accepts :scheduler_lock => lock_object. lock_object only needs to respond to lock and unlock, and both of these methods should be idempotent.

Look at rufus/scheduler/locks.rb for an example.

Returns true if this job is currently scheduled.

Takes extra care to answer true if the job is a repeat job currently firing.

stop(opt=nil)

Alias for shutdown

Lists all the threads associated with this scheduler.

Sister method to lock, is called when the scheduler shuts down.

Lists all the work threads (the ones actually running the scheduled block code)

Accepts a query option, which can be set to:

  • :all (default), returns all the threads that are work threads or are currently running a job
  • :active, returns all threads that are currenly running a job
  • :vacant, returns the threads that are not running a job

If, thanks to :blocking => true, a job is scheduled to monopolize the main scheduler thread, that thread will get returned when :active or :all.

Protected Instance methods

Returns [ job, job_id ]

def free_all_work_threads

  work_threads.each { |t| t.raise(KillSignal) }

end

[Validate]