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

39 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 16:52 +0100

1from portality import models 

2from portality.core import app 

3 

4from portality.tasks.redis_huey import main_queue, schedule 

5from portality.decorators import write_required 

6 

7from portality.background import BackgroundTask, BackgroundApi, BackgroundException 

8from portality.bll.doaj import DOAJ 

9 

10class JournalCSVBackgroundTask(BackgroundTask): 

11 

12 __action__ = "journal_csv" 

13 

14 def run(self): 

15 """ 

16 Execute the task as specified by the background_job  

17 :return: 

18 """ 

19 job = self.background_job 

20 

21 journalService = DOAJ.journalService() 

22 url, action_register = journalService.csv() 

23 for ar in action_register: 

24 job.add_audit_message(ar) 

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

26 

27 def cleanup(self): 

28 """ 

29 Cleanup after a successful OR failed run of the task 

30 :return: 

31 """ 

32 pass 

33 

34 @classmethod 

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

36 """ 

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

38 or fail with a suitable exception 

39 

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

41 :return: a BackgroundJob instance representing this task 

42 """ 

43 # prepare a job record 

44 job = models.BackgroundJob() 

45 job.user = username 

46 job.action = cls.__action__ 

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=10) 

59 

60 

61@main_queue.periodic_task(schedule("journal_csv")) 

62@write_required(script=True) 

63def scheduled_journal_csv(): 

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

65 job = JournalCSVBackgroundTask.prepare(user) 

66 JournalCSVBackgroundTask.submit(job) 

67 

68 

69@main_queue.task() 

70@write_required(script=True) 

71def journal_csv(job_id): 

72 job = models.BackgroundJob.pull(job_id) 

73 task = JournalCSVBackgroundTask(job) 

74 BackgroundApi.execute(task)