Class: Message::Worker
- Inherits:
-
Object
- Object
- Message::Worker
- Defined in:
- lib/message/worker.rb
Defined Under Namespace
Modules: Enqueue
Constant Summary collapse
- DEFAULT_JOB_NAME =
'message-worker-default'
Class Attribute Summary collapse
-
.default_job ⇒ Object
Returns the value of attribute default_job.
-
.synch ⇒ Object
Returns the value of attribute synch.
Instance Attribute Summary collapse
-
#job_name ⇒ Object
readonly
Returns the value of attribute job_name.
Class Method Summary collapse
Instance Method Summary collapse
- #enq(work) ⇒ Object (also: #<<)
-
#initialize(job_name) ⇒ Worker
constructor
A new instance of Worker.
- #process(size = 1) ⇒ Object
- #start(size = 10, interval = 1) ⇒ Object
Constructor Details
#initialize(job_name) ⇒ Worker
Returns a new instance of Worker.
62 63 64 |
# File 'lib/message/worker.rb', line 62 def initialize(job_name) @job_name = job_name end |
Class Attribute Details
.default_job ⇒ Object
Returns the value of attribute default_job.
31 32 33 |
# File 'lib/message/worker.rb', line 31 def default_job @default_job end |
.synch ⇒ Object
Returns the value of attribute synch.
31 32 33 |
# File 'lib/message/worker.rb', line 31 def synch @synch end |
Instance Attribute Details
#job_name ⇒ Object (readonly)
Returns the value of attribute job_name.
60 61 62 |
# File 'lib/message/worker.rb', line 60 def job_name @job_name end |
Class Method Details
.default ⇒ Object
37 38 39 |
# File 'lib/message/worker.rb', line 37 def default new(default_job) end |
.jobs ⇒ Object
49 50 51 |
# File 'lib/message/worker.rb', line 49 def jobs @jobs ||= RUBY_PLATFORM =~ /java/ ? java.util.concurrent.ConcurrentHashMap.new : {} end |
.process(*args) ⇒ Object
41 42 43 |
# File 'lib/message/worker.rb', line 41 def process(*args) default.process(*args) end |
.reset ⇒ Object
53 54 55 56 57 |
# File 'lib/message/worker.rb', line 53 def reset @default_job = nil @synch = nil @jobs = nil end |
.start(*args) ⇒ Object
45 46 47 |
# File 'lib/message/worker.rb', line 45 def start(*args) default.start(*args) end |
Instance Method Details
#enq(work) ⇒ Object Also known as: <<
85 86 87 88 89 |
# File 'lib/message/worker.rb', line 85 def enq(work) job.enq(YAML.dump(work)).tap do process if self.class.synch end end |
#process(size = 1) ⇒ Object
81 82 83 |
# File 'lib/message/worker.rb', line 81 def process(size=1) job.process(size) end |
#start(size = 10, interval = 1) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/message/worker.rb', line 66 def start(size=10, interval=1) Thread.start do begin log(:info) { "start" } loop do process(size) sleep interval end log(:info) { "stopped" } rescue => e log(:error) { "crashed: #{e.message}\n#{e.backtrace.join("\n")}"} end end end |