pyfarm.agent.service module

Manager Service

Sends and receives information from the master and performs systems level tasks such as log reading, system information gathering, and management of processes.

class pyfarm.agent.service.Agent[source]

Bases: object

Main class associated with getting getting the internals of the agent’s operations up and running including adding or updating itself with the master, starting the periodic task manager, and handling shutdown conditions.

classmethod agent_api()[source]

Return the API url for this agent or None if agent_id has not been set

classmethod agents_endpoint()[source]

Returns the API endpoint for used for updating or creating agents on the master

shutting_down[source]
repeating_call(delay, function, function_args=None, function_kwargs=None, now=True, repeat_max=None, function_id=None)[source]

Causes function to be called repeatedly up until repeat_max or until stopped.

Parameters:
  • delay (int) –

    Number of seconds to delay between calls of function.

    Note

    delay is an approximate interval between when one call ends and the next one begins. The exact time can vary due to how the Twisted reactor runs, how long it takes function to run and what else may be going on in the agent at the time.

  • function – A callable function to run
  • function_args (tuple, list) – Arguments to pass into function
  • function_kwargs (dict) – Keywords to pass into function
  • now (bool) – If True then run function right now in addition to scheduling it.
  • repeat_max (int) – Repeat calling function this may times. If not provided then we’ll continue to repeat calling function until the agent shuts down.
  • function_id (uuid.UUID) – Used internally to track a function’s execution count. This keyword exists so if you call repeating_call() multiple times on the same function or method it will handle repeat_max properly.
should_reannounce()[source]

Small method which acts as a trigger for reannounce()

reannounce()[source]

Method which is used to periodically contact the master. This method is generally called as part of a scheduled task.

system_data(requery_timeoffset=False)[source]

Returns a dictionary of data containing information about the agent. This is the information that is also passed along to the master.

build_http_resource()[source]
start(shutdown_events=True, http_server=True)[source]

Internal code which starts the agent, registers it with the master, and performs the other steps necessary to get things running.

Parameters:
  • shutdown_events (bool) – If True register all shutdown events so certain actions, such as information the master we’re going offline, can take place.
  • http_server (bool) – If True then construct and serve the externally facing http server.
stop()[source]

Internal code which stops the agent. This will terminate any running processes, inform the master of the terminated tasks, update the state of the agent on the master.

sigint_handler(*_)[source]
post_shutdown_to_master(stop_reactor=True)[source]

This method is called before the reactor shuts down and lets the master know that the agent’s state is now offline

post_agent_to_master(*args, **kwargs)[source]

Runs the POST request to contact the master. Running this method multiple times should be considered safe but is generally something that should be avoided.

callback_post_free_ram(response)[source]

Called when we get a response back from the master after POSTing a change for free_ram

errback_post_free_ram(failure)[source]

Error handler which is called if we fail to post a ram update to the master for some reason

post_free_ram()[source]

Posts the current nu

callback_free_ram_changed(change_type, key, new_value, old_value)[source]

Callback used to decide and act on changes to the config['ram'] value.

errback_post_cpu_count_change(failure)[source]

Error handler which is called if we fail to post a cpu count update to an existing agent for some reason.

callback_post_cpu_count_change(response)[source]

Called when we received a response from the master after

post_cpu_count(run=True)[source]

POSTs CPU count changes to the master

callback_cpu_count_changed(change_type, key, new_value, old_value)[source]

Callback used to decide and act on changes to the config['cpus'] value.

callback_agent_id_set(change_type, key, new_value, old_value, shutdown_events=True)[source]

When agent_id is created we need to:

  • Register a shutdown event so that when the agent is told to shutdown it will notify the master of a state change.
  • Star the scheduled task manager