#!/usr/bin/env python3 from pathlib import Path from datetime import datetime import subprocess from settings import * from logger import Logger logger = Logger() def trigger_export(courseid, categoryid): global MOODLE_PATH, OUTPUT_PATH output_name = f"course_{courseid}_{categoryid}_{int(datetime.now().timestamp())}.mbz" result = subprocess.run([MOOSH_PATH, "-p", MOODLE_PATH, "course-backup", "-p", OUTPUT_PATH, "-f", output_name, "--template", courseid], capture_output=True) for line in str(result.stdout).splitlines(): logger.debug(line) return output_name def main(): global SIGNAL_PATH, OUTPUT_PATH logger.info("Moodle Export Worker starting") SIGNAL_PATH = Path(SIGNAL_PATH) OUTPUT_PATH = Path(OUTPUT_PATH) if SIGNAL_PATH.exists(): if not SIGNAL_PATH.is_dir(): logger.fatal( f"Cannot create signalling directory {SIGNAL_PATH} because a file with that name exists.") exit(1) else: try: SIGNAL_PATH.mkdir() except Exception as e: logger.fatal( f"Cannot create signalling directory {SIGNAL_PATH}: {e}") exit(1) for f in SIGNAL_PATH.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} seconds") except Exception as e: logger.error( f"Export of course {f.name.split('.')[0].split('-')[0]} failed: {e}") logger.info("Moodle Export Worker done") if __name__ == "__main__": main()