GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of mojombo/god
Description: Ruby process monitor
Homepage: http://god.rubyforge.org
Clone URL: git://github.com/Bertg/god.git
mojombo (author)
Thu Jan 24 15:37:20 -0800 2008
commit  b9a0832c664cff4c60fd1787c798b8a774603242
tree    67a5d8afc291475ea9745df75bedba3b8a439862
parent  f2cffe126c48a78a6950896bb9b5c2bf1692aead
god / lib / god / driver.rb
100644 108 lines (92 sloc) 2.765 kb
1
2
3
4
5
6
7
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
module God
  
  class DriverEvent
    attr_accessor :condition, :at
    
    # Instantiate a new TimerEvent that will be triggered after the specified delay
    # +condition+ is the Condition
    # +delay+ is the number of seconds from now at which to trigger
    #
    # Returns TimerEvent
    def initialize(condition, delay)
      self.condition = condition
      self.at = Time.now + delay
    end
    
    def due?
      Time.now >= self.at
    end
  end # DriverEvent
  
  class Driver
    attr_reader :thread
    
    INTERVAL = 0.25
    
    # Instantiate a new Driver and start the scheduler loop to handle events
    # +task+ is the Task this Driver belongs to
    #
    # Returns Driver
    def initialize(task)
      @task = task
      @events = []
      @ops = Queue.new
      
      @thread = Thread.new do
        loop do
          begin
            if !@ops.empty?
              self.handle_op
            elsif !@events.empty?
              self.handle_event
            else
              sleep INTERVAL
            end
          rescue Exception => e
            message = format("Unhandled exception in driver loop - (%s): %s\n%s",
                             e.class, e.message, e.backtrace.join("\n"))
            applog(nil, :fatal, message)
          end
        end
      end
    end
    
    # Handle the next queued operation that was issued asynchronously
    #
    # Returns nothing
    def handle_op
      command = @ops.pop
      @task.send(command[0], *command[1])
    end
    
    # Handle the next event (poll condition) that is due
    #
    # Returns nothing
    def handle_event
      if @events.first.due?
        event = @events.shift
        @task.handle_poll(event.condition)
      end
      
      # don't sleep if there is a pending event and it is due
      unless @events.first && @events.first.due?
        sleep INTERVAL
      end
    end
    
    # Clear all events for this Driver
    #
    # Returns nothing
    def clear_events
      @events.clear
    end
    
    # Queue an asynchronous message
    # +name+ is the Symbol name of the operation
    # +args+ is an optional Array of arguments
    #
    # Returns nothing
    def message(name, args = [])
      @ops.push([name, args])
    end
    
    # Create and schedule a new DriverEvent
    # +condition+ is the Condition
    # +delay+ is the number of seconds to delay (default: interval defined in condition)
    #
    # Returns nothing
    def schedule(condition, delay = condition.interval)
      applog(nil, :debug, "driver schedule #{condition} in #{delay} seconds")
      
      @events.concat([DriverEvent.new(condition, delay)])
      
      # sort events
      @events.sort! { |x, y| x.at <=> y.at }
    end
  end # Driver
  
end # God