2022-11-09 06:06:29 +00:00
|
|
|
import asyncio
|
|
|
|
import datetime
|
2023-01-16 18:15:28 +00:00
|
|
|
import os
|
|
|
|
import signal
|
2022-11-09 06:06:29 +00:00
|
|
|
import time
|
2022-11-10 06:48:31 +00:00
|
|
|
import traceback
|
2022-11-09 06:06:29 +00:00
|
|
|
import uuid
|
|
|
|
|
2023-02-04 18:46:38 +00:00
|
|
|
from asgiref.sync import async_to_sync, sync_to_async
|
2022-12-20 08:02:35 +00:00
|
|
|
from django.conf import settings
|
2022-11-09 06:06:29 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-12-05 01:08:23 +00:00
|
|
|
from core import exceptions, sentry
|
2022-11-23 04:06:21 +00:00
|
|
|
from core.models import Config
|
2022-12-15 19:26:17 +00:00
|
|
|
from stator.models import StatorModel, Stats
|
2022-11-09 06:06:29 +00:00
|
|
|
|
|
|
|
|
2023-02-04 04:51:24 +00:00
|
|
|
class LoopingTask:
|
|
|
|
"""
|
|
|
|
Wrapper for having a coroutine go in the background and only have one
|
|
|
|
copy running at a time.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, callable):
|
|
|
|
self.callable = callable
|
|
|
|
self.task: asyncio.Task | None = None
|
|
|
|
|
|
|
|
def run(self) -> bool:
|
|
|
|
# If we have a task object, see if we can clear it up
|
|
|
|
if self.task is not None:
|
|
|
|
if self.task.done():
|
|
|
|
self.task = None
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
# OK, launch a new task
|
|
|
|
self.task = asyncio.create_task(self.callable())
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-11-09 06:06:29 +00:00
|
|
|
class StatorRunner:
|
|
|
|
"""
|
|
|
|
Runs tasks on models that are looking for state changes.
|
2022-11-25 00:11:04 +00:00
|
|
|
Designed to run either indefinitely, or just for a few seconds.
|
2022-11-09 06:06:29 +00:00
|
|
|
"""
|
|
|
|
|
2022-11-14 01:42:47 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-12-05 17:38:37 +00:00
|
|
|
models: list[type[StatorModel]],
|
2022-12-20 08:02:35 +00:00
|
|
|
concurrency: int = getattr(settings, "STATOR_CONCURRENCY", 50),
|
|
|
|
concurrency_per_model: int = getattr(
|
2023-02-04 04:51:24 +00:00
|
|
|
settings, "STATOR_CONCURRENCY_PER_MODEL", 15
|
2022-12-20 08:02:35 +00:00
|
|
|
),
|
2022-12-05 17:38:37 +00:00
|
|
|
liveness_file: str | None = None,
|
2022-11-19 17:20:13 +00:00
|
|
|
schedule_interval: int = 30,
|
|
|
|
lock_expiry: int = 300,
|
2022-11-26 18:54:14 +00:00
|
|
|
run_for: int = 0,
|
2022-11-14 01:42:47 +00:00
|
|
|
):
|
2022-11-09 06:06:29 +00:00
|
|
|
self.models = models
|
|
|
|
self.runner_id = uuid.uuid4().hex
|
2022-11-14 01:42:47 +00:00
|
|
|
self.concurrency = concurrency
|
|
|
|
self.concurrency_per_model = concurrency_per_model
|
2022-11-19 17:20:13 +00:00
|
|
|
self.liveness_file = liveness_file
|
|
|
|
self.schedule_interval = schedule_interval
|
|
|
|
self.lock_expiry = lock_expiry
|
2022-11-26 18:54:14 +00:00
|
|
|
self.run_for = run_for
|
2022-12-22 05:03:41 +00:00
|
|
|
self.minimum_loop_delay = 0.5
|
|
|
|
self.maximum_loop_delay = 5
|
2023-01-16 18:15:28 +00:00
|
|
|
# Set up SIGALRM handler
|
|
|
|
signal.signal(signal.SIGALRM, self.alarm_handler)
|
2022-11-09 06:06:29 +00:00
|
|
|
|
|
|
|
async def run(self):
|
2022-12-05 01:08:23 +00:00
|
|
|
sentry.set_takahe_app("stator")
|
2022-12-15 19:26:17 +00:00
|
|
|
self.handled = {}
|
2022-11-26 18:54:14 +00:00
|
|
|
self.started = time.monotonic()
|
2022-11-19 17:20:13 +00:00
|
|
|
self.last_clean = time.monotonic() - self.schedule_interval
|
2022-11-09 06:06:29 +00:00
|
|
|
self.tasks = []
|
2022-12-22 05:03:41 +00:00
|
|
|
self.loop_delay = self.minimum_loop_delay
|
2023-02-04 04:51:24 +00:00
|
|
|
self.schedule_task = LoopingTask(self.run_scheduling)
|
|
|
|
self.fetch_task = LoopingTask(self.fetch_and_process_tasks)
|
|
|
|
self.config_task = LoopingTask(self.load_config)
|
2022-11-09 06:06:29 +00:00
|
|
|
# For the first time period, launch tasks
|
2022-11-10 05:29:33 +00:00
|
|
|
print("Running main task loop")
|
2022-11-19 17:20:13 +00:00
|
|
|
try:
|
2022-12-05 01:08:23 +00:00
|
|
|
with sentry.configure_scope() as scope:
|
|
|
|
while True:
|
|
|
|
# Do we need to do cleaning?
|
|
|
|
if (time.monotonic() - self.last_clean) >= self.schedule_interval:
|
2023-01-16 18:15:28 +00:00
|
|
|
# Set up the watchdog timer (each time we do this the
|
|
|
|
# previous one is cancelled)
|
|
|
|
signal.alarm(self.schedule_interval * 2)
|
2022-12-05 01:08:23 +00:00
|
|
|
# Refresh the config
|
2023-02-04 04:51:24 +00:00
|
|
|
self.config_task.run()
|
|
|
|
if self.schedule_task.run():
|
|
|
|
print("Running cleaning and scheduling")
|
|
|
|
else:
|
|
|
|
print("Previous scheduling still running...!")
|
2022-12-31 18:54:51 +00:00
|
|
|
# Write liveness file if configured
|
|
|
|
if self.liveness_file:
|
|
|
|
with open(self.liveness_file, "w") as fh:
|
|
|
|
fh.write(str(int(time.time())))
|
2023-02-04 04:51:24 +00:00
|
|
|
self.last_clean = time.monotonic()
|
2022-11-27 18:09:46 +00:00
|
|
|
|
2022-12-05 01:08:23 +00:00
|
|
|
# Clear the cleaning breadcrumbs/extra for the main part of the loop
|
|
|
|
sentry.scope_clear(scope)
|
2022-11-27 18:09:46 +00:00
|
|
|
|
2022-12-05 01:08:23 +00:00
|
|
|
self.remove_completed_tasks()
|
2023-02-04 04:51:24 +00:00
|
|
|
|
|
|
|
# Fetching is kind of blocking, so we need to do this
|
|
|
|
# as a separate coroutine
|
|
|
|
self.fetch_task.run()
|
2022-12-05 01:08:23 +00:00
|
|
|
|
|
|
|
# Are we in limited run mode?
|
|
|
|
if (
|
|
|
|
self.run_for
|
|
|
|
and (time.monotonic() - self.started) > self.run_for
|
|
|
|
):
|
|
|
|
break
|
2022-12-22 05:03:41 +00:00
|
|
|
|
|
|
|
# Prevent busylooping, but also back off delay if we have
|
|
|
|
# no tasks
|
|
|
|
if self.tasks:
|
|
|
|
self.loop_delay = self.minimum_loop_delay
|
|
|
|
else:
|
|
|
|
self.loop_delay = min(
|
|
|
|
self.loop_delay * 1.5,
|
|
|
|
self.maximum_loop_delay,
|
|
|
|
)
|
|
|
|
await asyncio.sleep(self.loop_delay)
|
|
|
|
|
2022-12-05 01:08:23 +00:00
|
|
|
# Clear the Sentry breadcrumbs and extra for next loop
|
|
|
|
sentry.scope_clear(scope)
|
2022-11-26 18:54:14 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
# Wait for tasks to finish
|
|
|
|
print("Waiting for tasks to complete")
|
|
|
|
while True:
|
|
|
|
self.remove_completed_tasks()
|
|
|
|
if not self.tasks:
|
|
|
|
break
|
|
|
|
# Prevent busylooping
|
2022-12-22 05:03:41 +00:00
|
|
|
await asyncio.sleep(0.5)
|
2022-11-10 05:29:33 +00:00
|
|
|
print("Complete")
|
2022-11-09 06:06:29 +00:00
|
|
|
return self.handled
|
|
|
|
|
2023-01-16 18:15:28 +00:00
|
|
|
def alarm_handler(self, signum, frame):
|
|
|
|
"""
|
|
|
|
Called when SIGALRM fires, which means we missed a schedule loop.
|
|
|
|
Just exit as we're likely deadlocked.
|
|
|
|
"""
|
|
|
|
print("Watchdog timeout exceeded")
|
|
|
|
os._exit(2)
|
|
|
|
|
2023-02-04 04:51:24 +00:00
|
|
|
async def load_config(self):
|
|
|
|
"""
|
|
|
|
Refreshes config from the DB
|
|
|
|
"""
|
|
|
|
Config.system = await Config.aload_system()
|
|
|
|
|
2022-11-27 19:09:08 +00:00
|
|
|
async def run_scheduling(self):
|
2022-11-27 18:09:46 +00:00
|
|
|
"""
|
|
|
|
Do any transition cleanup tasks
|
|
|
|
"""
|
2023-02-04 04:51:24 +00:00
|
|
|
if self.handled:
|
|
|
|
print("Tasks processed since last flush:")
|
|
|
|
for label, number in self.handled.items():
|
|
|
|
print(f" {label}: {number}")
|
|
|
|
else:
|
|
|
|
print("No tasks handled since last flush.")
|
2022-12-05 01:08:23 +00:00
|
|
|
with sentry.start_transaction(op="task", name="stator.run_scheduling"):
|
|
|
|
for model in self.models:
|
2023-02-04 04:51:24 +00:00
|
|
|
await self.submit_stats(model)
|
|
|
|
await model.atransition_clean_locks()
|
|
|
|
await model.atransition_schedule_due()
|
|
|
|
await model.atransition_delete_due()
|
2022-11-27 18:09:46 +00:00
|
|
|
|
2022-12-15 19:26:17 +00:00
|
|
|
async def submit_stats(self, model):
|
|
|
|
"""
|
|
|
|
Pop some statistics into the database
|
|
|
|
"""
|
|
|
|
stats_instance = await Stats.aget_for_model(model)
|
|
|
|
if stats_instance.model_label in self.handled:
|
|
|
|
stats_instance.add_handled(self.handled[stats_instance.model_label])
|
|
|
|
del self.handled[stats_instance.model_label]
|
|
|
|
stats_instance.set_queued(await model.atransition_ready_count())
|
|
|
|
stats_instance.trim_data()
|
|
|
|
await sync_to_async(stats_instance.save)()
|
|
|
|
|
2022-11-27 18:09:46 +00:00
|
|
|
async def fetch_and_process_tasks(self):
|
|
|
|
# Calculate space left for tasks
|
|
|
|
space_remaining = self.concurrency - len(self.tasks)
|
|
|
|
# Fetch new tasks
|
|
|
|
for model in self.models:
|
|
|
|
if space_remaining > 0:
|
|
|
|
for instance in await model.atransition_get_with_lock(
|
|
|
|
number=min(space_remaining, self.concurrency_per_model),
|
|
|
|
lock_expiry=(
|
|
|
|
timezone.now() + datetime.timedelta(seconds=self.lock_expiry)
|
|
|
|
),
|
|
|
|
):
|
|
|
|
self.tasks.append(
|
|
|
|
asyncio.create_task(self.run_transition(instance))
|
|
|
|
)
|
2022-12-15 19:26:17 +00:00
|
|
|
self.handled[model._meta.label_lower] = (
|
|
|
|
self.handled.get(model._meta.label_lower, 0) + 1
|
|
|
|
)
|
2022-11-27 18:09:46 +00:00
|
|
|
space_remaining -= 1
|
|
|
|
|
2022-11-10 06:48:31 +00:00
|
|
|
async def run_transition(self, instance: StatorModel):
|
|
|
|
"""
|
|
|
|
Wrapper for atransition_attempt with fallback error handling
|
|
|
|
"""
|
2022-12-05 01:08:23 +00:00
|
|
|
task_name = f"stator.run_transition:{instance._meta.label_lower}#{{id}} from {instance.state}"
|
2023-02-04 18:46:38 +00:00
|
|
|
with sentry.start_transaction(op="task", name=task_name):
|
|
|
|
sentry.set_context(
|
|
|
|
"instance",
|
|
|
|
{
|
|
|
|
"model": instance._meta.label_lower,
|
|
|
|
"pk": instance.pk,
|
|
|
|
"state": instance.state,
|
|
|
|
"state_age": instance.state_age,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
print(
|
|
|
|
f"Attempting transition on {instance._meta.label_lower}#{instance.pk} from state {instance.state}"
|
2022-12-05 01:08:23 +00:00
|
|
|
)
|
2023-02-04 18:46:38 +00:00
|
|
|
await instance.atransition_attempt()
|
|
|
|
except BaseException as e:
|
|
|
|
await exceptions.acapture_exception(e)
|
|
|
|
traceback.print_exc()
|
2022-11-10 06:48:31 +00:00
|
|
|
|
2022-11-09 06:06:29 +00:00
|
|
|
def remove_completed_tasks(self):
|
2022-11-10 06:48:31 +00:00
|
|
|
"""
|
|
|
|
Removes all completed asyncio.Tasks from our local in-progress list
|
|
|
|
"""
|
2022-11-09 06:06:29 +00:00
|
|
|
self.tasks = [t for t in self.tasks if not t.done()]
|
2022-12-06 02:21:00 +00:00
|
|
|
|
|
|
|
async def run_single_cycle(self):
|
|
|
|
"""
|
2022-12-29 05:47:28 +00:00
|
|
|
Testing entrypoint to advance things just one cycle, and allow errors
|
|
|
|
to propagate out.
|
2022-12-06 02:21:00 +00:00
|
|
|
"""
|
|
|
|
await asyncio.wait_for(self.fetch_and_process_tasks(), timeout=1)
|
2022-12-29 05:47:28 +00:00
|
|
|
for task in self.tasks:
|
|
|
|
await task
|
2022-12-06 02:21:00 +00:00
|
|
|
|
|
|
|
run_single_cycle_sync = async_to_sync(run_single_cycle)
|