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)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

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

#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

#pauseObject

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.



125
126
127
128
# File 'lib/rufus/sc/jobs.rb', line 125

def pause

  @paused = true
end

#paused?Boolean

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?

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/rufus/sc/jobs.rb', line 112

def paused?

  @paused
end

#resumeObject

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

This job will trigger again.



134
135
136
137
# File 'lib/rufus/sc/jobs.rb', line 134

def resume

  @paused = false
end

#runningObject Also known as: running?

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

Note : paused? is not related to running?



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

def running

  @running
end

#schedule_infoObject

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



157
158
159
160
# File 'lib/rufus/sc/jobs.rb', line 157

def schedule_info

  @t
end

#tagsObject

Returns the list of tags attached to the job.



141
142
143
144
# File 'lib/rufus/sc/jobs.rb', line 141

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).



149
150
151
152
# File 'lib/rufus/sc/jobs.rb', line 149

def tags=(tags)

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

#trigger(t = Time.now) ⇒ Object

Triggers the job.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# 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_blockObject

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



228
229
230
231
232
# 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

#unscheduleObject

Unschedules this job.



236
237
238
239
# File 'lib/rufus/sc/jobs.rb', line 236

def unschedule

  @scheduler.unschedule(self.job_id)
end