2022-11-10 05:29:33 +00:00
|
|
|
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
|
|
|
|
|
2022-11-18 15:28:15 +00:00
|
|
|
from core.models import Config
|
2022-11-10 05:29:33 +00:00
|
|
|
from stator.models import StatorModel
|
|
|
|
from stator.runner import StatorRunner
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2022-11-19 17:20:13 +00:00
|
|
|
help = "Runs a Stator runner"
|
2022-11-10 05:29:33 +00:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2022-11-14 01:42:47 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--concurrency",
|
|
|
|
"-c",
|
|
|
|
type=int,
|
|
|
|
default=30,
|
|
|
|
help="How many tasks to run at once",
|
|
|
|
)
|
2022-11-19 17:20:13 +00:00
|
|
|
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",
|
|
|
|
)
|
2022-11-27 18:03:52 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--run-for",
|
|
|
|
"-r",
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
help="How long to run for before exiting (defaults to infinite)",
|
|
|
|
)
|
2022-11-10 05:29:33 +00:00
|
|
|
parser.add_argument("model_labels", nargs="*", type=str)
|
|
|
|
|
2022-11-19 17:20:13 +00:00
|
|
|
def handle(
|
|
|
|
self,
|
|
|
|
model_labels: List[str],
|
|
|
|
concurrency: int,
|
|
|
|
liveness_file: str,
|
|
|
|
schedule_interval: int,
|
2022-11-27 18:03:52 +00:00
|
|
|
run_for: int,
|
2022-11-19 17:20:13 +00:00
|
|
|
*args,
|
|
|
|
**options
|
|
|
|
):
|
2022-11-18 15:28:15 +00:00
|
|
|
# Cache system config
|
|
|
|
Config.system = Config.load_system()
|
2022-11-10 05:29:33 +00:00
|
|
|
# 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
|
2022-11-19 17:20:13 +00:00
|
|
|
runner = StatorRunner(
|
|
|
|
models,
|
|
|
|
concurrency=concurrency,
|
|
|
|
liveness_file=liveness_file,
|
|
|
|
schedule_interval=schedule_interval,
|
2022-11-27 18:03:52 +00:00
|
|
|
run_for=run_for,
|
2022-11-19 17:20:13 +00:00
|
|
|
)
|
2022-11-10 05:29:33 +00:00
|
|
|
async_to_sync(runner.run)()
|