def delete_ssh(
*,
task_group_activity_id: int,
task_group_id: int,
resource: Resource,
profile: Profile,
) -> None:
"""
Delete a task group.
This function is run as a background task, therefore exceptions must be
handled.
Args:
task_group_id:
task_group_activity_id:
resource:
profile:
"""
LOGGER_NAME = f"{__name__}.ID{task_group_activity_id}"
with TemporaryDirectory() as tmpdir:
log_file_path = get_log_path(Path(tmpdir))
logger = set_logger(
logger_name=LOGGER_NAME,
log_file_path=log_file_path,
)
logger.debug("START")
with next(get_sync_db()) as db:
task_group, activity = get_activity_and_task_group(
task_group_activity_id=task_group_activity_id,
task_group_id=task_group_id,
db=db,
logger_name=LOGGER_NAME,
)
with SingleUseFractalSSH(
ssh_config=SSHConfig(
host=resource.host,
user=profile.username,
key_path=profile.ssh_key_path,
),
logger_name=LOGGER_NAME,
) as fractal_ssh:
try:
# Check SSH connection
ssh_ok = check_ssh_or_fail_and_cleanup(
fractal_ssh=fractal_ssh,
task_group=task_group,
task_group_activity=activity,
logger_name=LOGGER_NAME,
log_file_path=log_file_path,
db=db,
)
if not ssh_ok:
return
activity.status = TaskGroupActivityStatus.ONGOING
activity.log = get_current_log(log_file_path)
activity = add_commit_refresh(obj=activity, db=db)
db.delete(task_group)
db.commit()
logger.debug("Task group removed from database.")
if task_group.origin != TaskGroupOriginEnum.OTHER:
logger.debug(
f"Removing remote {task_group.path=} "
f"(with {profile.tasks_remote_dir=})."
)
fractal_ssh.remove_folder(folder=task_group.path)
logger.debug(f"Remote {task_group.path=} removed.")
activity.status = TaskGroupActivityStatus.OK
activity.log = get_current_log(log_file_path)
activity.timestamp_ended = get_timestamp()
activity = add_commit_refresh(obj=activity, db=db)
logger.debug("END")
except Exception as e:
fail_and_cleanup(
task_group=task_group,
task_group_activity=activity,
logger_name=LOGGER_NAME,
log_file_path=log_file_path,
exception=e,
db=db,
)
finally:
reset_logger_handlers(logger)