Class: Punctual

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePunctual

Returns a new instance of Punctual.



6
7
8
9
10
# File 'lib/punctual.rb', line 6

def initialize 
  @sleep_time = 1
  @clock_initialized = false
  @jobs = Array.new
end

Instance Attribute Details

#jobsObject (readonly)

Returns the value of attribute jobs.



4
5
6
# File 'lib/punctual.rb', line 4

def jobs
  @jobs
end

Instance Method Details

#schedule(name, cron, &block) ⇒ Object

register new job



13
14
15
16
17
# File 'lib/punctual.rb', line 13

def schedule(name, cron, &block)
  job = Job.new(name, cron, block)
  puts "Scheduling block: #{name}, #{cron}"
  @jobs.push(job)  
end

#startObject

starts the scheduler



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/punctual.rb', line 20

def start()
  t = Thread.new do
    while true do
      time = Time.new
      if (time.sec == 0)
        @sleep_time = 60
 @clock_initialized = true
      end
      @jobs.each do |job|
        if @clock_initialized == true && job.should_execute? 
          job.execute
        end
      end
      sleep @sleep_time
    end
  end
  t.join
end