diff --git a/src/elody/job.py b/src/elody/job.py index 2cf7a62..8a239ba 100644 --- a/src/elody/job.py +++ b/src/elody/job.py @@ -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"] @@ -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): @@ -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) diff --git a/src/elody/loader.py b/src/elody/loader.py index d717504..777deb1 100644 --- a/src/elody/loader.py +++ b/src/elody/loader.py @@ -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, @@ -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: