Class: Rufus::Scheduler::Job

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

Overview

– job classes ++

Direct Known Subclasses

OneTimeJob, RepeatJob

Defined Under Namespace

Classes: KillSignal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Job

Raises:

  • (ArgumentError)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rufus/scheduler/jobs.rb', line 65

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 = Time.now
  @unscheduled_at = nil
  @last_time = nil

  @locals = {}
  @local_mutex = Mutex.new

  @id = determine_id

  raise(
    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



59
60
61
# File 'lib/rufus/scheduler/jobs.rb', line 59

def callable
  @callable
end

#countObject (readonly)

Returns the value of attribute count



48
49
50
# File 'lib/rufus/scheduler/jobs.rb', line 48

def count
  @count
end

#handlerObject (readonly)

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



63
64
65
# File 'lib/rufus/scheduler/jobs.rb', line 63

def handler
  @handler
end

#idObject (readonly) Also known as: job_id

Returns the value of attribute id



41
42
43
# File 'lib/rufus/scheduler/jobs.rb', line 41

def id
  @id
end

#last_timeObject (readonly)

Returns the value of attribute last_time



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

def last_time
  @last_time
end

#last_work_timeObject (readonly)

Returns the value of attribute last_work_time



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

def last_work_time
  @last_work_time
end

#mean_work_timeObject (readonly)

Returns the value of attribute mean_work_time



50
51
52
# File 'lib/rufus/scheduler/jobs.rb', line 50

def mean_work_time
  @mean_work_time
end

#next_timeObject

next trigger time



54
55
56
# File 'lib/rufus/scheduler/jobs.rb', line 54

def next_time
  @next_time
end

#optsObject (readonly)

Returns the value of attribute opts



42
43
44
# File 'lib/rufus/scheduler/jobs.rb', line 42

def opts
  @opts
end

#originalObject (readonly)

Returns the value of attribute original



43
44
45
# File 'lib/rufus/scheduler/jobs.rb', line 43

def original
  @original
end

#scheduled_atObject (readonly)

Returns the value of attribute scheduled_at



44
45
46
# File 'lib/rufus/scheduler/jobs.rb', line 44

def scheduled_at
  @scheduled_at
end

#tagsObject (readonly)

Returns the value of attribute tags



47
48
49
# File 'lib/rufus/scheduler/jobs.rb', line 47

def tags
  @tags
end

#unscheduled_atObject (readonly)

Returns the value of attribute unscheduled_at



46
47
48
# File 'lib/rufus/scheduler/jobs.rb', line 46

def unscheduled_at
  @unscheduled_at
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)

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

#[]=(key, value) ⇒ Object



167
168
169
170
# File 'lib/rufus/scheduler/jobs.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.



200
201
202
203
# File 'lib/rufus/scheduler/jobs.rb', line 200

def call(do_rescue=false)

  do_call(Time.now, do_rescue)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def key?(key)

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

#keysObject



182
183
184
185
# File 'lib/rufus/scheduler/jobs.rb', line 182

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.rb', line 152

def kill

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

#running?Boolean

Returns:

  • (Boolean)


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

def running?

  threads.any?
end

#scheduled?Boolean

Returns:

  • (Boolean)


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

def scheduled?

  @scheduler.scheduled?(self)
end

#threadsObject



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

def threads

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

#trigger(time) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rufus/scheduler/jobs.rb', line 118

def trigger(time)

  set_next_time(time)

  return if (
    opts[:overlap] == false &&
    running?
  )
  return if (
    callback(:confirm_lock, time) &&
    callback(:on_pre_trigger, time)
  ) == false

  @count += 1

  if opts[:blocking]
    do_trigger(time)
  else
    do_trigger_in_thread(time)
  end
end

#unscheduleObject



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

def unschedule

  @unscheduled_at = Time.now
end