From e35c071c36df43d1ce9e96d134c0f69aca5c8cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eva=20Mill=C3=A1n?= Date: Mon, 28 Aug 2023 12:03:24 +0200 Subject: [PATCH] [api] Add function to create scheduled tasks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the 'add_scheduled_task' API function. Signed-off-by: Eva Millán --- sortinghat/core/api.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/sortinghat/core/api.py b/sortinghat/core/api.py index caeed25e..c892113f 100644 --- a/sortinghat/core/api.py +++ b/sortinghat/core/api.py @@ -35,6 +35,7 @@ add_individual as add_individual_db, add_identity as add_identity_db, add_organization as add_organization_db, + add_scheduled_task as add_scheduled_task_db, add_team as add_team_db, add_domain as add_domain_db, delete_individual as delete_individual_db, @@ -1387,6 +1388,45 @@ def _move_enrollments(trxl, from_org, to_org): return target +@atomic_using_tenant +def add_scheduled_task(ctx, job_type, interval=None, args=None, job_id=None): + """Add an scheduled task to the registry. + + This function adds a new task to the registry. + + As a result, the function returns a new `ScheduledTask` object. + + :param ctx: context from where this method is called. + :param job_type: name of the job to be scheduled + :param interval: period of executions, in minutes. None to disable + :param args: specific arguments for the job + :param job_id: current job running the task + + :returns: a new ScheduledTask + """ + if not job_type: + raise InvalidValueError(msg="'job_type' cannot be None") + + trxl = TransactionsLog.open('add_scheduled_task', ctx) + + try: + task = add_scheduled_task_db(trxl, + job_type=job_type, + interval=interval, + args=args, + job_id=job_id) + except ValueError as e: + raise InvalidValueError(msg=str(e)) + except AlreadyExistsError as exc: + raise exc + + trxl.close() + + logger.info(f"'{job_type}' task created") + + return task + + @atomic_using_tenant def update_scheduled_task(ctx, task_id, **kwargs): """Update an scheduled task.