# Celery Schedule - Daily Task Execution This document outlines the scheduled tasks in the DAS system organized by hour of the day. ## Hour-by-Hour Schedule ### 12:00 AM (00:00) - **set_alert_counter_for_all_users** - Resets alert counter for all users - Task: `activity.tasks.reset_alert_counter_for_all_users` - Schedule: Daily at midnight ### 2:00 AM - **download-features-from-wfs** - Downloads features from Web Feature Service - Task: `mapping.tasks.automate_download_features_from_wfs` - Schedule: Daily at 2:00 AM ### 3:00 AM - **reconcile-observation-segments** - Daily safety net that detects and rebuilds `ObservationSegment` gaps in the recent window for each tenant. Walks every source with activity in the last `OBSERVATION_SEGMENT_RECONCILE_HOURS` hours; runs `recompute_observation_segments_for_source_range` (idempotent via `select_for_update` + `IntegrityError` fallback) when the segment count diverges from `count(valid_obs) - 1`. Closes the gap left by paths that bypass `post_save` (notably bulk updates on `exclusion_flags`). - Task: `observations.tasks.reconcile_observation_segments_task` - Schedule: Daily at 3:00 AM (tenant-local, ahead of the 4:00 AM cleanup run) - Queue: `maintenance` - Options: `expires=20h` so stale per-tenant messages drop before the next run if workers fall behind. ### 4:00 AM - **routine-delete-observational-data** - Maintains observation data (cleanup) - Task: `observations.tasks.maintain_observation_data` - Schedule: Daily at 4:00 AM ### 6:00 AM - **reports** - Generates subject/source reports - Task: `reports.tasks.subjectsource_report` - Schedule: Daily at 6:00 AM ### Every Hour - **check-sources-threshold** - Checks sources threshold - Task: `reports.tasks.run_check_sources_threshold` - Schedule: Every hour - **refresh_tenants_cache** - Refreshes tenants cache - Task: `das_server.tasks.refresh_tenants_cache` - Schedule: Every hour ### Every 15 Minutes - **firms-plugins** - Schedules FIRMS plugins - Task: `tracking.tasks.schedule_firms_plugins` - Schedule: Every 15 minutes - **poll_news_gcs_bucket** - Polls news GCS bucket - Task: `observations.tasks.poll_news_gcs_bucket` - Schedule: Every 15 minutes ### Every 10 Minutes - **sweep-orphan-socketio-queues** - Deletes orphan `python-socketio.*` queue keys left in the realtime broker Redis when a socketio consumer terminates abnormally. Removes keys idle for more than 24h and the matching entry in the `_kombu.binding.socketio` registry. - Task: `rt_api.tasks.sweep_orphan_socketio_queues` - Schedule: Every 10 minutes - Queue: `maintenance` ### Every 5 Minutes - **plugins** - Runs tracking plugins - Task: `tracking.tasks.run_plugins` - Schedule: Every 5 minutes - **auto-resolve** - Automatically updates event state - Task: `activity.tasks.automatically_update_event_state` - Schedule: Every 5 minutes ### Every 30 Minutes - **observation-lag-report** - Reports on observation lag delays - Task: `reports.tasks.alert_lag_delay` - Schedule: Every 30 minutes ### Every Minute - **periodically_maintain_patrol_state** - Maintains patrol state - Task: `activity.tasks.periodically_maintain_patrol_state` - Schedule: Every minute ### Every 60 Seconds - **beat-pulse** - Celery beat pulse routine - Task: `das_server.tasks.celerybeat_pulse` - Schedule: Every 60 seconds ### Every 15 Seconds - **service-status** - Broadcasts service status - Task: `rt_api.tasks.broadcast_service_status` - Schedule: Every 15 seconds ### Every 60 Seconds - **redis-status** - Checks Redis queues - Task: `rt_api.tasks.check_redis_queues` - Schedule: Every 60 seconds ### Every 12 Hours - **subject-status-maintenance** - Maintains subject status for all subjects - Task: `observations.tasks.maintain_subjectstatus_all` - Schedule: Every 12 hours ### Weekly (Sunday at 11:00 PM) - **postgresql_partman_maintain_observation_partitions** - Creates the missing monthly partitions of each partman-managed observations table (`observations_observation`, `observations_observationsegment`) in turn, for the current UTC month through `premake` months ahead, with `partman.create_partition_time`. A failure on one table or month is logged and does not stop the others. - Task: `observations.tasks.maintain_observation_partitions` - Schedule: Every Sunday at 11:00 PM (finishes before the Monday midnight check) - Runs once per database, not per tenant: every tenant's rows share these tables and pg_partman config is per table. - Applies no retention. The `'3 years'` retention in `partman.part_config` for `observations_observationsegment` (set by migration `observations/0200`) is not enforced by this job and would need its own explicit job. ### Weekly (Monday at 12:00 AM) - **postgresql_partman_check_observation_partitions** - Runs the partition table check (premake >= 3, and a partition present for every month from the current UTC month through `premake - 1` months ahead) on `observations_observation` and `observations_observationsegment`. A missing month whose rows are already in the default partition also gets the `fix_partition_by_month` hint described below. - Task: `observations.tasks.check_observation_partitions` - Schedule: Every Monday at midnight ## Queue Priorities The system uses different queues to manage task priorities: - **realtime_p1**: High-priority real-time tasks - **realtime_p2**: Medium-priority real-time tasks - **realtime_p3**: Lower-priority real-time tasks - **maintenance**: System maintenance tasks - **analyzers**: Analysis and processing tasks ## Notes - All times are based on the system's configured timezone (`settings.TIME_ZONE`) - Tasks with `timedelta` schedules run continuously at the specified intervals - Tasks with `crontab` schedules run at specific times on specific days - The `PLUGINS_INTERVAL` is set to 5 minutes (300 seconds) for plugin execution - High-frequency tasks like service status and Redis checks ensure system health monitoring - **Partition maintenance:** `maintain_observation_partitions` never calls `partman.run_maintenance()` or `run_maintenance_proc()`. In pg_partman 5, `run_maintenance` anchors on the newest child partition that holds rows and ignores `now()` (with `infinite_time_partitions = false`) and the default partition. One far-future row in a child therefore makes it create every month up to that date plus `premake` in a single run, and rows landing in the default partition freeze it. Instead, for each table the job reads `control`, `premake` and `partition_interval` from `partman.part_config`. It skips the table, with an `ER Partman:` error, if the table is not registered or the interval is not exactly one month. Its target window runs from the current UTC month through `premake` months ahead, which covers every month the Monday check expects. The check deliberately expects one month fewer than the job creates: it runs an hour after the job, so when the 1st is a Monday it runs in the next month, and expecting `premake + 1` months would then demand a month the job had no reason to create yet, and the months missing from the existing partition boundaries are the ones to create. Each missing month not blocked by default-partition rows (see below) is created with `partman.create_partition_time` and an explicit UTC month start (`'2026-11-01 00:00:00+00'`), one month per transaction with `lock_timeout = 5s`, `statement_timeout = 60s` and `TimeZone = UTC` set locally. One month failing does not undo or stop the others. A monthly maintenance task with no timeouts was removed in December 2024 after it brought down the API (ERA-10622): attaching a partition takes an ACCESS EXCLUSIVE lock on the default partition and scans it, so a lock request stuck behind a long-running query queues all later queries on the table. A month that hits a timeout logs `ER Partman: [] Could not create the YYYY-MM partition.` and is retried the following Sunday; with `premake = 5`, weekly runs leave months of slack. To run the same job on demand, use `python manage.py partman_maintain_observation_partitions`. `--table ` limits it to one table, and `--dry-run` reports each table's control, premake and interval, the target window, the months already present, the months it would create, and the blocked months with fix hints, without writing anything. It prints a per-table outcome (created, already present, blocked, failed) and exits non-zero if a table was skipped or any month was blocked or failed. Do not use `manage.py partman_run_maintenance_proc` for this: it runs `run_maintenance` on every table in `partman.part_config`, with the anchoring problem above, and applies their retention. - **Partitions blocked by the default partition:** PostgreSQL refuses to attach a monthly partition while rows in its range sit in the default partition (`updated partition constraint for default partition ... would be violated by some row`). Before creating partitions, and when the weekly check finds a missing month, the job probes `
_default` for each unpartitioned month in the window (one bounded `EXISTS` per month, under the same timeouts, on the `control` column read from `partman.part_config`). Blocked months are skipped, and later months are still created; a month whose rows reach the default partition between the probe and its creation is reported the same way. The job only reports; it never moves data. For each blocked month it logs one error with the command an operator should run, for example: ``` ER Partman: [public.observations_observationsegment] Cannot create the 2026-11 partition: rows in that range are in the default partition. Preview with `python manage.py fix_partition_by_month -t observations_observationsegment --partition-column start_recorded_at -y 2026 -m 11 --dry-run`, then run `python manage.py fix_partition_by_month -t observations_observationsegment --partition-column start_recorded_at -y 2026 -m 11`. ``` - **Signal-driven (not scheduled):** `observations.tasks.update_observation_segments_batch_task` is enqueued from the `Observation` `post_save` signal — buffered per (thread, db_alias) and flushed on commit, chunked by `OBSERVATION_SEGMENT_POST_SAVE_BATCH_SIZE` so many saves in one `atomic()` produce one task per chunk per tenant. Routed to `realtime_p3` for both creates and updates (see `OBSERVATION_SEGMENT_ASYNC_*_QUEUE` in `observations/signals.py`). The legacy single-id `observations.tasks.update_observation_segments_for_observation_task` is retained only for in-flight messages from prior deploys (no producer in this codebase still enqueues it). The `reconcile-observation-segments` job above is the daily safety net that catches anything missed by this path. - **Operator-triggered (not scheduled):** `observations.tasks.bump_observation_segment_tile_cache_for_tenant_task` is enqueued by `manage.py bust_observation_tile_cache --enqueue` to bump the per-tenant segment tile version (O(1) Redis INCR) so cached MVT keys miss without a SCAN. Routed to the `maintenance` queue.