Coverage for portality / tasks / sitemap.py: 77%
39 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-05 00:09 +0100
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-05 00:09 +0100
1from portality import models
2from portality.background import BackgroundTask, BackgroundApi, BackgroundException
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
9class SitemapBackgroundTask(BackgroundTask):
10 """
11 ~~Sitemap:BackgroundTask~~
12 """
13 __action__ = "sitemap"
15 def run(self):
16 """
17 Execute the task as specified by the background_jon
18 :return:
19 """
20 job = self.background_job
22 # ~~-> Sitemap:Feature~~
23 siteService = DOAJ.siteService()
24 index_urls, action_register = siteService.sitemap()
25 for ar in action_register:
26 job.add_audit_message(ar)
27 job.add_audit_message("Sitemap generated; will be served from {y}".format(y=", ".join(index_urls)))
29 def cleanup(self):
30 """
31 Cleanup after a successful OR failed run of the task
32 :return:
33 """
34 pass
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
42 :param kwargs: arbitrary keyword arguments pertaining to this task type
43 :return: a BackgroundJob instance representing this task
44 """
46 base_url = app.config.get("BASE_URL")
47 if base_url is None:
48 raise BackgroundException("BASE_URL must be set in configuration before we can generate a sitemap")
50 # first prepare a job record
51 job = background_helper.create_job(username, cls.__action__,
52 queue_id=huey_helper.queue_id, )
53 return job
55 @classmethod
56 def submit(cls, background_job):
57 """
58 Submit the specified BackgroundJob to the background queue
60 :param background_job: the BackgroundJob instance
61 :return:
62 """
63 background_job.save()
64 generate_sitemap.schedule(args=(background_job.id,), delay=app.config.get('HUEY_ASYNC_DELAY', 10))
67huey_helper = SitemapBackgroundTask.create_huey_helper(queue)
70@huey_helper.register_schedule
71def scheduled_sitemap():
72 user = app.config.get("SYSTEM_USERNAME")
73 job = SitemapBackgroundTask.prepare(user)
74 SitemapBackgroundTask.submit(job)
77@huey_helper.register_execute(is_load_config=False)
78def generate_sitemap(job_id):
79 job = models.BackgroundJob.pull(job_id)
80 task = SitemapBackgroundTask(job)
81 BackgroundApi.execute(task)