cron.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from flask_restful import Resource, request
  2. from app.main import scheduler
  3. import json
  4. from datetime import datetime
  5. def printing_something(text):
  6. print("printing {0} at {1}".format(text, datetime.now()))
  7. class Cron(Resource):
  8. def get(self):
  9. jobs = []
  10. for job in scheduler.get_jobs():
  11. jobdict = {}
  12. crontab = {}
  13. jobdict['id'] = job.id
  14. jobdict['name'] = job.name
  15. jobdict['args'] = str(job.args)
  16. jobdict['next_run_time'] = str(job.next_run_time)
  17. for f in job.trigger.fields:
  18. cuarval = str(f)
  19. crontab[f.name] = cuarval
  20. jobdict['crontab'] = crontab
  21. jobs.append(jobdict)
  22. return {'jobs': jobs}
  23. def post(self):
  24. json_data = request.get_json()
  25. if not json_data:
  26. return {'message': 'No input data provided'}, 400
  27. day = '*' if not json_data.get('day') else json_data.get('day')
  28. hour = '*' if not json_data.get('hour') else json_data.get('hour')
  29. minute = '*' if not json_data.get('minute') else json_data.get('minute')
  30. text = json_data.get('text')
  31. job = scheduler.add_job(printing_something, trigger='cron',
  32. day=day, hour=hour, minute=minute, args=[text])
  33. return "job details: {0}".format(job)
  34. def delete(self, job_id):
  35. # json_data = request.get_json()
  36. # if not json_data:
  37. # return {'message': 'No input data provided'}, 400
  38. # jobid = json_data.get('id')
  39. resp = scheduler.remove_job(job_id=job_id)
  40. return resp