def heartbeat( instance_id: str = None, instance: Instance = None, system: System = None, **_, ) -> Instance: """Instance heartbeat Args: instance_id: The Instance ID instance: The Instance system: The System Returns: The updated Instance """ system, instance = _from_kwargs(system=system, instance=instance, instance_id=instance_id) system = db.modify( system, query={"instances__name": instance.name}, set__instances__S__status_info__heartbeat=datetime.utcnow(), ) return system.get_instance_by_name(instance.name)
def _from_kwargs( system: System = None, instance: Instance = None, system_id: str = None, instance_name: str = None, instance_id: str = None, **_, ) -> Tuple[System, Instance]: if system and instance: return system, instance if not system: if system_id: system = db.query_unique(System, raise_missing=True, id=system_id) elif instance: system = db.query_unique(System, raise_missing=True, instances__contains=instance) elif instance_id: system = db.query_unique(System, raise_missing=True, instances__id=instance_id) else: raise NotFoundException("Unable to find System") if not instance: if instance_name: instance = system.get_instance_by_name(instance_name) elif instance_id: instance = system.get_instance_by_id(instance_id) else: raise NotFoundException("Unable to find Instance") return system, instance
def update( instance_id: str = None, instance: Instance = None, system: System = None, new_status: str = None, metadata: dict = None, update_heartbeat: bool = True, **_, ) -> Instance: """Update an Instance status. Will also update the status_info heartbeat. Args: instance_id: The Instance ID instance: The Instance system: The System new_status: The new status metadata: New metadata update_heartbeat: Set the heartbeat to the current time Returns: The updated Instance """ system, instance = _from_kwargs(system=system, instance=instance, instance_id=instance_id) logger.debug(f"Updating instance {system}[{instance}]") updates = {} if new_status: updates["set__instances__S__status"] = new_status if new_status == "STOPPED": lpm.update(instance_id=instance_id, restart=False, stopped=True) if update_heartbeat: updates["set__instances__S__status_info__heartbeat"] = datetime.utcnow( ) if metadata: metadata_update = dict(instance.metadata) metadata_update.update(metadata) updates["set__instances__S__metadata"] = metadata_update system = db.modify(system, query={"instances__name": instance.name}, **updates) return system.get_instance_by_name(instance.name)
def initialize( instance_id: str = None, instance: Instance = None, system: System = None, runner_id: str = None, **_, ) -> Instance: """Initializes an instance. Args: instance_id: The Instance ID instance: The Instance system: The System runner_id: The runner id to associate with this plugin, if any Returns: The updated Instance """ system, instance = _from_kwargs(system=system, instance=instance, instance_id=instance_id) logger.debug(f"Initializing instance {system}[{instance}]") queue_spec = queue.create(instance, system) system = db.modify( system, query={"instances__name": instance.name}, **{ "set__instances__S__status": "INITIALIZING", "set__instances__S__status_info__heartbeat": datetime.utcnow(), "set__instances__S__metadata__runner_id": runner_id, "set__instances__S__queue_type": queue_spec["queue_type"], "set__instances__S__queue_info": queue_spec["queue_info"], }, ) start(instance=instance, system=system) return system.get_instance_by_name(instance.name)