2023-10-23 09:33:55 -07:00
|
|
|
import logging
|
2022-12-05 09:38:37 -08:00
|
|
|
from typing import cast
|
2022-11-09 21:29:33 -08:00
|
|
|
|
|
|
|
from django.apps import apps
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2022-11-18 07:28:15 -08:00
|
|
|
from core.models import Config
|
2022-11-09 21:29:33 -08:00
|
|
|
from stator.models import StatorModel
|
|
|
|
from stator.runner import StatorRunner
|
|
|
|
|
2023-11-16 09:27:20 -08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-11-09 21:29:33 -08:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2022-11-19 09:20:13 -08:00
|
|
|
help = "Runs a Stator runner"
|
2022-11-09 21:29:33 -08:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2022-11-13 17:42:47 -08:00
|
|
|
parser.add_argument(
|
|
|
|
"--concurrency",
|
|
|
|
"-c",
|
|
|
|
type=int,
|
2023-07-16 23:18:00 -07:00
|
|
|
default=15,
|
2022-11-13 17:42:47 -08:00
|
|
|
help="How many tasks to run at once",
|
|
|
|
)
|
2022-11-19 09:20:13 -08: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 10:03:52 -08:00
|
|
|
parser.add_argument(
|
|
|
|
"--run-for",
|
|
|
|
"-r",
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
help="How long to run for before exiting (defaults to infinite)",
|
|
|
|
)
|
2023-01-10 19:44:45 -08:00
|
|
|
parser.add_argument(
|
|
|
|
"--exclude",
|
|
|
|
"-x",
|
|
|
|
type=str,
|
|
|
|
action="append",
|
|
|
|
help="Model labels that should not be processed",
|
|
|
|
)
|
2022-11-09 21:29:33 -08:00
|
|
|
parser.add_argument("model_labels", nargs="*", type=str)
|
|
|
|
|
2022-11-19 09:20:13 -08:00
|
|
|
def handle(
|
|
|
|
self,
|
2022-12-05 09:38:37 -08:00
|
|
|
model_labels: list[str],
|
2022-11-19 09:20:13 -08:00
|
|
|
concurrency: int,
|
|
|
|
liveness_file: str,
|
|
|
|
schedule_interval: int,
|
2022-11-27 10:03:52 -08:00
|
|
|
run_for: int,
|
2023-01-10 19:44:45 -08:00
|
|
|
exclude: list[str],
|
2022-11-19 09:20:13 -08:00
|
|
|
*args,
|
|
|
|
**options
|
|
|
|
):
|
2022-11-18 07:28:15 -08:00
|
|
|
# Cache system config
|
|
|
|
Config.system = Config.load_system()
|
2023-11-12 15:48:44 -08:00
|
|
|
logging.basicConfig(
|
|
|
|
format="[%(asctime)s] %(levelname)8s - %(message)s",
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
|
|
level=logging.INFO,
|
|
|
|
force=True,
|
|
|
|
)
|
2022-11-09 21:29:33 -08:00
|
|
|
# Resolve the models list into names
|
|
|
|
models = cast(
|
2022-12-05 09:38:37 -08:00
|
|
|
list[type[StatorModel]],
|
2022-11-09 21:29:33 -08:00
|
|
|
[apps.get_model(label) for label in model_labels],
|
|
|
|
)
|
2023-01-10 19:44:45 -08:00
|
|
|
excluded = cast(
|
|
|
|
list[type[StatorModel]],
|
|
|
|
[apps.get_model(label) for label in (exclude or [])],
|
|
|
|
)
|
2022-11-09 21:29:33 -08:00
|
|
|
if not models:
|
|
|
|
models = StatorModel.subclasses
|
2023-01-10 19:44:45 -08:00
|
|
|
models = [model for model in models if model not in excluded]
|
2023-11-16 09:27:20 -08:00
|
|
|
logger.info(
|
2023-10-23 09:33:55 -07:00
|
|
|
"Running for models: " + " ".join(m._meta.label_lower for m in models)
|
|
|
|
)
|
2022-11-09 21:29:33 -08:00
|
|
|
# Run a runner
|
2022-11-19 09:20:13 -08:00
|
|
|
runner = StatorRunner(
|
|
|
|
models,
|
|
|
|
concurrency=concurrency,
|
|
|
|
liveness_file=liveness_file,
|
|
|
|
schedule_interval=schedule_interval,
|
2022-11-27 10:03:52 -08:00
|
|
|
run_for=run_for,
|
2022-11-19 09:20:13 -08:00
|
|
|
)
|
2023-01-16 10:15:28 -08:00
|
|
|
try:
|
2023-07-07 14:14:06 -07:00
|
|
|
runner.run()
|
2023-01-16 10:15:28 -08:00
|
|
|
except KeyboardInterrupt:
|
2023-11-16 09:27:20 -08:00
|
|
|
logger.critical("Ctrl-C received")
|