Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Generalize job handling to handle different resource types
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-christensen committed Aug 11, 2019
1 parent 8c5c0d4 commit 5db29aa
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,28 @@ def start_job(resource_value: str, resource_type: str):
)


def handle_job(resource_id: str, resource_type: str) -> dict:
try:
job = Job.fetch(resource_id, connection=Redis())
except NoSuchJobError:
job = start_job(resource_id, resource_type)
return {"Status": "Started"}

if job.result:
response = {"Status": "Finished", "url": job.result}
elif job.is_failed:
response = {
"Status": "Failed. Resubmit to retry.",
"error": job.meta["progress"],
}
job.delete()
else:
response = {"Status": "Working"}
return response


@app.route("/api/v1/resource")
def service():
def process_resource():
resource_id = request.args.get("id")
resource_type = "scta"
if not resource_id:
Expand All @@ -57,22 +77,7 @@ def service():
}
return jsonify(error_message)

try:
job = Job.fetch(resource_id, connection=Redis())

if job.result:
response = {"Status": "Finished", "url": job.result}
elif job.is_failed:
response = {
"Status": "Failed. Resubmit to retry.",
"error": job.meta["progress"],
}
job.delete()
else:
response = {"Status": "Working"}
except NoSuchJobError:
job = start_job(resource_id, resource_type=resource_type)
response = {"Status": "Started"}
response = handle_job(resource_id, resource_type)

return jsonify(response)

Expand Down

0 comments on commit 5db29aa

Please sign in to comment.