Class: Sequel::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db = nil) ⇒ Worker

Returns a new instance of Worker.



10
11
12
13
14
15
16
17
# File 'lib/sequel/worker.rb', line 10

def initialize(db = nil)
  @queue = Queue.new
  @errors = []
  t = self
  t.abort_on_exception = true
  @transaction = !db.nil?
  db ? super {db.transaction {t.work}} : super {t.work}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/sequel/worker.rb', line 7

def queue
  @queue
end

Instance Method Details

#async(proc = nil, &block) ⇒ Object Also known as: add, <<



30
31
32
33
# File 'lib/sequel/worker.rb', line 30

def async(proc = nil, &block)
  @queue << (proc || block)
  self
end

#busy?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/sequel/worker.rb', line 26

def busy?
  @cur || !@queue.empty?
end

#joinObject



37
38
39
40
41
42
43
# File 'lib/sequel/worker.rb', line 37

def join
  while busy?
    sleep 0.1
  end
  self.raise Error::WorkerStop
  super
end

#workObject



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

def work
  loop {next_job}
rescue Sequel::Error::WorkerStop # signals the worker thread to stop
ensure
  rollback! if @transaction && !@errors.empty?
end