Class: MongoAgent::Agent
- Inherits:
-
Object
- Object
- MongoAgent::Agent
- Defined in:
- lib/mongo_agent/agent.rb
Overview
Instance Attribute Summary collapse
-
#db ⇒ Moped::Session
readonly
This holds the Moped::Session object that can be used to query information from the MongoDB hosted by the MONGO_HOST environment variable.
-
#log ⇒ Hash
readonly
This holds the log while work! is running log will be a Hash with the following keys: tasks_processed: int number of tasks processed (success of failure) failed_tasks: int number of tasks that have failed The log is passed to the block that is assigned to process_while (see below).
-
#name ⇒ String
The name of the agent for which tasks will be taken from the queue.
-
#process_while ⇒ Object
This holds a block that will be passed the log as an argument and return true as long as the agent should continue to process tasks when work! is called, and false when work! should stop and return.
-
#queue ⇒ String
The name of the task queue that contains the tasks on which this agent will work.
-
#sleep_between ⇒ Object
number of seconds to sleep between each call to process! when running agent.work! or agent.process_while default 5.
Instance Method Summary collapse
-
#get_tasks(query = nil) ⇒ Moped::Query
get A MONGO_DB Moped::Query, either for the specified query Hash, or, when query is nil, all that are currently ready for the @name.
-
#initialize(attributes = nil) ⇒ Agent
constructor
create a new MongoAgent::Agent.
-
#process!(&agent_code) {|Task| ... } ⇒ Object
If a task for the agent is found that is ready, process! registers itself with the task by setting ready to false, and setting its hostname on the :agent_host field, and then passes the task to the supplied block.
-
#work!(&agent_code) {|Task| ... } ⇒ Object
Iteratively runs process! on the supplied Block, then sleeps :sleep_between between each attempt.
Constructor Details
#initialize(attributes = nil) ⇒ Agent
create a new MongoAgent::Agent
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mongo_agent/agent.rb', line 74 def initialize(attributes = nil) if attributes.nil? raise MongoAgent::Error, "attributes Hash required with name and queue keys required" end @name = attributes[:name] @queue = attributes[:queue] unless @name && @queue raise MongoAgent::Error, "attributes[:name] and attributes[:queue] are required!" end build_db() if attributes[:sleep_between] @sleep_between = attributes[:sleep_between] else @sleep_between = 5 end @log = { tasks_processed: 0, failed_tasks: 0 } @process_while = ->(log) { true } end |
Instance Attribute Details
#db ⇒ Moped::Session (readonly)
This holds the Moped::Session object that can be used to query information from the MongoDB
hosted by the MONGO_HOST environment variable
38 39 40 |
# File 'lib/mongo_agent/agent.rb', line 38 def db @db end |
#log ⇒ Hash (readonly)
This holds the log while work! is running
log will be a Hash with the following keys:
tasks_processed: int number of tasks processed (success of failure)
failed_tasks: int number of tasks that have failed
The log is passed to the block that is assigned to process_while (see below)
46 47 48 |
# File 'lib/mongo_agent/agent.rb', line 46 def log @log end |
#name ⇒ String
The name of the agent for which tasks will be taken from the queue
58 59 60 |
# File 'lib/mongo_agent/agent.rb', line 58 def name @name end |
#process_while ⇒ Object
This holds a block that will be passed the log as an argument and return true
as long as the agent should continue to process tasks when work! is called,
and false when work! should stop and return.
If not set, the agent will continue to process tasks until it is killed when
work! is called
@return [Block]
54 55 56 |
# File 'lib/mongo_agent/agent.rb', line 54 def process_while @process_while end |
#queue ⇒ String
The name of the task queue that contains the tasks on which this agent will work.
62 63 64 |
# File 'lib/mongo_agent/agent.rb', line 62 def queue @queue end |
#sleep_between ⇒ Object
number of seconds to sleep between each call to process! when running agent.work! or agent.process_while default 5
66 67 68 |
# File 'lib/mongo_agent/agent.rb', line 66 def sleep_between @sleep_between end |
Instance Method Details
#get_tasks(query = nil) ⇒ Moped::Query
get A MONGO_DB Moped::Query, either for the specified query Hash, or, when query is nil, all that are currently ready for the @name. This can be used to scan through the tasks on the @queue to perform aggregation tasks:
223 224 225 226 227 228 229 |
# File 'lib/mongo_agent/agent.rb', line 223 def get_tasks(query = nil) if query.nil? return @db[@queue].find({agent_name: @name, ready: true}) else return @db[@queue].find(query) end end |
#process!(&agent_code) {|Task| ... } ⇒ Object
If a task for the agent is found that is ready, process! registers itself with the task by setting ready to false, and setting its hostname on the :agent_host field, and then passes the task to the supplied block. This block must return a required boolean field indicating success or failure, and an optional hash of key - value fields that will be updated on the task Document. Note, the updates are made regardless of the value of success. In fact, the agent can be configured to update different fields based on success or failure. Also, note that any key, value supported by JSON can be stored in the hash. This allows the agent to communicate any useful information to the task for other agents (MongoAgent::Agent or human) to use. The block must try at all costs to avoid terminating. If an error is encountered, block should return false for the success field to signal that the process failed. If no errors are encountered block should return true for the success field.
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mongo_agent/agent.rb', line 153 def process!(&agent_code) (runnable, task) = register() return unless runnable (success, update) = agent_code.call(task) @log[:tasks_processed] += 1 if success complete_task(task, update) else fail_task(task, update) end return end |
#work!(&agent_code) {|Task| ... } ⇒ Object
Iteratively runs process! on the supplied Block, then sleeps :sleep_between between each attempt. Block should match the specifications of what can be passed to process! (see above).
If @process_while is set to a Block, Lambda, or Method, then it is called after
each task is processed, and passed the current @log. As long as the
Block returns true, work! will continue to process. work! will stop processing
tasks when the Block returns false.
194 195 196 197 198 199 200 |
# File 'lib/mongo_agent/agent.rb', line 194 def work!(&agent_code) while (@process_while.call(@log)) process!(&agent_code) sleep @sleep_between end end |