Schedule organization snapshot refresh
All checks were successful
CI/CD Pipeline / Quality Gate (push) Successful in 22s
CI/CD Pipeline / Build and Push Images (push) Successful in 5s
CI/CD Pipeline / Internal Notify (push) Successful in 1s
CI/CD Pipeline / Deploy Dev in Dokploy (push) Successful in 1s

This commit is contained in:
2026-05-06 20:54:13 +02:00
parent 0f17ff6773
commit 507ae2063a
3 changed files with 140 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
import json
from django.db import migrations
DAILY_ORGANIZATION_SNAPSHOT_TASK_NAME = "organizations:data-snapshots:daily-msk"
DAILY_ORGANIZATION_SNAPSHOT_TASK_PATH = (
"organizations.tasks.refresh_all_organization_data_snapshots"
)
DAILY_MSK_CRON = {
"minute": "30",
"hour": "4",
"day_of_week": "*",
"day_of_month": "*",
"month_of_year": "*",
"timezone": "Europe/Moscow",
}
def seed_daily_snapshot_refresh_schedule(apps, schema_editor):
CrontabSchedule = apps.get_model("django_celery_beat", "CrontabSchedule")
PeriodicTask = apps.get_model("django_celery_beat", "PeriodicTask")
crontab, _ = CrontabSchedule.objects.get_or_create(**DAILY_MSK_CRON)
field_names = {field.name for field in PeriodicTask._meta.fields}
schedule_fields = {"crontab": crontab}
for field_name in ("interval", "solar", "clocked"):
if field_name in field_names:
schedule_fields[field_name] = None
PeriodicTask.objects.update_or_create(
name=DAILY_ORGANIZATION_SNAPSHOT_TASK_NAME,
defaults={
"task": DAILY_ORGANIZATION_SNAPSHOT_TASK_PATH,
"args": json.dumps([]),
"kwargs": json.dumps({"batch_size": 100}),
"enabled": True,
"description": "Daily full refresh for API v2 organization snapshots.",
**schedule_fields,
},
)
def remove_daily_snapshot_refresh_schedule(apps, schema_editor):
PeriodicTask = apps.get_model("django_celery_beat", "PeriodicTask")
PeriodicTask.objects.filter(name=DAILY_ORGANIZATION_SNAPSHOT_TASK_NAME).delete()
class Migration(migrations.Migration):
dependencies = [
("django_celery_beat", "0018_improve_crontab_helptext"),
("organizations", "0003_allow_branch_kpp_organizations"),
]
operations = [
migrations.RunPython(
seed_daily_snapshot_refresh_schedule,
reverse_code=remove_daily_snapshot_refresh_schedule,
),
]

View File

@@ -12,6 +12,20 @@ from organizations.services import OrganizationDataSnapshotRefreshService
logger = logging.getLogger(__name__)
@shared_task
def refresh_all_organization_data_snapshots(batch_size: int = 100) -> dict:
"""Refresh all organization data snapshots for API v2."""
result = OrganizationDataSnapshotRefreshService.refresh(batch_size=batch_size)
cache.clear()
payload = {
"processed": result.processed,
"created": result.created,
"updated": result.updated,
}
logger.info("All organization data snapshots refreshed: %s", payload)
return payload
@shared_task
def refresh_organization_data_snapshots_for_parser_batch(
*,