Class: Proletariat::Worker

Inherits:
PoolableActor
  • Object
show all
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

Methods included from ConfigurationMethods

exception_handler, listen_on, routing_keys

Methods included from Concerns::Logging

#log_info

Methods inherited from PoolableActor

#on_message, pool

Methods included from ActorCommon

included, #on_event

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(message)
  work message.body, message.to, message.headers if message.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(message = nil)
  if message
    Proletariat.logger.info(message)

    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, message = '', headers = {})
  log "Publishing to: #{to}"
  Proletariat.publish to, message, headers

  nil
end

#startedObject

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

#stoppedObject

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(message, routing_key, headers)
  fail NotImplementedError
end

#work_methodObject

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