Code Documentation¶
-
class
mongoelector.
MongoLocker
(key, db, dbcollection='mongolocker', ttl=600, timeparanoid=True)[source]¶ Distributed lock object backed by MongoDB.
Intended to mimic standard lib Lock object as much as reasonable. This object is used by MongoElector, but is perfectly happy being used as a standalone distributed locking object.
Parameters: - key (str) – Name of distributed lock
- dbconn (PyMongo db connection) – Pymongo client connection to mongodb
- dbname (str) – name of database (defaults to ‘mongoelector’)
- dbname – name of collection (defaults to ‘mongolocker’)
- ttl (int) – Lock will expire (ttl seconds) after acquired unless renewed or released
- timeparanoid (bool) – Sanity check to ensure local server time matches mongodb server time (utc)
-
acquire
(blocking=True, timeout=None, step=0.25, force=False)[source]¶ Attempts to acquire the lock, will block and retry indefinitely by default. Can be configured not to block, or to have a timeout. You can also force the acquisition if you have a really good reason to do so.
Parameters: - blocking (bool) – If true (default), will wait until lock is acquired.
- timeout (int) – blocking acquire will fail after timeout in seconds if the lock hasn’t been acquired yet.
- step (float or int) – delay between acquire attempts
- force (bool) – CAUTION: will forcibly take ownership of the lock
-
get_current
()[source]¶ Returns the current (valid) lock object from the database, regardless of which instance it is owned by.
-
locked
()[source]¶ Returns current status of the lock, but does not indicate if the current instance has ownership or not. (for that, use ‘self.owned()’) This is a ‘look before you leap’ option. For example, it can be used to ensure that some process is owns the lock and is doing the associated work. Obviously this method does not guarantee that the current instance will be successful in obtaining the lock on a subsequent acquire.
Returns: Lock status Return type: bool
-
owned
()[source]¶ Determines if self is the owner of the lock object. This verifies the instance uuid matches the uuid of the lock record in the db.
Returns: Owner status Return type: bool
-
release
(force=False)[source]¶ releases lock if owned by the current instance.
Parameters: force – CAUTION: Forces the release to happen, even if the local instance isn’t the lock owner. :type force: bool
-
status
¶
-
class
mongoelector.
MongoElector
(key, db, ttl=15, onmaster=None, onmasterloss=None, onloop=None, app_version=None, report_status=True)[source]¶ This object will do lots of awesome distributed master election coolness
Create a MongoElector instance
Parameters: - key (str) – Name of the distributed lock that is used for master election. should be unique to this type of daemon i.e. any instance for which you want to run exactly one master should all share this same name.
- db – Connection to a MongoDB database
- ttl (int) – Time-to-live for the distributed lock. If the master node fails silently, this timeout must be hit before another node will take over.
- onmaster (Function or Method) – Function that will be run every time this instance is elected as the new master
- onmasterloss (Function or Method) – Function that will be run every time when this instance loses it’s master status
- onloop (Function or Method) – Function that will be run on every loop
- app_version (str) – Parent app version, if provided, will be included in node_status for monitoring
-
cluster_detail
¶
-
ismaster
¶ Returns True if this instance is master
-
master_exists
¶ Returns true if an instance (not necessarily this one) has master
-
node_status
¶ Status info for current object
-
poll
()[source]¶ Main polling logic, will refresh lock if it’s owned, or tries to obtain the lock if it’s available. Runs onloop callback after lock maintenance logic
In general, this should only be called by the elector thread
-
pollwait
¶ An appropriate sleep time to wait before next poll
-
running
¶ Returns true if the elector logic is running
-
start
(blocking=False)[source]¶ Starts mongo elector polling on a background thread then returns. If blocking is set to True, this will never return until stop() is
Parameters: blocking (bool) – If False, returns as soon as the elector thread is started. If True, will only return after stop() is called i.e. by another thread.