This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
125 additions
and
137 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,48 @@ | ||
import os | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
from fastapi import HTTPException | ||
|
||
from src.execute_user_code import execute_user_code | ||
from src.job_state import JobState | ||
from src.model.execution_input import ExecutionInput | ||
from src.model.job import Job | ||
|
||
|
||
class JobExecutor: | ||
def __init__(self): | ||
self.jobs = {} | ||
self.executor = ThreadPoolExecutor(max_workers=3) | ||
|
||
def create_job(self, job_id: str, execution_input: ExecutionInput) -> None: | ||
entry_point = os.environ.get("ENTRY_POINT", "user_code.src.program:run") | ||
future = self.executor.submit(execute_user_code, execution_input.data, execution_input.params, entry_point) | ||
|
||
job = JobState(job_id, future) | ||
self.jobs[job_id] = job | ||
|
||
def get_job_status(self, job_id: str) -> Job: | ||
job = self.jobs.get(job_id) | ||
if job is None: | ||
raise HTTPException(status_code=404, detail="Not found") | ||
|
||
return job.get_status() | ||
|
||
def get_job_result(self, job_id: str): | ||
job = self.jobs.get(job_id) | ||
if job is None: | ||
raise HTTPException(status_code=404, detail="Not found") | ||
|
||
result = job.get_result() | ||
if result is None: | ||
raise HTTPException(status_code=404, detail="Not found") | ||
|
||
return result | ||
|
||
def cancel_job(self, job_id: str): | ||
job = self.jobs.get(job_id) | ||
if job is None: | ||
raise HTTPException(status_code=404, detail="Not found") | ||
|
||
job.cancel() | ||
return job.get_status() |
This file was deleted.
Oops, something went wrong.
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,49 @@ | ||
import time | ||
from concurrent.futures import Future | ||
|
||
from src.helpers.date_formatter import format_timestamp | ||
from src.model.execution_output import ExecutionOutput | ||
from src.model.execution_status import ExecutionStatus | ||
from src.model.job import Job | ||
|
||
|
||
class JobState: | ||
def __init__(self, job_id: str, future: Future): | ||
self.job_id = job_id | ||
self.created_at = time.time() | ||
self.started_at = time.time() | ||
self.ended_at = None | ||
self.future = future | ||
self.future.add_done_callback(lambda f: self.__set_ended_at()) | ||
|
||
def has_finished(self): | ||
return self.future.done() | ||
|
||
def get_status(self) -> Job: | ||
if self.future.done(): | ||
status = ExecutionStatus.SUCCEEDED | ||
elif self.future.cancelled(): | ||
status = ExecutionStatus.CANCELLED | ||
elif self.future.exception() is not None: | ||
status = ExecutionStatus.FAILED | ||
else: | ||
status = ExecutionStatus.RUNNING | ||
|
||
return Job( | ||
id=self.job_id, | ||
status=status, | ||
created_at=format_timestamp(self.created_at), | ||
started_at=format_timestamp(self.started_at), | ||
ended_at=format_timestamp(self.ended_at), | ||
) | ||
|
||
def get_result(self) -> ExecutionOutput | None: | ||
if self.has_finished(): | ||
return self.future.result() | ||
return None | ||
|
||
def cancel(self): | ||
self.future.cancel() | ||
|
||
def __set_ended_at(self): | ||
self.ended_at = time.time() |
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