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 73
def initialize (scheduler, t, params, &block)

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

  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

Public Instance Methods

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 109
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 93
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 101
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 116
def trigger (t=Time.now)

  @last = t
  job_thread = nil

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

    job_thread = Thread.current
    @last_job_thread = job_thread

    begin

      trigger_block

      job_thread = nil

    rescue Exception => e

      @scheduler.handle_exception(self, e)
    end
  end

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

  if to = @params[:timeout]

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

      # at this point, @job_thread might be set

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

Simply encapsulating the block#call/trigger operation, for easy override.

# File lib/rufus/sc/jobs.rb, line 159
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 167
def unschedule

  @scheduler.unschedule(self.job_id)
end