Class: Rufus::Scheduler::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/rufus/scheduler/jobs_core.rb

Direct Known Subclasses

OneTimeJob, RepeatJob

Defined Under Namespace

Classes: KillSignal

Constant Summary collapse

EoTime =
::EtOrbi::EoTime

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheduler, original, opts, block) ⇒ Job

Returns a new instance of Job.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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/scheduler/jobs_core.rb', line 46

def initialize(scheduler, original, opts, block)

  @scheduler = scheduler
  @original = original
  @opts = opts

  @handler = block

  @callable =
    if block.respond_to?(:arity)
      block
    elsif block.respond_to?(:call)
      block.method(:call)
    elsif block.is_a?(Class)
      @handler = block.new
      @handler.method(:call) rescue nil
    else
      nil
    end

  @scheduled_at = EoTime.now
  @unscheduled_at = nil
  @last_time = nil

  @discard_past = opts[:discard_past]

  @locals = opts[:locals] || opts[:l] || {}
  @local_mutex = Mutex.new

  @id = determine_id
  @name = opts[:name] || opts[:n]

  fail(
    ArgumentError, 'missing block or callable to schedule', caller[2..-1]
  ) unless @callable

  @tags = Array(opts[:tag] || opts[:tags]).collect { |t| t.to_s }

  @count = 0
  @last_work_time = 0.0
  @mean_work_time = 0.0

  # tidy up options

  if @opts[:allow_overlap] == false || @opts[:allow_overlapping] == false
    @opts[:overlap] = false
  end
  if m = @opts[:mutex]
    @opts[:mutex] = Array(m)
  end
end

Instance Attribute Details

#callableObject (readonly)

anything with a #call(job[, timet]) method, what gets actually triggered



36
37
38
# File 'lib/rufus/scheduler/jobs_core.rb', line 36

def callable
  @callable
end

#countObject (readonly)

Returns the value of attribute count.



19
20
21
# File 'lib/rufus/scheduler/jobs_core.rb', line 19

def count
  @count
end

#handlerObject (readonly)

a reference to the instance whose call method is the @callable



40
41
42
# File 'lib/rufus/scheduler/jobs_core.rb', line 40

def handler
  @handler
end

#idObject (readonly) Also known as: job_id

Returns the value of attribute id.



11
12
13
# File 'lib/rufus/scheduler/jobs_core.rb', line 11

def id
  @id
end

#last_timeObject (readonly)

Returns the value of attribute last_time.



15
16
17
# File 'lib/rufus/scheduler/jobs_core.rb', line 15

def last_time
  @last_time
end

#last_work_timeObject (readonly)

Returns the value of attribute last_work_time.



20
21
22
# File 'lib/rufus/scheduler/jobs_core.rb', line 20

def last_work_time
  @last_work_time
end

#localsObject (readonly)

Returns the value of attribute locals.



18
19
20
# File 'lib/rufus/scheduler/jobs_core.rb', line 18

def locals
  @locals
end

#mean_work_timeObject (readonly)

Returns the value of attribute mean_work_time.



21
22
23
# File 'lib/rufus/scheduler/jobs_core.rb', line 21

def mean_work_time
  @mean_work_time
end

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/rufus/scheduler/jobs_core.rb', line 23

def name
  @name
end

#next_timeObject

next trigger time



27
28
29
# File 'lib/rufus/scheduler/jobs_core.rb', line 27

def next_time
  @next_time
end

#optsObject (readonly)

Returns the value of attribute opts.



12
13
14
# File 'lib/rufus/scheduler/jobs_core.rb', line 12

def opts
  @opts
end

#originalObject (readonly)

Returns the value of attribute original.



13
14
15
# File 'lib/rufus/scheduler/jobs_core.rb', line 13

def original
  @original
end

#previous_timeObject

previous “next trigger time”



31
32
33
# File 'lib/rufus/scheduler/jobs_core.rb', line 31

def previous_time
  @previous_time
end

#scheduled_atObject (readonly)

Returns the value of attribute scheduled_at.



14
15
16
# File 'lib/rufus/scheduler/jobs_core.rb', line 14

def scheduled_at
  @scheduled_at
end

#tagsObject (readonly)

Returns the value of attribute tags.



17
18
19
# File 'lib/rufus/scheduler/jobs_core.rb', line 17

def tags
  @tags
end

#unscheduled_atObject (readonly)

Returns the value of attribute unscheduled_at.



16
17
18
# File 'lib/rufus/scheduler/jobs_core.rb', line 16

def unscheduled_at
  @unscheduled_at
end

Instance Method Details

#[](key) ⇒ Object



172
173
174
175
# File 'lib/rufus/scheduler/jobs_core.rb', line 172

def [](key)

  @local_mutex.synchronize { @locals[key] }
end

#[]=(key, value) ⇒ Object



167
168
169
170
# File 'lib/rufus/scheduler/jobs_core.rb', line 167

def []=(key, value)

  @local_mutex.synchronize { @locals[key] = value }
end

#call(do_rescue = false) ⇒ Object

Calls the callable (usually a block) wrapped in this Job instance.

Warning: error rescueing is the responsibity of the caller.



205
206
207
208
# File 'lib/rufus/scheduler/jobs_core.rb', line 205

def call(do_rescue=false)

  do_call(EoTime.now, do_rescue)
end

#check_frequencyObject

Will fail with an ArgumentError if the job frequency is higher than the scheduler frequency.



117
118
119
120
# File 'lib/rufus/scheduler/jobs_core.rb', line 117

def check_frequency

  # this parent implementation never fails
end

#entriesObject



185
# File 'lib/rufus/scheduler/jobs_core.rb', line 185

def entries; @local_mutex.synchronize { @locals.entries }; end

#has_key?(key) ⇒ Boolean Also known as: key?

Returns:

  • (Boolean)


177
178
179
180
# File 'lib/rufus/scheduler/jobs_core.rb', line 177

def has_key?(key)

  @local_mutex.synchronize { @locals.has_key?(key) }
end

#keysObject



183
# File 'lib/rufus/scheduler/jobs_core.rb', line 183

def keys; @local_mutex.synchronize { @locals.keys }; end

#killObject

Kills all the threads this Job currently has going on.



152
153
154
155
# File 'lib/rufus/scheduler/jobs_core.rb', line 152

def kill

  threads.each { |t| t.raise(KillSignal) }
end

#next_times(count) ⇒ Object

might be necessary at some point



196
197
198
199
# File 'lib/rufus/scheduler/jobs_core.rb', line 196

def next_times(count)

  next_time ? [ next_time ] : []
end

#past?Boolean

Returns true if the job is scheduled in the past. Used for OneTimeJob when discard_past == true

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/rufus/scheduler/jobs_core.rb', line 109

def past?

  false # by default
end

#resume_discard_past=(v) ⇒ Object

Default, core, implementation has no effect. Repeat jobs do override it.



44
# File 'lib/rufus/scheduler/jobs_core.rb', line 44

def resume_discard_past=(v); end

#running?Boolean

Returns:

  • (Boolean)


157
158
159
160
# File 'lib/rufus/scheduler/jobs_core.rb', line 157

def running?

  threads.any?
end

#scheduled?Boolean

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/rufus/scheduler/jobs_core.rb', line 162

def scheduled?

  @scheduler.scheduled?(self)
end

#source_locationObject Also known as: location



100
101
102
103
# File 'lib/rufus/scheduler/jobs_core.rb', line 100

def source_location

  @callable.source_location
end

#threadsObject



145
146
147
148
# File 'lib/rufus/scheduler/jobs_core.rb', line 145

def threads

  Thread.list.select { |t| t[:rufus_scheduler_job] == self }
end

#trigger(time) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/rufus/scheduler/jobs_core.rb', line 122

def trigger(time)

  @previous_time = @next_time
  set_next_time(time)

  do_trigger(time)
end

#trigger_off_schedule(time = EoTime.now) ⇒ Object

Trigger the job right now, off of its schedule.

Done in collaboration with Piavka in github.com/jmettraux/rufus-scheduler/issues/214



135
136
137
138
# File 'lib/rufus/scheduler/jobs_core.rb', line 135

def trigger_off_schedule(time=EoTime.now)

  do_trigger(time)
end

#unscheduleObject



140
141
142
143
# File 'lib/rufus/scheduler/jobs_core.rb', line 140

def unschedule

  @unscheduled_at = EoTime.now
end

#valuesObject



184
# File 'lib/rufus/scheduler/jobs_core.rb', line 184

def values; @local_mutex.synchronize { @locals.values }; end