-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
334 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-Jobs is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
from functools import partial | ||
|
||
|
||
class RegisteredTask: | ||
|
||
arguments_schema = None | ||
task = None | ||
id = None | ||
title = None | ||
description = None | ||
@classmethod | ||
def factory(cls, job_cls_name, arguments_schema, id_, task, description, title, attrs=None): | ||
"""Create a new instance of a job.""" | ||
if not attrs: | ||
attrs = {} | ||
return type( | ||
job_cls_name, | ||
(RegisteredTask,), | ||
dict( | ||
id=id_, | ||
arguments_schema=arguments_schema, | ||
task=task, | ||
description=description, | ||
title=title, | ||
**attrs | ||
), | ||
) | ||
|
||
@classmethod | ||
def build_task_arguments(cls, job_obj, since=None, custom_args=None, **kwargs): | ||
if custom_args: | ||
return custom_args | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-Jobs is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
class JobsRegistry: | ||
"""A simple class to register jobs.""" | ||
|
||
def __init__(self): | ||
"""Initialize the registry.""" | ||
self._jobs = {} | ||
|
||
def register(self, job_instance, job_id=None): | ||
"""Register a new job instance.""" | ||
if job_id is None: | ||
job_id = job_instance.id | ||
if job_id in self._jobs: | ||
raise RuntimeError( | ||
f"Job with job id '{job_id}' is already registered." | ||
) | ||
self._jobs[job_id] = job_instance | ||
|
||
def get(self, job_id): | ||
"""Get a job for a given job_id.""" | ||
return self._jobs[job_id] | ||
|
||
def get_job_id(self, instance): | ||
"""Get the service id for a specific instance.""" | ||
for job_id, job_instance in self._jobs.items(): | ||
if instance == job_instance: | ||
return job_id | ||
raise KeyError("Job not found in registry.") | ||
|
||
def all_registered_jobs(self): | ||
"""Return a list of available tasks.""" | ||
return self._jobs | ||
|
||
def all_arguments(self): | ||
return [task.arguments_schema for task_id, task in self._jobs.items()] | ||
|
||
def registered_schemas(self): | ||
schemas = {} | ||
for id_, registered_task in self._jobs.items(): | ||
schema = registered_task.arguments_schema | ||
if schema: | ||
schemas[f"{schema.__name__}API"] = schema | ||
return schemas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.