shakemap.utils.queue

shakemap.utils.queue.send_queue(command, data, port=9755)[source]

Send a command and its data to the queue process.

Parameters:
  • command (str) – A valid command for the queue (e.g., ‘origin’).

  • data (JSON serializable) – The data associated with the command. Could be int, float, string, dictionary, list, etc. Must be JSON serializable. Must be less than MAX_SIZE bytes when serialized.

Returns:

Nothing

Return type:

nothing

Raises:
  • RuntimeError – If the serialized data is larger than MAX_SIZE.

  • OSError – If there is a problem with the socket or connection.

  • TypeError – If the supplied data is not JSON serializable.

shakemap.utils.queue.str_to_seconds(tstring)[source]

Convert time strings to seconds. Strings can be of the form:

<int> (ninutes) <int>m (minutes) <int>h (hours) <int>d (days) <int>y (years)

Parameters:

tstring (str) – An integer followed by an (optional) ‘m’, ‘h’, ‘d’, ‘y’.

Returns

int: The number of seconds represented by the input string. If the string is unadorned and represents a negative number, -1 is returned.

Raises:

ValueError – If the value cannot be converted to seconds.

shakemap.utils.queue.parse_config(config)[source]

Parse the config object to get usable data.

Parameters:

config (ConfigObj object) – The result of parsing the file’s configuration file.

Returns:

A cleaned up version of the input.

Return type:

dict

shakemap.utils.queue.get_config(install_path)[source]

Read the config and get it into a usable state.

Parameters:

install_path (str) – The install path of the current profile.

Returns:

A dictionary of configuration data.

Return type:

dict

class shakemap.utils.queue.Queue(pargs)[source]

Bases: object

queueMainLoop()[source]
doPeriodicTasks()[source]

Check for finished children and start any needed timed repeats.

Returns:

Nothing.

Return type:

nothing

getLogger()[source]

Set up a logger for this process.

Returns:

An instance of a logger.

Return type:

logging.logger

associateAll()[source]

Do the associateAll method of the the AmplitudeHandler and then process all of the events with updated data.

Returns:

Nothing.

Return type:

nothing

writeEventXml(event)[source]

Create the event directory if it doesn’t exist and write/re-write the event.xml file

Parameters:

event (dict) – The event data structure.

Returns:

Nothing.

Return type:

nothing

moveEventDirectory(oldid, newid)[source]

Change the name of an existing event directory to a new ID.

processOrigin(event, action)[source]

Determine if an event should be processed (or reprocessed) and dispatch it for processing.

Parameters:
  • event (dict) – The event data structure.

  • action (str) – The “type” of the trigger that caused this function to be called.

Returns:

Nothing.

Return type:

nothing

processOther(data, action)[source]

A trigger has been issued for an event. Treat this as an origin update. If the event in question is not in our database, ignore the message.

Parameters:
  • data (dict) – The event information dictionary.

  • action (str) – The “type” of the trigger that caused this function to be called.

Returns:

Nothing.

Return type:

nothing

processCancel(data)[source]

We’ve received a cancellation of an event: run ‘shake cancel’.

Parameters:

data (dict) – The dictionary must have an event ID under the ‘id’ key.

Returns:

Nothing.

Return type:

nothing

magnitudeTooSmall(event)[source]

Return False if the magnitude is greater than the threshold magnitude of the first metro box within which it falls, or the global minmag if it does not fall within a box; return true otherwise.

Parameters:

event (dict) – The event dictionary; must contain at least “mag”, “lon”, and “lat” keys.

Returns:

True if the event is too small to process; False otherwise.

Return type:

bool

eventTooOldOrInFuture(event)[source]

Return True if the event is too old or too far in the future to process; return False otherwise.

Parameters:

event (dict) – The event data structure.

Returns:

True if the event is older than old_event_age or is more than future_event_age in the future; returns False otherwise.

Return type:

bool

dispatchEvent(event, action)[source]

Queue a run for the specified event.

Parameters:
  • event (dict) – The data structure of the event to process.

  • action (str) – ‘cancel’, ‘test’, or some other string. ‘cancel’ starts the cancel process, ‘test’ queues the process ‘echo eventid’. Any other string queues the shake process to be run at the next opportunity. See the configuration file ‘queue.conf’ for the exact commands that will be run.

Returns:

Nothing.

Return type:

nothing

runQueuedEvents()[source]

If there is space, run events from the queue

reapChildren()[source]

Look through the list of child processes, reap the ones that have finished, and kill any that are taking too long.

Returns:

Nothing. Completed or killed child processes are removed from the list of children.

Return type:

nothing

getContext(context)[source]

Returns a context based on the value of the ‘attached’ argument. If attached is True, then the function returns an instance of the Dummycontext; if it is False the function returns the ‘context’ argument.

Parameters:

context (Context manager) – A valid context manager.

Returns:

If attached is True, the function returns an

instance of the Dummycontext; if False, returns the ‘context’ argument.

Return type:

Context manager

class shakemap.utils.queue.Dummycontext[source]

Bases: object

This is a dummy context that can be used as a context manager with ‘with’. It doesn’t do anything.

class shakemap.utils.queue.EventQueue(ipath)[source]

Bases: object

Class to maintain some persistence in the event of a program crash or shutdown. The db file can be removed if the operator wants a fresh start.

commit()[source]

Commit any operations to the database.

getQueuedEvents()[source]
queueEvent(eventid, command, mag)[source]
dequeueEvent(eventid)[source]
getRunningEvents()[source]
insertRunningEvent(eventid, command)[source]
deleteRunningEvent(eventid)[source]