class Rufus::Scheduler::Job


The base class for all types of jobs.

def self.known_params(*args)

def self.known_params(*args)
  define_method :known_params do
    super() + args
  end
end

def initialize(scheduler, t, params, &block)


Instantiating the job.
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

def known_params

def known_params
  [ :allow_overlapping,
    :blocking,
    :discard_past,
    :job_id,
    :mutex,
    :schedulable,
    :tags,
    :timeout ]
end

def pause


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

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

Pauses this job (sets the paused flag to true).
def pause
  @paused = true
end

def paused?


Note : paused? is not related to running?

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

Returns true if this job is paused, false else.
def paused?
  @paused
end

def raise_on_unknown_params

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

def resume


This job will trigger again.

Resumes this job (sets the paused flag to false).
def resume
  @paused = false
end

def running


Note : paused? is not related to running?

Returns true if this job is currently running (in the middle of #trigger)
def running
  @running
end

def schedule_info


(seconds, time string, date string)
Generally returns the string/float/integer used to schedule the job
def schedule_info
  @t
end

def tags


Returns the list of tags attached to the job.
def tags
  @params[:tags]
end

def tags=(tags)


via the schedule every/at/in/cron method).
Sets the list of tags attached to the job (Usually they are set
def tags=(tags)
  @params[:tags] = Array(tags)
end

def trigger(t=Time.now)


Triggers the job.
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 (@scheduler.options[:exception] || 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

def trigger_block


override.
Simply encapsulating the block#call/trigger operation, for easy
def trigger_block
  @block.respond_to?(:call) ?
    @block.call(self) : @block.trigger(@params.merge(:job => self))
end

def unschedule


Unschedules this job.
def unschedule
  @scheduler.unschedule(self.job_id)
end