Class: Proletariat::Worker
- Inherits:
-
PoolableActor
- Object
- Concurrent::Actor::Utils::AbstractWorker
- PoolableActor
- Proletariat::Worker
- Extended by:
- ConfigurationMethods
- Includes:
- Concerns::Logging
- Defined in:
- lib/proletariat/worker.rb
Overview
Public: Handles messages for Background processing. Subclasses should
overwrite the #work method.
Defined Under Namespace
Modules: ConfigurationMethods
Instance Method Summary collapse
-
#actor_work(message) ⇒ Object
Internal: Handles the Actor mailbox.
-
#log(message = nil) ⇒ Object
Public: Helper method to ease accessing the logger from within #work.
-
#publish(to, message = '', headers = {}) ⇒ Object
Public: Helper method to ease sending messages from within #work.
-
#started ⇒ Object
Public: Logs the ‘online’ status of the worker.
-
#stopped ⇒ Object
Public: Logs the ‘offline’ status of the worker.
-
#work(message, routing_key, headers) ⇒ Object
Public: Handles an incoming message to perform background work.
-
#work_method ⇒ Object
Public: Use #actor_work to handle the actor mailbox.
Methods included from ConfigurationMethods
exception_handler, listen_on, routing_keys
Methods included from Concerns::Logging
Methods inherited from PoolableActor
Methods included from ActorCommon
Instance Method Details
#actor_work(message) ⇒ Object
Internal: Handles the Actor mailbox. Delegates work to #work.
message - A Message to send.
12 13 14 |
# File 'lib/proletariat/worker.rb', line 12 def actor_work() work .body, .to, .headers if .is_a?(Message) end |
#log(message = nil) ⇒ Object
Public: Helper method to ease accessing the logger from within #work.
Sends #info to logger if message provided.
Examples
log 'Background Workers Unite!'
# Message is logged at info level.
log.error 'Something bad happened!'
# Message is logged at error level.
Returns the process-wide logger if message not supplied. Returns nil if message supplied.
29 30 31 32 33 34 35 36 37 |
# File 'lib/proletariat/worker.rb', line 29 def log( = nil) if Proletariat.logger.info() nil else Proletariat.logger end end |
#publish(to, message = '', headers = {}) ⇒ Object
Public: Helper method to ease sending messages from within #work.
to - The routing key for the message to as a String. In accordance
with the RabbitMQ convention you can use the '*' character to
replace one word and the '#' to replace many words.
message - The message as a String. headers - Hash of message headers.
Returns nil.
48 49 50 51 52 53 |
# File 'lib/proletariat/worker.rb', line 48 def publish(to, = '', headers = {}) log "Publishing to: #{to}" Proletariat.publish to, , headers nil end |
#started ⇒ Object
Public: Logs the ‘online’ status of the worker.
Returns nil.
58 59 60 61 62 |
# File 'lib/proletariat/worker.rb', line 58 def started log_info 'Now online' nil end |
#stopped ⇒ Object
Public: Logs the ‘offline’ status of the worker.
Returns nil.
67 68 69 70 71 |
# File 'lib/proletariat/worker.rb', line 67 def stopped log_info 'Now offline' nil end |
#work(message, routing_key, headers) ⇒ Object
Public: Handles an incoming message to perform background work.
message - The incoming message. routing_key - The incoming message’s routing key. headers - The incoming message’s headers.
Raises NotImplementedError unless implemented in subclass.
80 81 82 |
# File 'lib/proletariat/worker.rb', line 80 def work(, routing_key, headers) fail NotImplementedError end |
#work_method ⇒ Object
Public: Use #actor_work to handle the actor mailbox.
85 86 87 |
# File 'lib/proletariat/worker.rb', line 85 def work_method :actor_work end |