from flask_restful import Resource, request from app.main import scheduler import json from datetime import datetime def printing_something(text): print("printing {0} at {1}".format(text, datetime.now())) class Cron(Resource): def get(self): jobs = [] for job in scheduler.get_jobs(): jobdict = {} crontab = {} jobdict['id'] = job.id jobdict['name'] = job.name jobdict['args'] = str(job.args) jobdict['next_run_time'] = str(job.next_run_time) for f in job.trigger.fields: cuarval = str(f) crontab[f.name] = cuarval jobdict['crontab'] = crontab jobs.append(jobdict) return {'jobs': jobs} def post(self): json_data = request.get_json() if not json_data: return {'message': 'No input data provided'}, 400 day = '*' if not json_data.get('day') else json_data.get('day') hour = '*' if not json_data.get('hour') else json_data.get('hour') minute = '*' if not json_data.get('minute') else json_data.get('minute') text = json_data.get('text') job = scheduler.add_job(printing_something, trigger='cron', day=day, hour=hour, minute=minute, args=[text]) return "job details: {0}".format(job) def delete(self, job_id): # json_data = request.get_json() # if not json_data: # return {'message': 'No input data provided'}, 400 # jobid = json_data.get('id') resp = scheduler.remove_job(job_id=job_id) return resp