class Rufus::Scheduler::Job

The base class for all types of jobs.

Attributes

block[R]

The block to call when triggering

job_id[R]

The identifier for this job.

last[R]

Last time the job executed (for an {At|In}Job, it will mean ‘not executed’ if nil or when it got executed if set)

( Last time job got triggered (most useful with EveryJob, but can be useful with remaining instances of At/InJob (are they done ?)) )

last_job_thread[R]

Returns the thread instance of the last triggered job. May be null (especially before the first trigger).

params[R]

The job parameters (passed via the schedule method)

scheduler[RW]

A reference to the scheduler owning this job

t[R]

The initial, raw, scheduling info (at / in / every / cron)

Public Class Methods

new(scheduler, t, params, &block) click to toggle source

Instantiating the job.

# File lib/rufus/sc/jobs.rb, line 72
def initialize(scheduler, t, params, &block)

  @scheduler = scheduler
  @t = t
  @params = params
  @block = block || params[:schedulable]

  raise_on_unknown_params

  @running = false
  @paused = false

  raise ArgumentError.new(
    'no block or :schedulable passed, nothing to schedule'
  ) unless @block

  @params[:tags] = Array(@params[:tags])

  @job_id = params[:job_id] || "#{self.class.name}_#{self.object_id.to_s}"

  determine_at
end

Protected Class Methods

known_params(*args) click to toggle source
# File lib/rufus/sc/jobs.rb, line 255
def self.known_params(*args)

  define_method :known_params do
    super() + args
  end
end

Public Instance Methods

pause() click to toggle source

Pauses this job (sets the paused flag to true).

Note that it will not pause the execution of a block currently ‘running’. Future triggering of the job will not occur until resume is called.

Note too that, during the pause time, the schedule kept the same. Calling resume will not force old triggers in.

# File lib/rufus/sc/jobs.rb, line 125
def pause

  @paused = true
end
paused?() click to toggle source

Returns true if this job is paused, false else.

A paused job is still scheduled, but does not trigger.

Note : paused? is not related to running?

# File lib/rufus/sc/jobs.rb, line 112
def paused?

  @paused
end
resume() click to toggle source

Resumes this job (sets the paused flag to false).

This job will trigger again.

# File lib/rufus/sc/jobs.rb, line 134
def resume

  @paused = false
end
running() click to toggle source

Returns true if this job is currently running (in the middle of trigger)

Note : paused? is not related to running?

# File lib/rufus/sc/jobs.rb, line 99
def running

  @running
end
Also aliased as: running?
running?() click to toggle source
Alias for: running
schedule_info() click to toggle source

Generally returns the string/float/integer used to schedule the job (seconds, time string, date string)

# File lib/rufus/sc/jobs.rb, line 157
def schedule_info

  @t
end
tags() click to toggle source

Returns the list of tags attached to the job.

# File lib/rufus/sc/jobs.rb, line 141
def tags

  @params[:tags]
end
tags=(tags) click to toggle source

Sets the list of tags attached to the job (Usually they are set via the schedule every/at/in/cron method).

# File lib/rufus/sc/jobs.rb, line 149
def tags=(tags)

  @params[:tags] = Array(tags)
end
trigger(t=Time.now) click to toggle source

Triggers the job.

# File lib/rufus/sc/jobs.rb, line 164
def trigger(t=Time.now)

  return if @paused

  @last = t
  job_thread = nil
  to_job = nil

  return if @running and (params[:allow_overlapping] == false)

  @running = true

  @scheduler.send(:trigger_job, @params) do
    #
    # Note that #trigger_job is protected, hence the #send
    # (Only jobs know about this method of the scheduler)

    job_thread = Thread.current

    job_thread[
      "rufus_scheduler__trigger_thread__#{@scheduler.object_id}"
    ] = self

    @last_job_thread = job_thread

    begin

      trigger_block

      job_thread[
        "rufus_scheduler__trigger_thread__#{@scheduler.object_id}"
      ] = nil

      job_thread = nil

      to_job.unschedule if to_job

    rescue Exception => e

      @scheduler.do_handle_exception(self, e)
    end

    @running = false
  end

  # note that add_job and add_cron_job ensured that :blocking is
  # not used along :timeout

  if to = @params[:timeout]

    to_job = @scheduler.in(to, :parent => self, :tags => 'timeout') do

      # at this point, @job_thread might be set

      if job_thread && job_thread.alive?
        job_thread.raise(Rufus::Scheduler::TimeOutError)
      end
    end
  end
end
trigger_block() click to toggle source

Simply encapsulating the blockcall/trigger operation, for easy override.

# File lib/rufus/sc/jobs.rb, line 228
def trigger_block

  @block.respond_to?(:call) ?
    @block.call(self) : @block.trigger(@params.merge(:job => self))
end
unschedule() click to toggle source

Unschedules this job.

# File lib/rufus/sc/jobs.rb, line 236
def unschedule

  @scheduler.unschedule(self.job_id)
end

Protected Instance Methods

known_params() click to toggle source
# File lib/rufus/sc/jobs.rb, line 243
def known_params

  [ :allow_overlapping,
    :blocking,
    :discard_past,
    :job_id,
    :mutex,
    :schedulable,
    :tags,
    :timeout ]
end
raise_on_unknown_params() click to toggle source
# File lib/rufus/sc/jobs.rb, line 262
def raise_on_unknown_params

  rem = @params.keys - known_params

  raise(
    ArgumentError,
    "unknown option#{rem.size > 1 ? 's' : '' }: " +
    "#{rem.map(&:inspect).join(', ')}",
    caller[3..-1]
  ) if rem.any?
end