Merge pull request #4 from internetarchive/fixes-no-unique-service-after-nomination

Fixes no unique service after nomination
This commit is contained in:
Noah Levitt 2017-05-17 12:11:59 -07:00 committed by GitHub
commit 9b8b708c8c

View File

@ -20,6 +20,7 @@ import rethinkdb as r
import logging
import socket
import os
import doublethink
class ServiceRegistry(object):
'''
@ -220,6 +221,8 @@ class ServiceRegistry(object):
the unique service, if there is one and it is healthy, otherwise
None
'''
# use the same concept of 'now' for all queries
now = doublethink.utcnow()
if candidate is not None:
candidate['id'] = role
@ -231,8 +234,8 @@ class ServiceRegistry(object):
if not (isinstance(val, float) or isinstance(val, int)) or val <= 0:
raise Exception('heartbeat_interval must be a number > 0')
candidate['first_heartbeat'] = r.now()
candidate['last_heartbeat'] = r.now()
candidate['first_heartbeat'] = now
candidate['last_heartbeat'] = now
if not 'host' in candidate:
candidate['host'] = socket.gethostname()
if not 'pid' in candidate:
@ -243,7 +246,7 @@ class ServiceRegistry(object):
lambda row: r.branch(
r.branch(
row,
row['last_heartbeat'] > r.now() - row['heartbeat_interval'] * 3,
row['last_heartbeat'] > now - row['heartbeat_interval'] * 3,
False),
row, candidate),
return_changes='always').run()
@ -256,7 +259,7 @@ class ServiceRegistry(object):
results = list(self.rr.table(
'services', read_mode='majority').get_all(role).filter(
lambda row: row['last_heartbeat'] > r.now() - row['heartbeat_interval'] * 3).run())
lambda row: row['last_heartbeat'] > now - row['heartbeat_interval'] * 3).run())
if results:
return results[0]
else: