Coverage for portality/tasks/sitemap.py: 79%

42 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-22 15:59 +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 

8 

9from portality.bll.doaj import DOAJ 

10 

11class SitemapBackgroundTask(BackgroundTask): 

12 """ 

13 ~~Sitemap:BackgroundTask~~ 

14 """ 

15 __action__ = "sitemap" 

16 

17 def run(self): 

18 """ 

19 Execute the task as specified by the background_jon 

20 :return: 

21 """ 

22 job = self.background_job 

23 

24 # ~~-> Sitemap:Feature~~ 

25 siteService = DOAJ.siteService() 

26 url, action_register = siteService.sitemap() 

27 for ar in action_register: 

28 job.add_audit_message(ar) 

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

30 

31 def cleanup(self): 

32 """ 

33 Cleanup after a successful OR failed run of the task 

34 :return: 

35 """ 

36 pass 

37 

38 @classmethod 

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

40 """ 

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

42 or fail with a suitable exception 

43 

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

45 :return: a BackgroundJob instance representing this task 

46 """ 

47 

48 base_url = app.config.get("BASE_URL") 

49 if base_url is None: 

50 raise BackgroundException("BASE_URL must be set in configuration before we can generate a sitemap") 

51 

52 # first prepare a job record 

53 job = models.BackgroundJob() 

54 job.user = username 

55 job.action = cls.__action__ 

56 return job 

57 

58 @classmethod 

59 def submit(cls, background_job): 

60 """ 

61 Submit the specified BackgroundJob to the background queue 

62 

63 :param background_job: the BackgroundJob instance 

64 :return: 

65 """ 

66 background_job.save() 

67 generate_sitemap.schedule(args=(background_job.id,), delay=10) 

68 

69 

70@main_queue.periodic_task(schedule("sitemap")) 

71@write_required(script=True) 

72def scheduled_sitemap(): 

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

74 job = SitemapBackgroundTask.prepare(user) 

75 SitemapBackgroundTask.submit(job) 

76 

77 

78@main_queue.task() 

79@write_required(script=True) 

80def generate_sitemap(job_id): 

81 job = models.BackgroundJob.pull(job_id) 

82 task = SitemapBackgroundTask(job) 

83 BackgroundApi.execute(task)