Class: Rubish::JobControl

Inherits:
Object
  • Object
show all
Defined in:
lib/rubish/job_control.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJobControl

Returns a new instance of JobControl.



17
18
19
20
# File 'lib/rubish/job_control.rb', line 17

def initialize
  @mutex = Mutex.new
  @jobs = { }
end

Class Method Details

.currentObject



12
13
14
# File 'lib/rubish/job_control.rb', line 12

def current
  Rubish::Context.current.job_control
end

Instance Method Details

#jobsObject



22
23
24
# File 'lib/rubish/job_control.rb', line 22

def jobs
  @jobs.values
end

#remove(job) ⇒ Object

Raises:



34
35
36
37
38
39
40
# File 'lib/rubish/job_control.rb', line 34

def remove(job)
  raise "expects a Rubish::JobControl::Job" unless job.is_a?(Rubish::Job)
  raise Rubish::Error.new("Job not found: #{job}") unless @jobs.include?(job.object_id)
  @mutex.synchronize {
    @jobs.delete(job.object_id)
  }
end

#submit(job) ⇒ Object

need to synchronize access to the jobs hash



27
28
29
30
31
32
# File 'lib/rubish/job_control.rb', line 27

def submit(job)
  raise "expects a Rubish::JobControl::Job" unless job.is_a?(Rubish::Job)
  @mutex.synchronize {
    @jobs[job.object_id] = job
  }
end

#wait(*jobs) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubish/job_control.rb', line 42

def wait(*jobs)
  rss = jobs.map do |job|
    job.wait
    if block_given?
      yield(job)
    else
      job
    end
  end
  return *rss
end

#waitall(&block) ⇒ Object

TODO handle interrupt



55
56
57
# File 'lib/rubish/job_control.rb', line 55

def waitall(&block)
  wait(*@jobs.values,&block)
end