Class: V::Worker

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

Defined Under Namespace

Classes: Group

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorker

Returns a new instance of Worker.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/v/worker.rb', line 32

def initialize
  @queue = Queue.new

  super do
    while continue?
      operation, environment, thread, future = @queue.pop

      begin
        future.value = operation.call environment

      rescue Exception => e
        thread.raise e

      end if future
    end
  end

  if continue?
    Group.instance.add self
  else
    raise V::ECLOSED
  end
end

Class Method Details

.new(git_dir) ⇒ Object



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

def self.new(git_dir)
  @instances[git_dir] ||= super()
end

Instance Method Details

#continue?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/v/worker.rb', line 68

def continue?
  not Group.instance.enclosed?
end

#enq(operation, environment) ⇒ Object

Raises:



56
57
58
59
60
61
62
63
# File 'lib/v/worker.rb', line 56

def enq(operation, environment)
  raise V::ECLOSED unless continue?

  thread, future = Thread.current, Future.new(self)
  @queue.enq [operation, environment, thread, future]

  future
end

#stop!Object



65
66
67
# File 'lib/v/worker.rb', line 65

def stop!
  @queue.push nil
end