Class: Rufus::Scheduler::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/rufus/sc/jobs.rb

Overview

The base class for all types of jobs.

Direct Known Subclasses

CronJob, SimpleJob

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheduler, t, params, &block) ⇒ Job

Instantiating the job.

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rufus/sc/jobs.rb', line 74

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

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

  @running = false
  @allow_overlapping = true
  if !params[:allow_overlapping].nil?
    @allow_overlapping = params[:allow_overlapping]
  end

  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

Instance Attribute Details

#blockObject (readonly)

The block to call when triggering



53
54
55
# File 'lib/rufus/sc/jobs.rb', line 53

def block
  @block
end

#job_idObject (readonly)

The identifier for this job.



68
69
70
# File 'lib/rufus/sc/jobs.rb', line 68

def job_id
  @job_id
end

#lastObject (readonly)

Last time the job executed (for an At|InJob, 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 ?)) )



64
65
66
# File 'lib/rufus/sc/jobs.rb', line 64

def last
  @last
end

#last_job_threadObject (readonly)

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



45
46
47
# File 'lib/rufus/sc/jobs.rb', line 45

def last_job_thread
  @last_job_thread
end

#paramsObject (readonly)

The job parameters (passed via the schedule method)



49
50
51
# File 'lib/rufus/sc/jobs.rb', line 49

def params
  @params
end

#runningObject

Returns the value of attribute running.



70
71
72
# File 'lib/rufus/sc/jobs.rb', line 70

def running
  @running
end

#schedulerObject

A reference to the scheduler owning this job



36
37
38
# File 'lib/rufus/sc/jobs.rb', line 36

def scheduler
  @scheduler
end

#tObject (readonly)

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



40
41
42
# File 'lib/rufus/sc/jobs.rb', line 40

def t
  @t
end

Instance Method Details

#schedule_infoObject

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



116
117
118
119
# File 'lib/rufus/sc/jobs.rb', line 116

def schedule_info

  @t
end

#tagsObject

Returns the list of tags attached to the job.



100
101
102
103
# File 'lib/rufus/sc/jobs.rb', line 100

def tags

  @params[:tags]
end

#tags=(tags) ⇒ Object

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



108
109
110
111
# File 'lib/rufus/sc/jobs.rb', line 108

def tags= (tags)

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

#trigger(t = Time.now) ⇒ Object

Triggers the job.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rufus/sc/jobs.rb', line 123

def trigger (t=Time.now)

  @last = t
  job_thread = nil
  to_job = nil

  return if @running && !@allow_overlapping

  @running = true
  @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
    job_thread[
      "rufus_scheduler__trigger_thread__#{@scheduler.object_id}"
    ] = true
    @last_job_thread = job_thread

    begin

      trigger_block

      job_thread = nil
      to_job.unschedule if to_job

      @running = false

    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]

    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_blockObject

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



178
179
180
181
182
# File 'lib/rufus/sc/jobs.rb', line 178

def trigger_block

  @block.respond_to?(:call) ?
    @block.call(self) : @block.trigger(@params.merge(:job => self))
end

#unscheduleObject

Unschedules this job.



186
187
188
189
# File 'lib/rufus/sc/jobs.rb', line 186

def unschedule

  @scheduler.unschedule(self.job_id)
end