Coverage for portality / tasks / application_autochecks.py: 84%
50 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-04 09:41 +0100
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-04 09:41 +0100
1from portality import constants
2from portality import models
3from portality.background import BackgroundTask, BackgroundApi, BackgroundException
4from portality.tasks.helpers import background_helper
5from portality.tasks.redis_huey import events_queue as queue
6from portality.bll import DOAJ
9class ApplicationAutochecks(BackgroundTask):
11 __action__ = "application_autochecks"
13 def run(self):
14 """
15 Execute the task as specified by the background_jon
16 :return:
17 """
18 job = self.background_job
19 params = job.params
20 app_id = self.get_param(params, "application", False)
21 status_on_complete = self.get_param(params, "status_on_complete", "")
23 if app_id is False:
24 job.add_audit_message("Application ID must be provided when creating the job")
25 job.fail()
26 return
28 application = models.Application.pull(app_id)
29 if application is None:
30 job.add_audit_message("Application id {id} is not present, no further action required".format(id=app_id))
31 return
33 job.add_audit_message("Running application autocheck for application with id {id}".format(id=app_id))
35 annoSvc = DOAJ.autochecksService()
36 annoSvc.autocheck_application(application, created_date=job.created_date, logger=lambda x: job.add_audit_message(x))
38 if status_on_complete != "" and status_on_complete in constants.APPLICATION_STATUSES_ALL:
39 job.add_audit_message("Setting status to {x}".format(x=status_on_complete))
40 application.set_application_status(status_on_complete)
41 # Note: we have not locked the application
42 application.save()
44 def cleanup(self):
45 """
46 Cleanup after a successful OR failed run of the task
47 :return:
48 """
49 pass
51 @classmethod
52 def prepare(cls, username, **kwargs):
53 """
54 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob,
55 or fail with a suitable exception
57 :param kwargs: arbitrary keyword arguments pertaining to this task type
58 :return: a BackgroundJob instance representing this task
59 """
61 app_id = kwargs.get("application", False)
62 status_on_complete = kwargs.get("status_on_complete", "")
64 if not app_id:
65 raise BackgroundException("'application' ID must be provided to prepare function")
67 params = {}
68 cls.set_param(params, "application", app_id)
69 cls.set_param(params, "status_on_complete", status_on_complete)
71 # first prepare a job record
72 job = background_helper.create_job(username=username,
73 action=cls.__action__,
74 params=params,
75 queue_id=huey_helper.queue_id, )
77 return job
79 @classmethod
80 def submit(cls, background_job):
81 """
82 Submit the specified BackgroundJob to the background queue
84 :param background_job: the BackgroundJob instance
85 :return:
86 """
87 background_helper.submit_by_background_job(background_job, application_autochecks)
90huey_helper = ApplicationAutochecks.create_huey_helper(queue)
93@huey_helper.register_execute(is_load_config=True)
94def application_autochecks(job_id):
95 job = models.BackgroundJob.pull(job_id)
96 task = ApplicationAutochecks(job)
97 BackgroundApi.execute(task)