Coverage for portality / tasks / journal_autochecks.py: 41%

41 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-04 09:41 +0100

1from portality import models 

2from portality.core import app 

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 

7 

8####################################### 

9# NOTE: this background task is currently not in use, it is prepped for being used with 

10# autocheck-on-demand from the administrators via the user interface, which is not yet implemented 

11# 

12# When we implement that functionality we will also need to implement tests for this 

13####################################### 

14 

15 

16class JournalAutochecks(BackgroundTask): 

17 

18 __action__ = "journal_autochecks" 

19 

20 def run(self): 

21 """ 

22 Execute the task as specified by the background_jon 

23 :return: 

24 """ 

25 job = self.background_job 

26 params = job.params 

27 journal_id = self.get_param(params, "journal", False) 

28 

29 if journal_id is False: 

30 job.add_audit_message("Journal ID must be provided when creating the job") 

31 job.fail() 

32 return 

33 

34 journal = models.Journal.pull(journal_id) 

35 

36 job.add_audit_message("Running journal autocheck for journal with id {id}".format(id=journal_id)) 

37 

38 annoSvc = DOAJ.autochecksService() 

39 annoSvc.autocheck_journal(journal, logger=lambda x: job.add_audit_message(x)) 

40 

41 def cleanup(self): 

42 """ 

43 Cleanup after a successful OR failed run of the task 

44 :return: 

45 """ 

46 pass 

47 

48 @classmethod 

49 def prepare(cls, username, **kwargs): 

50 """ 

51 Take an arbitrary set of keyword arguments and return an instance of a BackgroundJob, 

52 or fail with a suitable exception 

53 

54 :param kwargs: arbitrary keyword arguments pertaining to this task type 

55 :return: a BackgroundJob instance representing this task 

56 """ 

57 

58 journal_id = kwargs.get("journal", False) 

59 

60 if not journal_id: 

61 raise BackgroundException("'journal' ID must be provided to prepare function") 

62 

63 params = {} 

64 cls.set_param(params, "journal", journal_id) 

65 

66 # first prepare a job record 

67 job = background_helper.create_job(username=username, 

68 action=cls.__action__, 

69 params=params, 

70 queue_id=huey_helper.queue_id, ) 

71 

72 return job 

73 

74 @classmethod 

75 def submit(cls, background_job): 

76 """ 

77 Submit the specified BackgroundJob to the background queue 

78 

79 :param background_job: the BackgroundJob instance 

80 :return: 

81 """ 

82 background_job.save() 

83 journal_autochecks.schedule(args=(background_job.id,), delay=app.config.get('HUEY_ASYNC_DELAY', 10)) 

84 

85 

86huey_helper = JournalAutochecks.create_huey_helper(queue) 

87 

88 

89@huey_helper.register_execute(is_load_config=True) 

90def journal_autochecks(job_id): 

91 job = models.BackgroundJob.pull(job_id) 

92 task = JournalAutochecks(job) 

93 BackgroundApi.execute(task)