class Procrastinate::Server

Public Class Methods

new() click to toggle source
# File lib/procrastinate/server.rb, line 3
def initialize
  @manager  = Procrastinate::ProcessManager.new
  @state    = :new
end

Public Instance Methods

shutdown() click to toggle source
# File lib/procrastinate/server.rb, line 19
def shutdown
  fail "For shutdown, server must be running." unless @state == :running

  @state = :shutdown
  @manager.wakeup
  
  @thread.join if @thread
  
  @thread = nil
  @state  = :new
end
start(n, activity_check_interval=nil, &block) click to toggle source
# File lib/procrastinate/server.rb, line 8
def start(n, activity_check_interval=nil, &block)
  fail "Already running server." unless @state == :new
  
  @block    = block
  @strategy = Procrastinate::SpawnStrategy::Throttled.new(n)
  @state    = :running
  @check_interval = activity_check_interval
  
  start_thread
end

Private Instance Methods

control_thread_main() click to toggle source

@note This method runs in the control thread only.

# File lib/procrastinate/server.rb, line 51
def control_thread_main
  # Start managers work
  @manager.setup

  # Loop until someone requests a shutdown.
  loop do
    spawn_new_workers

    @manager.step
    
    break if @state == :shutdown
  end
  
  @manager.kill_processes
  @manager.teardown
rescue => ex
  # Sometimes exceptions vanish silently. This will avoid that, even though
  # they should abort the whole process.

  warn "Exception #{ex.inspect} caught."
  ex.backtrace.first(5).each do |line|
    warn line
  end

  raise
end
spawn_new_workers() click to toggle source

@note This method runs in the control thread only.

# File lib/procrastinate/server.rb, line 38
def spawn_new_workers
  while @strategy.should_spawn?
    task = Procrastinate::Task::Callable.new(@block)
    
    @strategy.notify_spawn
    @manager.create_process(task) do
      @strategy.notify_dead
    end
  end
end
start_thread() click to toggle source
# File lib/procrastinate/server.rb, line 32
def start_thread
  @thread = Thread.start(&method(:control_thread_main))
end