summaryrefslogtreecommitdiffstats
path: root/stator/management/commands/runstator.py
blob: 4d525208e9c6361ccf0108a54e02df5ceb9a3afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from typing import List, Type, cast

from asgiref.sync import async_to_sync
from django.apps import apps
from django.core.management.base import BaseCommand

from core.models import Config
from stator.models import StatorModel
from stator.runner import StatorRunner


class Command(BaseCommand):
    help = "Runs a Stator runner"

    def add_arguments(self, parser):
        parser.add_argument(
            "--concurrency",
            "-c",
            type=int,
            default=30,
            help="How many tasks to run at once",
        )
        parser.add_argument(
            "--liveness-file",
            type=str,
            default=None,
            help="A file to touch at least every 30 seconds to say the runner is alive",
        )
        parser.add_argument(
            "--schedule-interval",
            "-s",
            type=int,
            default=30,
            help="How often to run cleaning and scheduling",
        )
        parser.add_argument(
            "--run-for",
            "-r",
            type=int,
            default=0,
            help="How long to run for before exiting (defaults to infinite)",
        )
        parser.add_argument("model_labels", nargs="*", type=str)

    def handle(
        self,
        model_labels: List[str],
        concurrency: int,
        liveness_file: str,
        schedule_interval: int,
        run_for: int,
        *args,
        **options
    ):
        # Cache system config
        Config.system = Config.load_system()
        # Resolve the models list into names
        models = cast(
            List[Type[StatorModel]],
            [apps.get_model(label) for label in model_labels],
        )
        if not models:
            models = StatorModel.subclasses
        print("Running for models: " + " ".join(m._meta.label_lower for m in models))
        # Run a runner
        runner = StatorRunner(
            models,
            concurrency=concurrency,
            liveness_file=liveness_file,
            schedule_interval=schedule_interval,
            run_for=run_for,
        )
        async_to_sync(runner.run)()