-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spark fair scheduling, asynchronous job in apii (jobs/), multiple api…
… end-points (...Spark for legacy, or algorithm/...) create handler manager to have multiple endpoint for the same algorithm implement a demo asynchronous mode in the restapi remove pydataclasses dependency
- Loading branch information
thomas loubrieu
committed
Sep 29, 2020
1 parent
af2b234
commit 9fa5a80
Showing
18 changed files
with
321 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ gdal==3.0.2 | |
mock==2.0.0 | ||
singledispatch==3.4.0.3 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0"?> | ||
<allocations> | ||
<pool name="default"> | ||
<schedulingMode>FAIR</schedulingMode> | ||
<weight>1</weight> | ||
<minShare>2</minShare> | ||
</pool> | ||
</allocations> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .job import Job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from datetime import datetime | ||
|
||
class Job(): | ||
def __init__(self): | ||
self.request = None # NexusRequestObject | ||
self.result_future = None # tornado.gen.Future | ||
self.time_created = datetime.now() | ||
self.time_done = None | ||
|
||
|
||
|
||
|
81 changes: 81 additions & 0 deletions
81
analysis/webservice/nexus_tornado/request/handlers/NexusAsyncJobHandler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import logging | ||
import json | ||
import uuid | ||
from datetime import datetime, timedelta | ||
import tornado.web | ||
import tornado.ioloop | ||
from webservice.nexus_tornado.request.renderers import NexusRendererFactory | ||
|
||
|
||
class NexusAsyncJobHandler(tornado.web.RequestHandler): | ||
|
||
_job_pool = {} | ||
__logger = logging.getLogger('nexus') | ||
|
||
obsolete_after = timedelta(hours=12) | ||
clean_obsolete_every = timedelta(minutes=15) | ||
|
||
@classmethod | ||
def get_job_pool(cls): | ||
return cls._job_pool | ||
|
||
@classmethod | ||
def start_jobs_cleaner(cls): | ||
|
||
def clean(): | ||
for key, job in cls._job_pool.iteritems(): | ||
if datetime.now() - job.time_done > cls.obsolete_after: | ||
cls.__logger.info("clean job {}".format(key)) | ||
del cls._job_pool[key] | ||
|
||
tornado.ioloop.IOLoop.current().call_later(cls.clean_obsolete_every.seconds, clean) | ||
|
||
def get(self, job_id): | ||
self.__logger.info("get job among {}".format(self._job_pool)) | ||
if job_id in self._job_pool: | ||
job = self._job_pool[job_id] | ||
if job.result_future.done(): | ||
renderer = NexusRendererFactory.get_renderer(job.request) | ||
renderer.render(self, job.result_future.result()) | ||
else: | ||
self._non_completed_job_callback(job_id) | ||
|
||
else: | ||
self._non_existing_job_callback(job_id) | ||
|
||
def _non_existing_job_callback(self, job_id, code=404): | ||
message = "Job {} does not exist".format(job_id) | ||
self._error_callback(message, code) | ||
|
||
def _non_completed_job_callback(self, job_id, code=202): | ||
message = "Job {} is being processed".format(job_id) | ||
self._error_callback(message, code) | ||
|
||
def _error_callback(self, message, code): | ||
self.__logger.info(message, exc_info=True) | ||
|
||
self.set_header("Content-Type", "application/json") | ||
self.set_header("Cache-Control", "no-cache, no-store, must-revalidate") | ||
self.set_header("Pragma", "no-cache") | ||
self.set_header("Expires", 0) | ||
self.set_status(code) | ||
|
||
response = { | ||
"error": message, | ||
"code": code | ||
} | ||
|
||
self.write(json.dumps(response, indent=5)) | ||
self.finish() | ||
|
||
def data_received(self, chunk): | ||
pass | ||
|
||
@classmethod | ||
def get_short_job_id(cls): | ||
while True: | ||
job_id = str(uuid.uuid4())[:6] | ||
if job_id not in cls._job_pool: | ||
return job_id | ||
|
||
|
Oops, something went wrong.