moodle-export-worker/worker.py

54 lines
1.6 KiB
Python
Raw Normal View History

2022-01-13 07:08:54 +00:00
#!/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()