Merge branch 'master' into rename-heartbeat-interval-to-ttl

* master:
  bump version for pull request just merged and tweak run-tests.sh
  avoid database transaction to get current time
  make sure this variable is actually defined
  correct comment
  standardize the concept of 'now' to ensure that the same view of the service is returned from the read and update queries.
This commit is contained in:
Noah Levitt 2017-05-17 12:15:44 -07:00
commit 03e9d4eeef
3 changed files with 9 additions and 6 deletions

View File

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

View File

@ -3,7 +3,7 @@ import codecs
setuptools.setup( setuptools.setup(
name='doublethink', name='doublethink',
version='0.2.0.dev78', version='0.2.0.dev79',
packages=['doublethink'], packages=['doublethink'],
classifiers=[ classifiers=[
'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 2.7',

View File

@ -15,5 +15,5 @@ do
&& virtualenv -p $python /tmp/venv \ && virtualenv -p $python /tmp/venv \
&& source /tmp/venv/bin/activate \ && source /tmp/venv/bin/activate \
&& pip install pytest . \ && pip install pytest . \
&& py.test -v -s tests" && py.test -v tests"
done done