diff --git a/scheduler.py b/scheduler.py index b472b10ee4af9c6511ddb643a156ea6714bdcf04_c2NoZWR1bGVyLnB5..d4e42d771b0cf581c05456a158b876b795d1973f_c2NoZWR1bGVyLnB5 100644 --- a/scheduler.py +++ b/scheduler.py @@ -20,8 +20,9 @@ import time import shlex import subprocess +import logging from os.path import dirname, join, abspath SCRIPT = abspath(join(dirname(__file__), "main-run.sh")) @@ -23,8 +24,11 @@ from os.path import dirname, join, abspath SCRIPT = abspath(join(dirname(__file__), "main-run.sh")) +LOGGER = logging.getLogger(__name__) +LOG_FORMAT = "[%(asctime)s] %(message)s" + def read_next_scheduling(task_file_path): with open(task_file_path, "r") as f: @@ -41,7 +45,9 @@ print("RUNNING %r" % args) subprocess.check_call(args) except subprocess.CalledProcessError as e: - print("\nTask %r failed with error code %r" % (task, e.returncode)) + msg = "Task %r failed with error code %r" + LOGGER.error(msg, task, e.returncode) + print("\n" + msg % (task, e.returncode)) # XXX find a way to report an error without removing a task @@ -79,4 +85,5 @@ print("=" * 80) print("Got task %r" % task) + LOGGER.info("Starting %r", task) run(task) @@ -82,4 +89,5 @@ run(task) + LOGGER.info("Ending %r", task) print("\n" * 2) remove_task(next_task, task_file_path) print("=" * 80) @@ -87,5 +95,22 @@ time.sleep(1) +def setup_logging(logging_file): + # Reset handlers + LOGGER.handlers = [] + + LOGGER.setLevel(logging.INFO) + + # Add handler + console = logging.FileHandler(logging_file) + + console.setLevel(logging.INFO) + + formatter = logging.Formatter(LOG_FORMAT) + console.setFormatter(formatter) + + LOGGER.addHandler(console) + + if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) @@ -90,6 +115,7 @@ if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--log", help="The logging file to use") parser.add_argument("TASKS", help="The file path to the task file") args = parser.parse_args(sys.argv[1:]) @@ -92,5 +118,9 @@ parser.add_argument("TASKS", help="The file path to the task file") args = parser.parse_args(sys.argv[1:]) + # Configure logging + if args.log: + setup_logging(args.log) + main(args.TASKS)