Coverage for portality / tasks / journal_csv.py: 53%

36 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_short_queue as queue 

7 

8 

9class JournalCSVBackgroundTask(BackgroundTask): 

10 

11 __action__ = "journal_csv" 

12 

13 def run(self): 

14 """ 

15 Execute the task as specified by the background_job  

16 :return: 

17 """ 

18 

19 def logger(msg): 

20 self.background_job.add_audit_message(msg) 

21 

22 job = self.background_job 

23 

24 journalService = DOAJ.journalService() 

25 jc = journalService.csv(logger=logger) 

26 

27 job.add_audit_message("CSV generated; will be served from {y}".format(y=jc.url)) 

28 

29 def cleanup(self): 

30 """ 

31 Cleanup after a successful OR failed run of the task 

32 :return: 

33 """ 

34 pass 

35 

36 @classmethod 

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

38 """ 

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

40 or fail with a suitable exception 

41 

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

43 :return: a BackgroundJob instance representing this task 

44 """ 

45 # prepare a job record 

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

47 return job 

48 

49 @classmethod 

50 def submit(cls, background_job): 

51 """ 

52 Submit the specified BackgroundJob to the background queue 

53 

54 :param background_job: the BackgroundJob instance 

55 :return: 

56 """ 

57 background_job.save() 

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

59 

60 

61huey_helper = JournalCSVBackgroundTask.create_huey_helper(queue) 

62 

63 

64@huey_helper.register_schedule 

65def scheduled_journal_csv(): 

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

67 job = JournalCSVBackgroundTask.prepare(user) 

68 JournalCSVBackgroundTask.submit(job) 

69 

70 

71@huey_helper.register_execute(is_load_config=False) 

72def journal_csv(job_id): 

73 job = models.BackgroundJob.pull(job_id) 

74 task = JournalCSVBackgroundTask(job) 

75 BackgroundApi.execute(task)