+
+class PakfireDaemonKeepalive(multiprocessing.Process):
+ def __init__(self, config):
+ multiprocessing.Process.__init__(self)
+
+ # Save config.
+ self.config = config
+
+ def run(self, heartbeat=30):
+ # Register signal handlers.
+ self.register_signal_handlers()
+
+ # Create connection to the hub.
+ self.transport = transport.PakfireHubTransport(self.config)
+ self.transport.fork()
+
+ # Send our profile to the hub.
+ self.send_builder_info()
+
+ while True:
+ time_started = time.time()
+
+ # Send keepalive message.
+ self.send_keepalive()
+
+ # Get runtime of this loop iteration.
+ time_elapsed = time.time() - time_started
+
+ # Wait if the heartbeat time has not been reached, yet.
+ if time_elapsed < heartbeat:
+ time.sleep(heartbeat - time_elapsed)
+
+ def shutdown(self):
+ """
+ Ends this process immediately.
+ """
+ sys.exit(1)
+
+ # Signal handling.
+
+ def register_signal_handlers(self):
+ signal.signal(signal.SIGCHLD, self.handle_SIGCHLD)
+ signal.signal(signal.SIGINT, self.handle_SIGTERM)
+ signal.signal(signal.SIGTERM, self.handle_SIGTERM)
+
+ def handle_SIGCHLD(self, signum, frame):
+ """
+ Handle signal SIGCHLD.
+ """
+ # Must be here so that SIGCHLD won't be propagated to
+ # PakfireDaemon.
+ pass
+
+ def handle_SIGTERM(self, signum, frame):
+ """
+ Handle signal SIGTERM.
+ """
+ # Just shut down.
+ self.shutdown()
+