Class: God::Driver

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

Overview

The Driver class is responsible for scheduling all of the events for a given Task.

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 - The Task this Driver belongs to.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/god/driver.rb', line 172

def initialize(task)
  @task = task
  @events = God::DriverEventQueue.new

  @thread = Thread.new do
    loop do
      @events.pop.handle_event
    rescue ThreadError
      # queue is empty
      break
    rescue Object => e
      message = format("Unhandled exception in driver loop - (%{class}): %{message}\n%{backtrace}",
                       class: e.class, message: e.message, backtrace: e.backtrace.join("\n"))
      applog(nil, :fatal, message)
    end
  end
end

Instance Attribute Details

#threadObject (readonly)

The Thread running the driver loop.



167
168
169
# File 'lib/god/driver.rb', line 167

def thread
  @thread
end

Instance Method Details

#clear_eventsObject

Clear all events for this Driver.

Returns nothing.



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

def clear_events
  @events.clear
end

#in_driver_context?Boolean

Check if we’re in the driver context.

Returns true if in driver thread, false if not.

Returns:

  • (Boolean)


193
194
195
# File 'lib/god/driver.rb', line 193

def in_driver_context?
  Thread.current == @thread
end

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

Queue an asynchronous message.

name - The Symbol name of the operation. args - An optional Array of arguments.

Returns nothing.



217
218
219
# File 'lib/god/driver.rb', line 217

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 - The Condition. delay - The Numeric number of seconds to delay (default: interval

defined in condition).

Returns nothing.



228
229
230
231
232
# File 'lib/god/driver.rb', line 228

def schedule(condition, delay = condition.interval)
  applog(nil, :debug, "driver schedule #{condition} in #{delay} seconds")

  @events.push(DriverEvent.new(delay, @task, condition))
end

#shutdownObject

Shutdown the DriverEventQueue threads.

Returns nothing.



207
208
209
# File 'lib/god/driver.rb', line 207

def shutdown
  @events.shutdown
end