Class: Actor::Scheduler

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

Overview

The Actor Scheduler maintains a run queue of actors with outstanding messages who have not yet processed their mailbox. If all actors have processed their mailboxes then the scheduler waits for any outstanding Rev events. If there are no active Rev watchers then the scheduler exits.

Defined Under Namespace

Classes: Mailbox

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



17
18
19
20
21
22
# File 'lib/revactor/scheduler.rb', line 17

def initialize
  @queue = []
  @running = false
  @mailbox = Mailbox.new
  @mailbox.attach Rev::Loop.default
end

Instance Attribute Details

#mailboxObject (readonly)

Returns the value of attribute mailbox.



15
16
17
# File 'lib/revactor/scheduler.rb', line 15

def mailbox
  @mailbox
end

Instance Method Details

#<<(actor) ⇒ Object

Schedule an Actor to be executed, and run the scheduler if it isn’t currently running

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/revactor/scheduler.rb', line 26

def <<(actor)
  raise ArgumentError, "must be an Actor" unless actor.is_a? Actor
  
  @queue << actor unless @queue.last == actor
  
  unless @running
    # Reschedule the current Actor for execution
    @queue << Actor.current

    # Start the scheduler
    Fiber.new { run }.resume
  end
end

#running?Boolean

Is the scheduler running?

Returns:

  • (Boolean)


41
# File 'lib/revactor/scheduler.rb', line 41

def running?; @running; end