Class: Stud::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/stud/task.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Task

Returns a new instance of Task.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/stud/task.rb', line 5

def initialize(*args, &block)
  # A queue to receive the result of the block
  # TODO(sissel): Don't use a queue, just store it in an instance variable.
  @queue = Queue.new

  @thread = Thread.new(@queue, *args) do |queue, *args|
    begin
      result = block.call(*args)
      queue << [:return, result]
    rescue => e
      queue << [:exception, e]
    end
  end # thread
end

Class Method Details

.interrupted?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/stud/task.rb', line 36

def self.interrupted?
  Thread.current[:stud_task_interrupted]
end

Instance Method Details

#stop!Object

def wait



32
33
34
# File 'lib/stud/task.rb', line 32

def stop!
  Thread.current[:stud_task_interrupted] = true
end

#waitObject

def initialize



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stud/task.rb', line 20

def wait
  @thread.join
  reason, result = @queue.pop

  if reason == :exception
    #raise StandardError.new(result)
    raise result
  else
    return result
  end
end