repos

blog.bythewood.me-wagtail

mirror archived upstream

Self-hostable blog for developers built on Wagtail and Django, with webpack, Bootstrap, and CodeMirror.

blogbootstrapdjangodockerhandcodedpythonself-hostedwagtailwebpack

1.1 KB · 34 lines · Python Raw History
 1import time
 2
 3from django.core.management.base import BaseCommand
 4from django.core.management import call_command
 5from django.utils import timezone
 6
 7from ...models import ScheduledTask
 8
 9
10class Command(BaseCommand):
11    def handle(self, *args, **options):
12        self.stdout.write('[Scheduler] Starting scheduler...')
13
14        while True:
15            tasks = ScheduledTask.objects.all()
16
17            for task in tasks:
18                if task.should_run():
19                    now = timezone.now()
20                    timestamp = now.strftime('%d/%b/%Y %H:%M:%S %z')
21                    self.stdout.write(f'[Scheduler] [{timestamp}] Running task {task.management_command}')
22                    call_command(task.management_command)
23                    task.last_run_at = now
24                    task.next_run_at = task.get_next_run_at(now)
25                    task.save()
26
27            self.stdout.write('[Scheduler] Sleeping scheduler for 60 seconds...')
28
29            try:
30                time.sleep(60)
31            except KeyboardInterrupt:
32                self.stdout.write('[Scheduler] Stopping scheduler...')
33                break