Class: Rubish::Job::ThreadJob

Inherits:
Rubish::Job show all
Defined in:
lib/rubish/job.rb

Overview

carry out some computation in a thread.

Instance Attribute Summary collapse

Attributes inherited from Rubish::Job

#job_control, #result

Instance Method Summary collapse

Methods inherited from Rubish::Job

#__finish, #__start, #done?

Constructor Details

#initialize(&block) ⇒ ThreadJob

Returns a new instance of ThreadJob.



64
65
66
67
68
69
70
# File 'lib/rubish/job.rb', line 64

def initialize(&block)
  # run block in a thread
  @thread = ::Thread.new {
    block.call
  }
  __start
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



63
64
65
# File 'lib/rubish/job.rb', line 63

def thread
  @thread
end

Instance Method Details

#stopObject



85
86
87
88
# File 'lib/rubish/job.rb', line 85

def stop
  @thread.kill
  wait
end

#waitObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubish/job.rb', line 72

def wait
  # wait thread to completeggg64
  begin
    @thread.join
    @result = @thread.value
    return self.result
  rescue => e
    raise Rubish::Job::Failure.new(self,e)
  ensure
    __finish
  end
end