Skip to content

Commit

Permalink
feat(api): add pending field to image ready response
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Mar 18, 2023
1 parent 15b6e03 commit 8cbdad3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
31 changes: 20 additions & 11 deletions api/onnx_web/server/api.py
Expand Up @@ -51,12 +51,17 @@


def ready_reply(
ready: bool, progress: int = 0, error: bool = False, cancel: bool = False
ready: bool = False,
cancelled: bool = False,
failed: bool = False,
pending: bool = False,
progress: int = 0,
):
return jsonify(
{
"cancel": cancel,
"error": error,
"cancelled": cancelled,
"failed": failed,
"pending": pending,
"progress": progress,
"ready": ready,
}
Expand Down Expand Up @@ -439,9 +444,9 @@ def cancel(context: ServerContext, pool: DevicePoolExecutor):
return error_reply("output name is required")

output_file = sanitize_name(output_file)
cancel = pool.cancel(output_file)
cancelled = pool.cancel(output_file)

return ready_reply(cancel is not False, cancel=cancel)
return ready_reply(cancelled=cancelled)


def ready(context: ServerContext, pool: DevicePoolExecutor):
Expand All @@ -450,22 +455,26 @@ def ready(context: ServerContext, pool: DevicePoolExecutor):
return error_reply("output name is required")

output_file = sanitize_name(output_file)
progress = pool.done(output_file)
pending, progress = pool.done(output_file)

if pending:
return ready_reply(pending=True)

if progress is None:
output = base_join(context.output_path, output_file)
if path.exists(output):
return ready_reply(True)
return ready_reply(ready=True)
else:
return ready_reply(
True, error=True
ready=True,
failed=True,
) # is a missing image really an error? yes will display the retry button

return ready_reply(
progress.finished,
ready=progress.finished,
progress=progress.progress,
error=progress.error,
cancel=progress.cancel,
failed=progress.error,
cancelled=progress.cancel,
)


Expand Down
12 changes: 6 additions & 6 deletions api/onnx_web/worker/command.py
Expand Up @@ -6,24 +6,24 @@ class ProgressCommand:
job: str
finished: bool
progress: int
cancel: bool
error: bool
cancelled: bool
failed: bool

def __init__(
self,
job: str,
device: str,
finished: bool,
progress: int,
cancel: bool = False,
error: bool = False,
cancelled: bool = False,
failed: bool = False,
):
self.job = job
self.device = device
self.finished = finished
self.progress = progress
self.cancel = cancel
self.error = error
self.cancelled = cancelled
self.failed = failed


class JobCommand:
Expand Down
26 changes: 20 additions & 6 deletions api/onnx_web/worker/pool.py
Expand Up @@ -232,7 +232,7 @@ def done(self, key: str) -> Tuple[bool, Optional[ProgressCommand]]:
for job in self.pending_jobs:
if job.name == key:
logger.debug("checking status for pending job: %s", key)
return (True, ProgressCommand(job.name, job.device, False, 0))
return (True, None)

logger.trace("checking status for unknown job: %s", key)
return (False, None)
Expand Down Expand Up @@ -365,25 +365,39 @@ def submit(
self.pending_jobs.append(job)
self.pending[device].put(job, block=False)

def status(self) -> List[Tuple[str, int, bool, bool, bool]]:
def status(self) -> List[Tuple[str, int, bool, bool, bool, bool]]:
history = [
(
name,
job.progress,
False,
job.finished,
job.cancel,
job.error,
job.cancelled,
job.failed,
)
for name, job in self.running_jobs.items()
]
history.extend(
[
(
job.name,
0,
True,
False,
False,
False,
) for job in self.pending_jobs
]
)
history.extend(
[
(
job.job,
job.progress,
False,
job.finished,
job.cancel,
job.error,
job.cancelled,
job.failed,
)
for job in self.finished_jobs
]
Expand Down

0 comments on commit 8cbdad3

Please sign in to comment.