Coverage for portality / tasks / ris_export.py: 45%

49 statements  

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

1from portality import models 

2from portality.background import BackgroundTask, BackgroundApi 

3from portality.bll.doaj import DOAJ 

4from portality.core import app 

5from portality.tasks.helpers import background_helper 

6from portality.tasks.redis_huey import scheduled_long_queue as queue 

7from portality.bll.services.export import RISExportReporter 

8 

9class RISExportBackgroundTaskReporter(RISExportReporter): 

10 def __init__(self, job): 

11 super().__init__() 

12 self._job = job 

13 

14 def processed(self, total_so_far): 

15 super().processed(total_so_far) 

16 if self._processed % 10000 == 0: 

17 self.msg(f"Processed {self._processed} articles so far; {self._loaded} RIS exports generated.") 

18 

19 def msg(self, message): 

20 self._job.add_audit_message(message) 

21 

22 

23class RISExportBackgroundTask(BackgroundTask): 

24 

25 __action__ = "ris_export" 

26 

27 def run(self): 

28 """ 

29 Execute the task as specified by the background_job  

30 :return: 

31 """ 

32 job = self.background_job 

33 force_update = self.get_param(job.params, "force", False) 

34 

35 exportSvc = DOAJ.exportService() 

36 

37 job.add_audit_message("Starting RIS Update") 

38 exportSvc.bulk_generate_ris(force_update=force_update, reporter=RISExportBackgroundTaskReporter(job)) 

39 job.add_audit_message("RIS Entries updated") 

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 # prepare a job record 

58 params = {} 

59 cls.set_param(params, "force", kwargs.get("force", False)) 

60 job = background_helper.create_job(username, cls.__action__, queue_id=huey_helper.queue_id, params=params) 

61 return job 

62 

63 @classmethod 

64 def submit(cls, background_job): 

65 """ 

66 Submit the specified BackgroundJob to the background queue 

67 

68 :param background_job: the BackgroundJob instance 

69 :return: 

70 """ 

71 background_job.save() 

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

73 

74 

75huey_helper = RISExportBackgroundTask.create_huey_helper(queue) 

76 

77 

78@huey_helper.register_schedule 

79def scheduled_ris_export(): 

80 user = app.config.get("SYSTEM_USERNAME") 

81 job = RISExportBackgroundTask.prepare(user) 

82 RISExportBackgroundTask.submit(job) 

83 

84 

85@huey_helper.register_execute(is_load_config=False) 

86def ris_export(job_id): 

87 job = models.BackgroundJob.pull(job_id) 

88 task = RISExportBackgroundTask(job) 

89 BackgroundApi.execute(task)