def keepAlive(): while True: try: # Wait keepAlive period time.sleep(config.timeout / 2) except: break # Connect to master and ask for status print("{time:s} Send KeepAlive".format(time=Utilities.current_time_formatted())) try: conn = messages.Connection(socket.create_connection((config.parent_address, config.parent_port))) connections_manager.add(conn) msg = messages.Status(config.id, [{ "State": "Idle" }]) conn.send(msg) # Inform parent about lower backup registrations and deregistrations for msg in backup_servers.getParentMessages(): conn.send(msg) conn.socket.shutdown(socket.SHUT_WR) # Read all messages from client separated by 0x17 until EOC (write stream closed) for msg in conn: parseMessageBCS(msg) conn.socket.close() connections_manager.remove(conn) except OSError as msg: print("KeepAlive connection error: {:s}".format(str(msg))) print("Assuming main communication server role") # Refresh components timeout, else when assume main cs role all would be thought dead for component in components.active(): component.touch() config.is_backup = False break
def removeInactiveComponents(): removeInactiveBackup() if config.is_backup: return for component in components.active(): # Wait twice the timeout, as nodes are sending state each timeout seconds # Don't check CSs, as their state is updated only for the parent in backups list if not component.isAlive(2 * config.timeout * 1000) and component.type != "CommunicationServer": # Release all problems or tasks assigned to that component problems.release(component.id) print("Removing inactive component: #{:d} ({:s})".format(component.id, type(component).__name__)) # Backup message: remove component backup_msg = copy.deepcopy(component.registerMessage) backup_msg.Id = component.id backup_msg.Deregister = True backup_servers.registerQueueAppend(backup_msg)