mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-08-05 16:58:59 +00:00
21 lines
771 B
Python
21 lines
771 B
Python
![]() |
PRIMARY_STATE = 1
|
||
|
SECONDARY_STATE = 2
|
||
|
|
||
|
def mongo_max_replication_lag(connection):
|
||
|
status = connection.admin.command('replSetGetStatus')
|
||
|
members = status['members']
|
||
|
primary_optime = None
|
||
|
oldest_secondary_optime = None
|
||
|
for member in members:
|
||
|
member_state = member['state']
|
||
|
optime = member['optime']
|
||
|
if member_state == PRIMARY_STATE:
|
||
|
primary_optime = optime.time
|
||
|
elif member_state == SECONDARY_STATE:
|
||
|
if not oldest_secondary_optime or optime.time < oldest_secondary_optime.time:
|
||
|
oldest_secondary_optime = optime.time
|
||
|
|
||
|
if not primary_optime or not oldest_secondary_optime:
|
||
|
raise Exception("Replica set is not healthy")
|
||
|
|
||
|
return primary_optime - oldest_secondary_optime
|