2022-11-09 06:06:29 +00:00
|
|
|
import asyncio
|
|
|
|
import datetime
|
|
|
|
import time
|
2022-11-10 06:48:31 +00:00
|
|
|
import traceback
|
2022-11-09 06:06:29 +00:00
|
|
|
import uuid
|
|
|
|
from typing import List, Type
|
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-11-10 05:29:33 +00:00
|
|
|
from stator.models import StatorModel
|
2022-11-09 06:06:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StatorRunner:
|
|
|
|
"""
|
|
|
|
Runs tasks on models that are looking for state changes.
|
|
|
|
Designed to run in a one-shot mode, living inside a request.
|
|
|
|
"""
|
|
|
|
|
2022-11-14 01:42:47 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
models: List[Type[StatorModel]],
|
|
|
|
concurrency: int = 30,
|
|
|
|
concurrency_per_model: int = 5,
|
|
|
|
run_period: int = 30,
|
|
|
|
wait_period: int = 30,
|
|
|
|
):
|
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
|
|
|
|
self.run_period = run_period
|
|
|
|
self.total_period = run_period + wait_period
|
2022-11-09 06:06:29 +00:00
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
start_time = time.monotonic()
|
|
|
|
self.handled = 0
|
|
|
|
self.tasks = []
|
|
|
|
# Clean up old locks
|
2022-11-10 05:29:33 +00:00
|
|
|
print("Running initial cleaning and scheduling")
|
|
|
|
initial_tasks = []
|
|
|
|
for model in self.models:
|
|
|
|
initial_tasks.append(model.atransition_clean_locks())
|
|
|
|
initial_tasks.append(model.atransition_schedule_due())
|
|
|
|
await asyncio.gather(*initial_tasks)
|
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-14 01:42:47 +00:00
|
|
|
while (time.monotonic() - start_time) < self.run_period:
|
2022-11-09 06:06:29 +00:00
|
|
|
self.remove_completed_tasks()
|
2022-11-14 01:42:47 +00:00
|
|
|
space_remaining = self.concurrency - len(self.tasks)
|
2022-11-09 06:06:29 +00:00
|
|
|
# Fetch new tasks
|
2022-11-10 05:29:33 +00:00
|
|
|
for model in self.models:
|
|
|
|
if space_remaining > 0:
|
|
|
|
for instance in await model.atransition_get_with_lock(
|
2022-11-14 01:42:47 +00:00
|
|
|
number=min(space_remaining, self.concurrency_per_model),
|
|
|
|
lock_expiry=(
|
|
|
|
timezone.now()
|
|
|
|
+ datetime.timedelta(seconds=(self.total_period * 2) + 60)
|
|
|
|
),
|
2022-11-10 05:29:33 +00:00
|
|
|
):
|
|
|
|
self.tasks.append(
|
2022-11-10 06:48:31 +00:00
|
|
|
asyncio.create_task(self.run_transition(instance))
|
2022-11-10 05:29:33 +00:00
|
|
|
)
|
|
|
|
self.handled += 1
|
|
|
|
space_remaining -= 1
|
2022-11-09 06:06:29 +00:00
|
|
|
# Prevent busylooping
|
2022-11-10 05:29:33 +00:00
|
|
|
await asyncio.sleep(0.1)
|
2022-11-09 06:06:29 +00:00
|
|
|
# Then wait for tasks to finish
|
2022-11-10 05:29:33 +00:00
|
|
|
print("Waiting for tasks to complete")
|
2022-11-14 01:42:47 +00:00
|
|
|
while (time.monotonic() - start_time) < self.total_period:
|
2022-11-09 06:06:29 +00:00
|
|
|
self.remove_completed_tasks()
|
|
|
|
if not self.tasks:
|
|
|
|
break
|
|
|
|
# Prevent busylooping
|
|
|
|
await asyncio.sleep(1)
|
2022-11-10 05:29:33 +00:00
|
|
|
print("Complete")
|
2022-11-09 06:06:29 +00:00
|
|
|
return self.handled
|
|
|
|
|
2022-11-10 06:48:31 +00:00
|
|
|
async def run_transition(self, instance: StatorModel):
|
|
|
|
"""
|
|
|
|
Wrapper for atransition_attempt with fallback error handling
|
|
|
|
"""
|
|
|
|
try:
|
2022-11-11 06:42:43 +00:00
|
|
|
print(
|
|
|
|
f"Attempting transition on {instance._meta.label_lower}#{instance.pk} from state {instance.state}"
|
|
|
|
)
|
2022-11-10 06:48:31 +00:00
|
|
|
await instance.atransition_attempt()
|
|
|
|
except BaseException:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
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()]
|