Advanced Scheduled Data Syncer¶
This example shows a Trusted plugin that synchronizes data from an external API into the local database on a schedule. It uses the @cron decorator from the SDK, delegates heavy processing to XWorker, and relies on the scheduler's built-in distributed lock for multi-worker safety.
1. The Manifest (plugin.yaml)¶
- Permission to call a dedicated gateway plugin that handles HTTP communication.
2. Implementation (src/main.py)¶
3. Key design points¶
No manual lock needed¶
Previous versions of this pattern used a cache key (sync:active_lock) as a manual distributed lock. This is no longer necessary: the SchedulerService automatically acquires a Redis lock (xcore:sched:lock:data_syncer.trigger_sync) before each execution and releases it when the job finishes. Workers that lose the race skip silently.
If the job exceeds the lock TTL (default 300 s), the lock expires and another worker can take over on the next trigger. For jobs that must never run concurrently regardless of duration, implement idempotency inside the job itself (e.g., a database-level unique constraint on the sync log).
Gateway pattern (IPC)¶
Instead of calling the external API directly, the plugin delegates to a gateway plugin. This separates network policy (retries, timeouts, credentials) from sync logic and keeps the plugin testable in isolation.
Background batching¶
Processing thousands of items in the main event loop would block requests. Chunking the data and forwarding batches to XWorker distributes the CPU load across physical worker processes.
4. Monitoring¶
See Also¶
- Scheduler Service
- Full reference for triggers, backends, scaling, and configuration.
- XWorker Internals
- How task serialization and queues work.
- Plugin Registry
- How to discover and call gateway services via IPC.