Class: Scheddy::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/scheddy/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cronObject (readonly)

Returns the value of attribute cron.



3
4
5
# File 'lib/scheddy/task.rb', line 3

def cron
  @cron
end

#delayObject (readonly)

Returns the value of attribute delay.



3
4
5
# File 'lib/scheddy/task.rb', line 3

def delay
  @delay
end

#intervalObject (readonly)

Returns the value of attribute interval.



3
4
5
# File 'lib/scheddy/task.rb', line 3

def interval
  @interval
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/scheddy/task.rb', line 3

def name
  @name
end

#next_cycleObject



47
48
49
50
# File 'lib/scheddy/task.rb', line 47

def next_cycle
  initial_cycle! if @next_cycle == :initial
  @next_cycle
end

#tagObject (readonly)

Returns the value of attribute tag.



3
4
5
# File 'lib/scheddy/task.rb', line 3

def tag
  @tag
end

#taskObject (readonly)

Returns the value of attribute task.



3
4
5
# File 'lib/scheddy/task.rb', line 3

def task
  @task
end

#track_runsObject

Returns the value of attribute track_runs.



3
4
5
# File 'lib/scheddy/task.rb', line 3

def track_runs
  @track_runs
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/scheddy/task.rb', line 3

def type
  @type
end

Instance Method Details

#finish_before(grace: 0.1.seconds) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/scheddy/task.rb', line 143

def finish_before(grace: 0.1.seconds)
  case type
  when :interval
    Time.current + interval - grace
  when :cron
    cron.next_time.to_utc_time - grace
  end
end

#inspectObject



77
78
79
80
# File 'lib/scheddy/task.rb', line 77

def inspect
  attrs = to_h.map{|k,v| "#{k}: #{v.inspect}"}
  %Q{#<#{self.class} #{attrs.join(', ')}>}
end

#killObject



39
40
41
# File 'lib/scheddy/task.rb', line 39

def kill
  thread&.kill
end

#perform(scheduler, now: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scheddy/task.rb', line 8

def perform(scheduler, now: false)
  return next_cycle if Time.current < next_cycle && !now
  record_this_run
  if running?
    logger.error "Scheddy task '#{name}' already running; skipping this cycle"
    return next_cycle!
  end
  context = Context.new(scheduler, self)
  self.thread =
    Thread.new do
      logger.tagged tag do
        Rails.application.reloader.wrap do
          if context.finish_before < Time.current
            logger.info "Rails dev-mode reloader locked for entire task interval; skipping this run"
          else
            task.call(*[context].take(task.arity.abs))
          end
        rescue Exception => e
          Scheddy.handle_error(e, self)
        end
      end
    ensure
      self.thread = nil
    end
  next_cycle!
rescue Exception => e
  logger.error "Scheddy: error scheduling task '#{name}'; retrying in 5 seconds"
  Scheddy.handle_error(e, self)
  return self.next_cycle = 5.seconds.from_now
end

#resetObject



52
53
54
55
# File 'lib/scheddy/task.rb', line 52

def reset
  self.next_cycle = :initial
  @task_history = nil
end

#running?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/scheddy/task.rb', line 43

def running?
  !!thread
end

#to_hObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/scheddy/task.rb', line 58

def to_h
  attrs = {
    name:       name,
    next_cycle: next_cycle.utc,
    type:       type,
    tag:        tag,
    task:       task,
    track_runs: track_runs,
  }
  case type
  when :interval
    attrs[:initial_delay] = ActiveSupport::Duration.build(delay) unless track_runs && last_run
    attrs[:interval] = ActiveSupport::Duration.build(interval)
  when :cron
    attrs[:cron] = cron.original
  end
  attrs.to_a.sort_by!{_1.to_s}.to_h
end