Skip to content

Commit

Permalink
feat: improve job system
Browse files Browse the repository at this point in the history
  • Loading branch information
eray-inuits committed Aug 23, 2024
1 parent 12de990 commit dbf0a9a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
68 changes: 46 additions & 22 deletions src/elody/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,48 @@
_post_crud_hook = _config.crud()["post_crud_hook"]


def start_job(name, type, *, get_rabbit, parent_id=None, get_user_context=None) -> str:
def start_job(
name,
job_type,
*,
get_rabbit,
get_user_context=None,
user_email=None,
parent_id=None,
id_of_document_job_was_initiated_for=None,
type_of_document_job_was_initiated_for=None,
) -> str:
relations = []
if parent_id:
relations.append({"key": parent_id, "type": "hasParentJob"})
if id_of_document_job_was_initiated_for and type_of_document_job_was_initiated_for:
relations.append(
{"key": id_of_document_job_was_initiated_for, "type": "isJobOf"}
)

job = _create(
{
"metadata": [
{"key": "name", "value": name},
{"key": "status", "value": "running"},
{"key": "type", "value": type},
{"key": "type", "value": job_type},
],
"relations": (
[{"key": parent_id, "type": "hasParentJob"}] if parent_id else []
),
"relations": relations,
"type": "job",
},
get_user_context=get_user_context,
get_user_context=get_user_context
or (lambda: type("UserContext", (object,), {"email": user_email})()),
)
del job["computed_values"]["created_at"]

_post_crud_hook(
crud="create", document=job, parent_id=parent_id, get_rabbit=get_rabbit
)
__patch_document_job_was_initiated_for(
id_of_document_job_was_initiated_for,
type_of_document_job_was_initiated_for,
get_rabbit,
)
return job["_id"]


Expand All @@ -32,30 +56,21 @@ def finish_job(
id_of_document_job_was_initiated_for=None,
type_of_document_job_was_initiated_for=None,
*,
get_rabbit
get_rabbit,
):
document = {
"id": id,
"patch": {
"metadata": [{"key": "status", "value": "finished"}],
"relations": (
[{"key": id_of_document_job_was_initiated_for, "type": "isJobOf"}]
if id_of_document_job_was_initiated_for
else []
),
"relations": ([] if id_of_document_job_was_initiated_for else []),
},
}
_post_crud_hook(crud="update", document=document, get_rabbit=get_rabbit)

if id_of_document_job_was_initiated_for and type_of_document_job_was_initiated_for:
document = {
"document_info_job_was_initiated_for": {
"id": id_of_document_job_was_initiated_for,
"type": type_of_document_job_was_initiated_for,
},
"patch": {"relations": [{"key": id, "type": "hasJob"}]},
}
_post_crud_hook(crud="update", document=document, get_rabbit=get_rabbit)
__patch_document_job_was_initiated_for(
id_of_document_job_was_initiated_for,
type_of_document_job_was_initiated_for,
get_rabbit,
)


def fail_job(id, exception_message, *, get_rabbit):
Expand All @@ -69,3 +84,12 @@ def fail_job(id, exception_message, *, get_rabbit):
},
}
_post_crud_hook(crud="update", document=document, get_rabbit=get_rabbit)


def __patch_document_job_was_initiated_for(id, type, get_rabbit):
if id and type:
document = {
"document_info_job_was_initiated_for": {"id": id, "type": type},
"patch": {"relations": [{"key": id, "type": "hasJob"}]},
}
_post_crud_hook(crud="update", document=document, get_rabbit=get_rabbit)
4 changes: 3 additions & 1 deletion src/elody/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import os

from elody.policies.permission_handler import set_permissions
from importlib import import_module
from inuits_policy_based_auth.exceptions import (
PolicyFactoryException,
Expand All @@ -19,7 +18,10 @@ def load_apps(flask_app, logger):

def load_policies(policy_factory, logger, permissions={}):
if permissions:
from elody.policies.permission_handler import set_permissions

set_permissions(permissions)

apps = util.read_json_as_dict(os.getenv("APPS_MANIFEST", ""), logger)
for app in apps:
try:
Expand Down

0 comments on commit dbf0a9a

Please sign in to comment.