1
0
Fork 0

feat(api): add pending field to image ready response

This commit is contained in:
Sean Sube 2023-03-18 17:25:13 -05:00
parent 15b6e036e1
commit 8cbdad3a71
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
3 changed files with 46 additions and 23 deletions

View File

@ -51,12 +51,17 @@ logger = getLogger(__name__)
def ready_reply( 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( return jsonify(
{ {
"cancel": cancel, "cancelled": cancelled,
"error": error, "failed": failed,
"pending": pending,
"progress": progress, "progress": progress,
"ready": ready, "ready": ready,
} }
@ -439,9 +444,9 @@ def cancel(context: ServerContext, pool: DevicePoolExecutor):
return error_reply("output name is required") return error_reply("output name is required")
output_file = sanitize_name(output_file) 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): def ready(context: ServerContext, pool: DevicePoolExecutor):
@ -450,22 +455,26 @@ def ready(context: ServerContext, pool: DevicePoolExecutor):
return error_reply("output name is required") return error_reply("output name is required")
output_file = sanitize_name(output_file) 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: if progress is None:
output = base_join(context.output_path, output_file) output = base_join(context.output_path, output_file)
if path.exists(output): if path.exists(output):
return ready_reply(True) return ready_reply(ready=True)
else: else:
return ready_reply( return ready_reply(
True, error=True ready=True,
failed=True,
) # is a missing image really an error? yes will display the retry button ) # is a missing image really an error? yes will display the retry button
return ready_reply( return ready_reply(
progress.finished, ready=progress.finished,
progress=progress.progress, progress=progress.progress,
error=progress.error, failed=progress.error,
cancel=progress.cancel, cancelled=progress.cancel,
) )

View File

@ -6,8 +6,8 @@ class ProgressCommand:
job: str job: str
finished: bool finished: bool
progress: int progress: int
cancel: bool cancelled: bool
error: bool failed: bool
def __init__( def __init__(
self, self,
@ -15,15 +15,15 @@ class ProgressCommand:
device: str, device: str,
finished: bool, finished: bool,
progress: int, progress: int,
cancel: bool = False, cancelled: bool = False,
error: bool = False, failed: bool = False,
): ):
self.job = job self.job = job
self.device = device self.device = device
self.finished = finished self.finished = finished
self.progress = progress self.progress = progress
self.cancel = cancel self.cancelled = cancelled
self.error = error self.failed = failed
class JobCommand: class JobCommand:

View File

@ -232,7 +232,7 @@ class DevicePoolExecutor:
for job in self.pending_jobs: for job in self.pending_jobs:
if job.name == key: if job.name == key:
logger.debug("checking status for pending job: %s", 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) logger.trace("checking status for unknown job: %s", key)
return (False, None) return (False, None)
@ -365,25 +365,39 @@ class DevicePoolExecutor:
self.pending_jobs.append(job) self.pending_jobs.append(job)
self.pending[device].put(job, block=False) 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 = [ history = [
( (
name, name,
job.progress, job.progress,
False,
job.finished, job.finished,
job.cancel, job.cancelled,
job.error, job.failed,
) )
for name, job in self.running_jobs.items() 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( history.extend(
[ [
( (
job.job, job.job,
job.progress, job.progress,
False,
job.finished, job.finished,
job.cancel, job.cancelled,
job.error, job.failed,
) )
for job in self.finished_jobs for job in self.finished_jobs
] ]