summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKristofer Hallin <kristofer@sunet.se>2022-01-04 21:20:09 +0100
committerKristofer Hallin <kristofer@sunet.se>2022-01-04 21:20:09 +0100
commit725968e03a7fdef32fecd748c7144fe572abeec1 (patch)
tree209e75e017e5b31decdb23a812818a7614dc5511
parentb74745607c07a3980da705342271af49288eaeae (diff)
More job related things.feature.job_scheduler
-rw-r--r--src/jobs.py29
-rwxr-xr-xsrc/main.py15
2 files changed, 41 insertions, 3 deletions
diff --git a/src/jobs.py b/src/jobs.py
index 67c4e1c..2e6a76a 100644
--- a/src/jobs.py
+++ b/src/jobs.py
@@ -7,6 +7,9 @@ from pytz import utc
from db import SqlDB
from job import Job, JobStatus
+from log import get_logger
+
+logger = get_logger()
class JobScheduler(object):
@@ -18,6 +21,19 @@ class JobScheduler(object):
timezone=utc,
)
+ self.reset_job_status()
+
+ def reset_job_status(self):
+ logger.info('Clearing jobs.')
+ with SqlDB.sql_session() as session:
+ query = session.query(Job).all()
+
+ for instance in query:
+ if instance.status == JobStatus.RUNNING or \
+ instance.status == JobStatus.SCHEDULED:
+ instance.status = JobStatus.CLEARED
+ logger.info(f'Job with ID {instance.id} was cleared.')
+
def get(self):
return self.__scheduler
@@ -41,6 +57,8 @@ class JobScheduler(object):
kwargs['job_id'] = job_id
kwargs['func'] = func
+ logger.info(f'Adding new job with ID {job_id}')
+
self.__scheduler.add_job(Job.starter, kwargs=kwargs)
return job_id
@@ -56,3 +74,14 @@ class JobScheduler(object):
jobs.append(job_dict)
return jobs
+
+ @classmethod
+ def get_job(cls, job_id):
+ with SqlDB.sql_session() as session:
+ job = session.query(Job).filter(Job.id == job_id).one_or_none()
+
+ if job is None:
+ return job
+ job_dict = job.as_dict()
+
+ return job_dict
diff --git a/src/main.py b/src/main.py
index 4454f7d..fada6e9 100755
--- a/src/main.py
+++ b/src/main.py
@@ -28,6 +28,8 @@ app.add_middleware(
# TODO: X-Total-Count
+scheduler = JobScheduler()
+
@app.middleware("http")
async def mock_x_total_count_header(request: Request, call_next):
@@ -147,9 +149,7 @@ async def get_key(key=None, Authorize: AuthJWT = Depends()):
@app.post('/sc/v0/add')
async def add(data: Request, Authorize: AuthJWT = Depends()):
- # Maybe we should protect this enpoint too and let the scanner use
- # a JWT token as well.
- # Authorize.jwt_required()
+ Authorize.jwt_required()
json_data = await data.json()
@@ -180,6 +180,15 @@ async def jobs_get(Authorize: AuthJWT = Depends()):
return JSONResponse(content={"status": "success", "jobs": data})
+@app.get('/sc/v0/jobs/{job_id}')
+async def jobs_get_id(job_id, Authorize: AuthJWT = Depends()):
+ Authorize.jwt_required()
+
+ data = [JobScheduler.get_job(job_id)]
+
+ return JSONResponse(content={"status": "success", "jobs": data})
+
+
def main(standalone=False):
if not standalone:
return app