Use pause library for sleeping
As described in https://pypi.python.org/pypi/pause , the pause library has higher precision than the sleep lib, and uses machine timestamp instead of counters. Change-Id: I0f1135757ef8d1ed6e4eb203b84632ef5ec91977
This commit is contained in:
parent
31413508a9
commit
a16b654009
|
@ -24,6 +24,7 @@ import threading
|
|||
import time
|
||||
|
||||
import croniter
|
||||
import pause
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.abspath(os.pardir)))
|
||||
sys.path.insert(0, os.path.abspath(os.getcwd()))
|
||||
|
@ -44,30 +45,21 @@ def validate_cfg(file):
|
|||
return False
|
||||
|
||||
|
||||
def do_something(**kwargs):
|
||||
def run_audit(**kwargs):
|
||||
# Put a message on the mq
|
||||
audit.send_message(**kwargs)
|
||||
|
||||
|
||||
def start_audit(**kwargs):
|
||||
#TODO(praneshp): fix bug here, where thread wakes up 0.0003 seconds
|
||||
#before it should, and then sleeps off and cannot wake up in time.
|
||||
#We lose the message this way.
|
||||
|
||||
now = datetime.datetime.now()
|
||||
schedule = kwargs['schedule']
|
||||
cron = croniter.croniter(schedule, now)
|
||||
next_iteration = cron.get_next(datetime.datetime)
|
||||
while True:
|
||||
now = datetime.datetime.now()
|
||||
LOG.warning(str(now) + str(next_iteration))
|
||||
if now > next_iteration:
|
||||
do_something(**kwargs['mq_args'])
|
||||
next_iteration = cron.get_next(datetime.datetime)
|
||||
else:
|
||||
sleep_time = (next_iteration - now).total_seconds()
|
||||
LOG.warning('Will sleep for ' + str(sleep_time))
|
||||
time.sleep(sleep_time)
|
||||
LOG.warning('Next call at %s' % next_iteration)
|
||||
pause.until(next_iteration)
|
||||
run_audit(**kwargs['mq_args'])
|
||||
next_iteration = cron.get_next(datetime.datetime)
|
||||
|
||||
|
||||
def register_audit(args):
|
||||
|
|
|
@ -4,3 +4,4 @@ sphinx>=1.1.2,<1.2
|
|||
croniter>=0.3.3
|
||||
kombu==3.0.7
|
||||
PyYAML>=3.1.0
|
||||
pause==0.1.2
|
||||
|
|
Loading…
Reference in New Issue