#!/usr/bin/env python3 """ Log jobs to be processed by the job processor. """ import os, json, pathlib, sys, time from settings import MINIQ_ROOT # Sub-dir for queued jobs with params QUEUED_JOBS = os.path.join(MINIQ_ROOT, 'queue') # Sub-dir for job definition files JOB_DEFS = os.path.join(MINIQ_ROOT, 'jobs') # Make sure we specified the job type if len(sys.argv) < 2: print('Missing name of job to enqueue.') sys.exit(1) # Get the job type and any arguments from the CLI _, job_name, *args = sys.argv data = { 'job': job_name, 'arguments': args, } # Make sure the job type is valid if not os.path.exists(os.path.join(JOB_DEFS, data['job'] + '.job')): print('Invalid job type.') sys.exit(1) # This is the name of the file that will be written to the queued jobs file_name = str(int(time.time() * 100000)) + '.' + str(job_name) + '.json' # Write the job data to the queue file with open(os.path.join(QUEUED_JOBS, file_name), 'w') as out_file: out_file.write(json.dumps(data)) print('Queued ' + job_name + ' as ' + file_name)