Class: Sequel::Worker
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Instance Method Summary collapse
- #async(proc = nil, &block) ⇒ Object (also: #add, #<<)
- #busy? ⇒ Boolean
-
#initialize(db = nil) ⇒ Worker
constructor
A new instance of Worker.
- #join ⇒ Object
- #work ⇒ Object
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
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/sequel/worker.rb', line 8 def errors @errors end |
#queue ⇒ Object (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
26 27 28 |
# File 'lib/sequel/worker.rb', line 26 def busy? @cur || !@queue.empty? end |
#join ⇒ Object
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 |
#work ⇒ Object
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 |