2023-11-05 22:32:41 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# celery.py - PVC client function library, Celery helper fuctions
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2023-12-29 11:16:59 -05:00
|
|
|
# Copyright (C) 2018-2024 Joshua M. Boniface <joshua@boniface.me>
|
2023-11-05 22:32:41 -05:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, version 3.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from logging import getLogger
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
|
|
|
|
class TaskFailure(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-11-09 14:05:15 -05:00
|
|
|
def start(celery, msg, current=0, total=1):
|
2023-11-05 22:32:41 -05:00
|
|
|
logger = getLogger(__name__)
|
2023-11-09 14:05:15 -05:00
|
|
|
logger.info(f"Starting {current}/{total}: {msg}")
|
2023-11-10 01:28:41 -05:00
|
|
|
if celery is None:
|
|
|
|
return
|
2023-11-05 22:32:41 -05:00
|
|
|
celery.update_state(
|
|
|
|
state="RUNNING", meta={"current": current, "total": total, "status": msg}
|
|
|
|
)
|
2023-11-09 23:54:05 -05:00
|
|
|
sleep(1)
|
2023-11-05 22:32:41 -05:00
|
|
|
|
|
|
|
|
2023-11-16 16:05:55 -05:00
|
|
|
def fail(celery, msg, exception=None, current=1, total=1):
|
|
|
|
if exception is None:
|
|
|
|
exception = TaskFailure
|
|
|
|
|
2023-11-16 18:05:52 -05:00
|
|
|
msg = f"{type(exception()).__name__}: {msg}"
|
2023-11-16 16:05:55 -05:00
|
|
|
|
2023-11-05 22:32:41 -05:00
|
|
|
logger = getLogger(__name__)
|
|
|
|
logger.error(msg)
|
2023-11-16 16:05:55 -05:00
|
|
|
|
2023-11-05 22:32:41 -05:00
|
|
|
sys.tracebacklimit = 0
|
2023-11-16 16:05:55 -05:00
|
|
|
raise exception(msg)
|
2023-11-05 22:32:41 -05:00
|
|
|
|
|
|
|
|
2023-11-09 14:05:15 -05:00
|
|
|
def log_info(celery, msg):
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
logger.info(f"Task log: {msg}")
|
|
|
|
|
|
|
|
|
|
|
|
def log_warn(celery, msg):
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
logger.warning(f"Task log: {msg}")
|
|
|
|
|
|
|
|
|
|
|
|
def log_err(celery, msg):
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
logger.error(f"Task log: {msg}")
|
|
|
|
|
|
|
|
|
2023-11-05 22:32:41 -05:00
|
|
|
def update(celery, msg, current=1, total=2):
|
|
|
|
logger = getLogger(__name__)
|
2023-11-09 14:05:15 -05:00
|
|
|
logger.info(f"Task update {current}/{total}: {msg}")
|
2023-11-10 01:28:41 -05:00
|
|
|
if celery is None:
|
|
|
|
return
|
2023-11-05 22:32:41 -05:00
|
|
|
celery.update_state(
|
|
|
|
state="RUNNING", meta={"current": current, "total": total, "status": msg}
|
|
|
|
)
|
2023-11-09 23:54:05 -05:00
|
|
|
sleep(1)
|
2023-11-05 22:32:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
def finish(celery, msg, current=2, total=2):
|
|
|
|
logger = getLogger(__name__)
|
2023-11-09 14:05:15 -05:00
|
|
|
logger.info(f"Task update {current}/{total}: Finishing up")
|
2023-11-10 01:28:41 -05:00
|
|
|
if celery is None:
|
|
|
|
return
|
2023-11-05 22:32:41 -05:00
|
|
|
celery.update_state(
|
|
|
|
state="RUNNING",
|
|
|
|
meta={"current": current, "total": total, "status": "Finishing up"},
|
|
|
|
)
|
2023-11-09 23:54:05 -05:00
|
|
|
sleep(1)
|
2023-11-09 14:05:15 -05:00
|
|
|
logger.info(f"Success {current}/{total}: {msg}")
|
2023-11-05 22:32:41 -05:00
|
|
|
return {"status": msg, "current": current, "total": total}
|