self.hub = self.connect_to_hub()
self.queue = None
- # Indicates if this daemon is in running mode.
- self.__running = True
+ # Set when this process receives a shutdown signal
+ self._shutdown_signalled = None
# List of worker processes.
self.workers = []
# Register signal handlers.
self.register_signal_handlers()
+ # Initialize shutdown signal
+ self._shutdown_signalled = asyncio.Event()
+
# Send builder information
await self.hub.send_builder_info()
self.queue = await self.hub.queue(self.job_received)
# Run main loop
- while self.__running:
+ while True:
+ # Check if we are running by awaiting the shutdown signal
+ try:
+ await asyncio.wait_for(self._shutdown_signalled.wait(), timeout=5)
+ break
+ except asyncio.TimeoutError:
+ pass
+
# Send some information about this builder
await self.hub.send_builder_stats()
- # Sleep for 10 seconds
- await asyncio.sleep(10)
-
# Main loop has ended, but we wait until all workers have finished.
self.terminate_all_workers()
"""
Terminates all workers and exists the daemon.
"""
- if not self.__running:
+ # Ignore if the main method has never been called
+ if not self._shutdown_signalled:
+ return
+
+ # Ignore, if we are already shutting down
+ if self._shutdown_signalled.is_set():
return
self.log.info(_("Shutting down..."))
- self.__running = False
+ self._shutdown_signalled.set()
# Close queue connection so we won't receive any new jobs
if self.queue: