Skip to content

Commit

Permalink
make the scheduler a flask application p2
Browse files Browse the repository at this point in the history
  • Loading branch information
afourmy committed Jun 29, 2020
1 parent e1d0542 commit 3332bab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
2 changes: 0 additions & 2 deletions build/requirements/requirements_scheduler.txt

This file was deleted.

2 changes: 1 addition & 1 deletion build/scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function run() {
export ENMS_ADDR="http://192.168.56.102"
export ENMS_USER="admin"
export ENMS_PASSWORD="admin"
uvicorn scheduler:scheduler --host 0.0.0.0
gunicorn --config ../gunicorn.py scheduler:scheduler
exit 0
fi
if [ "$redis" = true ]; then
Expand Down
24 changes: 12 additions & 12 deletions scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from datetime import datetime
from flask import Flask, jsonify
from flask import Flask, jsonify, request
from json import load
from logging.config import dictConfig
from os import environ
Expand Down Expand Up @@ -48,38 +48,38 @@ def configure_scheduler(self):

def register_routes(self):
@self.route("/job", methods=["DELETE"])
def delete(request):
job_id = await request.json()
def delete():
print(request.json)
job_id = request.json
if self.scheduler.get_job(job_id):
self.scheduler.remove_job(job_id)
return jsonify(True)

@self.route("/next_runtime/{task_id}")
def next_runtime(request):
job = self.scheduler.get_job(request.path_params["task_id"])
def next_runtime(task_id):
job = self.scheduler.get_job(task_id)
if job and job.next_run_time:
return jsonify(job.next_run_time.strftime("%Y-%m-%d %H:%M:%S"))
return jsonify("Not Scheduled")

@self.route("/schedule", methods=["POST"])
def schedule(request):
data = await request.json()
if data["mode"] in ("resume", "schedule"):
result = self.schedule_task(data["task"])
def schedule():
if request.json["mode"] in ("resume", "schedule"):
result = self.schedule_task(request.json["task"])
if not result:
return jsonify({"alert": "Cannot schedule in the past."})
else:
return jsonify({"response": "Task resumed.", "active": True})
else:
try:
self.scheduler.pause_job(data["task"]["id"])
self.scheduler.pause_job(request.json["task"]["id"])
return jsonify({"response": "Task paused."})
except JobLookupError:
return jsonify({"alert": "There is no such job scheduled."})

@self.route("/time_left/{task_id}")
def time_left(request):
job = self.scheduler.get_job(request.path_params["task_id"])
def time_left(task_id):
job = self.scheduler.get_job(task_id)
if job and job.next_run_time:
delta = job.next_run_time.replace(tzinfo=None) - datetime.now()
hours, remainder = divmod(delta.seconds, 3600)
Expand Down

0 comments on commit 3332bab

Please sign in to comment.