From 78ba2af316455d14d995c78b43db00f69c8b0749 Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 13 Jan 2022 08:08:54 +0100 Subject: [PATCH] Initial commit - untested --- .gitignore | 4 ++++ logger.py | 19 +++++++++++++++++++ settings.py | 15 +++++++++++++++ worker.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 logger.py create mode 100644 settings.py create mode 100755 worker.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..608670f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +__pycache__/ +venv/ +.vscode/ \ No newline at end of file diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..65c28b6 --- /dev/null +++ b/logger.py @@ -0,0 +1,19 @@ +from datetime import datetime + +class Logger: + @staticmethod + def _format(message: str, severity: str) -> str: + datestr = str(datetime.now()) + return f"{datestr} - {severity} - {message}" + + def debug(self, message: str) -> None: + print(self.__class__()._format(message, "DEBUG")) + + def info(self, message: str) -> None: + print(self.__class__()._format(message, "INFO")) + + def error(self, message: str) -> None: + print(self.__class__()._format(message, "ERROR")) + + def fatal(self, message: str) -> None: + print(self.__class__()._format(message, "FATAL")) \ No newline at end of file diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..7dbc537 --- /dev/null +++ b/settings.py @@ -0,0 +1,15 @@ +# Full path to the base directory of the Moodle installation +MOODLE_PATH = "/var/www/html/prod/" + +# Full path to the Moosh executable +MOOSH_PATH = "/usr/local/bin/moosh" + +# Full path to the signalling directory +# Must be read-writeable by the user executing the worker + +SIGNAL_PATH = "/tmp/moodle-export-worker/" + +# Full path to the destination directory for export files +# Must exist and be writeable by the user executing the worker + +OUTPUT_PATH = "/replication/courses/" \ No newline at end of file diff --git a/worker.py b/worker.py new file mode 100755 index 0000000..a185168 --- /dev/null +++ b/worker.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +from pathlib import Path +from datetime import datetime + +from settings import * +from logger import Logger + + +logger = Logger() + + +def trigger_export(courseid, categoryid): + output_name = f"course_{courseid}_{categoryid}_{datetime.now().timestamp()}" + subprocess.run([MOOSH_PATH, "-p", MOODLE_PATH, "course-backup", + "-p", OUTPUT_PATH, "-f", output_name, "--template", courseid]) + return output_name + + +def main(): + logger.info("Moodle Export Worker starting") + SIGNAL_PATH = Path(SIGNAL_PATH) + + if test.exists(): + if not test.is_dir(): + logger.fatal( + f"Cannot create signalling directory {SIGNAL_PATH} because a file with that name exists.") + exit(1) + else: + try: + test.mkdir() + except Exception as e: + logger.fatal( + f"Cannot create signalling directory {SIGNAL_PATH}: {e}") + exit(1) + + for f in test.glob("*-*.mew"): + logger.debug(f"Found file {f.name} - start processing") + try: + pre = datetime.now() + output_name = trigger_export(*f.name.split(".")[0].split("-")) + dur = datetime.now() - pre + size = Path(OUTPUT_PATH / output_name).stat().st_size + f.unlink() + logger.debug( + f"Created file {output_name} ({size} bytes) in {dur.seconds}") + except Exception as e: + logger.error( + f"Export of course {f.name.split('.')[0].split('-')[0]} failed: {e}") + + +if __name__ == "__main__": + main()