PubsubConsumer: Implement max runtime

To have a clean break and a limited log file for monitoring bots
This commit is contained in:
Stephan Kulow 2019-03-09 08:25:26 +01:00
parent bb48831eca
commit fa6e49559b
2 changed files with 10 additions and 3 deletions

View File

@ -1,6 +1,7 @@
import logging import logging
import pika import pika
import sys import sys
import time
from datetime import datetime from datetime import datetime
class PubSubConsumer(object): class PubSubConsumer(object):
@ -30,6 +31,7 @@ class PubSubConsumer(object):
self._consumer_tag = None self._consumer_tag = None
self._prefix = amqp_prefix self._prefix = amqp_prefix
self._timer_id = None self._timer_id = None
self._run_until = None
self.logger = logger self.logger = logger
def restart_timer(self): def restart_timer(self):
@ -45,7 +47,10 @@ class PubSubConsumer(object):
def still_alive(self): def still_alive(self):
# output something so gocd doesn't consider it stalled # output something so gocd doesn't consider it stalled
self.logger.info('Still alive: {}'.format(datetime.now().time())) self.logger.info('Still alive: {}'.format(datetime.now().time()))
self.restart_timer() if self._run_until and time.time() > self._run_until:
self.stop()
else:
self.restart_timer()
def connect(self): def connect(self):
"""This method connects to RabbitMQ, returning the connection handle. """This method connects to RabbitMQ, returning the connection handle.
@ -315,11 +320,13 @@ class PubSubConsumer(object):
self.logger.debug('Creating a new channel') self.logger.debug('Creating a new channel')
self._connection.channel(on_open_callback=self.on_channel_open) self._connection.channel(on_open_callback=self.on_channel_open)
def run(self): def run(self, runtime=None):
"""Run the example consumer by connecting to RabbitMQ and then """Run the example consumer by connecting to RabbitMQ and then
starting the IOLoop to block and allow the SelectConnection to operate. starting the IOLoop to block and allow the SelectConnection to operate.
""" """
if runtime:
self._run_until = time.time() + runtime
self._connection = self.connect() self._connection = self.connect()
self._connection.ioloop.start() self._connection.ioloop.start()

View File

@ -264,6 +264,6 @@ if __name__ == '__main__':
l.add(Project(entry.get('name'))) l.add(Project(entry.get('name')))
try: try:
l.run() l.run(runtime=3600)
except KeyboardInterrupt: except KeyboardInterrupt:
l.stop() l.stop()