Initial commit - untested

This commit is contained in:
Kumi 2022-01-13 08:08:54 +01:00
commit 78ba2af316
4 changed files with 91 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
*.pyc
__pycache__/
venv/
.vscode/

19
logger.py Normal file
View file

@ -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"))

15
settings.py Normal file
View file

@ -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/"

53
worker.py Executable file
View file

@ -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()