What are Cron Jobs?
Cron jobs in SubWallet Extension are scheduled background tasks that run periodically in the background environment. They handle time-sensitive operations like price updates, NFT refreshes, staking reward calculations, and other recurring data synchronization tasks. Cron jobs provide:- Automatic data refreshes without user interaction
- Background processing that doesn’t block the UI
- Resource optimization through scheduled execution
- Event-driven updates based on application state changes
Cron Architecture
The cron system is managed by theKoniCron class located in packages/extension-base/src/koni/background/cron.ts.
KoniCron Class Structure
Cron Job Methods
Adding a Cron Job
name: Unique identifier for the cron jobcallback: Function to execute on each intervalinterval: Time in milliseconds between executionsrunFirst: Whether to run immediately before scheduling
Adding a Subscribable Cron Job
Removing a Cron Job
Starting the Cron System
Stopping the Cron System
Common Cron Jobs in SubWallet
1. NFT Refresh Cron
2. Price Update Cron
Implemented inPriceService:
3. Staking Metadata Cron
4. MantaPay Sync Cron
5. Marketing Campaign Cron
Creating a New Cron Job
Step 1: Define the Cron Interval Constant
Step 2: Create the Cron Job Function
InKoniCron class:
Step 3: Register the Cron Job in Start Method
Step 4: Handle Dynamic Restart (Optional)
If your cron job should restart based on events:Event-Driven Cron Management
KoniCron uses an event-driven approach to dynamically manage cron jobs:Service-Based Cron Jobs
Services can implement their own cron functionality through theCronServiceInterface: