Class: Forever::Job
- Inherits:
-
Object
- Object
- Forever::Job
- Defined in:
- lib/forever/job.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(period, options, &block) ⇒ Job
constructor
A new instance of Job.
- #last ⇒ Object
- #run! ⇒ Object
- #running? ⇒ Boolean
- #stop! ⇒ Object
- #time?(t) ⇒ Boolean
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, , &block) @period = period @at = [:at] ? parse_at(*[:at]) : [] @pid = File.join([:dir], "/tmp/#{object_id}.job") @block = block end |
Instance Method Details
#call ⇒ Object
11 12 13 |
# File 'lib/forever/job.rb', line 11 def call @block.call end |
#last ⇒ Object
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
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
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 |