This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AutoShutdownMiddleware(dramatiq.Middleware): | |
def __init__(self, idle_timeout=120): | |
"""Middleware to shutdown worker if idle for more than idle_timeout seconds. | |
:param idle_timeout: Time in seconds to wait before shutting down the worker. | |
""" | |
self.idle_timeout = idle_timeout | |
self.last_message_time = time.time() | |
self.monitor_thread = threading.Thread(target=self._monitor_idle) | |
self.monitor_thread.start() |