Class: God::Driver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task) ⇒ Driver

Instantiate a new Driver and start the scheduler loop to handle events task is the Task this Driver belongs to

Returns Driver



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/god/driver.rb', line 158

def initialize(task)
  @task = task
  @events = God::DriverEventQueue.new
  
  @thread = Thread.new do
    loop do
      begin
        @events.pop.handle_event
      rescue ThreadError => e
        # queue is empty
        break
      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

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



152
153
154
# File 'lib/god/driver.rb', line 152

def thread
  @thread
end

Instance Method Details

#clear_eventsObject

Clear all events for this Driver

Returns nothing



181
182
183
# File 'lib/god/driver.rb', line 181

def clear_events
  @events.clear
end

#message(name, args = []) ⇒ Object

Queue an asynchronous message name is the Symbol name of the operation args is an optional Array of arguments

Returns nothing



190
191
192
# File 'lib/god/driver.rb', line 190

def message(name, args = [])
  @events.push(DriverOperation.new(@task, name, args))
end

#schedule(condition, delay = condition.interval) ⇒ Object

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



199
200
201
202
203
# File 'lib/god/driver.rb', line 199

def schedule(condition, delay = condition.interval)
  applog(nil, :debug, "driver schedule #{condition} in #{delay} seconds")
  
  @events.push(DriverEvent.new(delay, @task, condition))
end