Class: Forever::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/forever/job.rb

Instance Method Summary collapse

Constructor Details

#initialize(period, options, &block) ⇒ Job

Returns a new instance of Job


4
5
6
7
8
9
# File 'lib/forever/job.rb', line 4

def initialize(period, options, &block)
  @period = period
  @at     = options[:at] ? parse_at(*options[:at]) : []
  @pid    = File.join(options[:dir], "/tmp/#{object_id}.job")
  @block  = block
end

Instance Method Details

#callObject


11
12
13
# File 'lib/forever/job.rb', line 11

def call
  @block.call
end

#lastObject


27
28
29
30
31
# File 'lib/forever/job.rb', line 27

def last
  File.mtime(@pid)
rescue Errno::ENOENT
  0
end

#run!Object


15
16
17
# File 'lib/forever/job.rb', line 15

def run!
  File.open(@pid, 'w') { |f| f.write('running') }
end

#running?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/forever/job.rb', line 23

def running?
  File.exist?(@pid) && File.read(@pid) == 'running'
end

#stop!Object


19
20
21
# File 'lib/forever/job.rb', line 19

def stop!
  File.open(@pid, 'w') { |f| f.write('idle') }
end

#time?(t) ⇒ Boolean

Returns:

  • (Boolean)

33
34
35
36
37
# File 'lib/forever/job.rb', line 33

def time?(t)
  elapsed_ready = (t - last).to_i >= @period
  time_ready = @at.empty? || @at.any? { |at| (at[0].empty? || t.hour == at[0].to_i) && (at[1].empty? || t.min == at[1].to_i) }
  !running? && elapsed_ready && time_ready
end