Class: Futuroscope::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/futuroscope/worker.rb

Overview

A futuroscope worker takes care of resolving a future’s value. It works together with a Pool.

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ Worker

Public: Initializes a new Worker.

pool - The worker Pool it belongs to.



8
9
10
# File 'lib/futuroscope/worker.rb', line 8

def initialize(pool)
  @pool = pool
end

Instance Method Details

#runObject

Runs the worker. It keeps asking the Pool for a new job. If the pool decides there’s no job use it now or in the future, it will die and the Pool will be notified. Otherwise, it will be given a new job or blocked until there’s a new future available to process.



17
18
19
20
21
22
23
24
# File 'lib/futuroscope/worker.rb', line 17

def run
  @thread = Thread.new do
    while(future = @pool.pop) do
      future.run_future
    end
    die
  end
end

#stopObject

Public: Stops this worker.



27
28
29
30
# File 'lib/futuroscope/worker.rb', line 27

def stop
  @thread.kill
  die
end