Core Scheduled Jobs Reference
Scheduled jobs are functions executed at specified intervals in the background of your Mercur application. They are used to perform recurring tasks such as processing payouts, sending notifications, and maintaining data consistency. Learn more about scheduled jobs in the Medusa documentation.Daily Payouts Job
Overview
Thedaily-payouts job processes pending payouts for completed orders. It runs daily at midnight, identifying all orders that don’t have associated payouts and emitting events to trigger payout processing workflows.
Configuration
- Name:
daily-payouts - Schedule:
0 0 * * *(Every day at midnight) - Source:
packages/modules/b2c-core/src/jobs/daily-payouts.ts
Functionality
The job performs the following operations:- Query Pending Orders: Identifies orders that don’t have associated payouts by performing a left join between the
orderandorder_payouttables - Batch Processing: Processes orders in batches of 100 to manage system load
- Event Emission: Emits
payout.receivedevents for each order that needs payout processing - Retry Configuration: Each event is configured with:
- 3 retry attempts
- Staggered delays to prevent system overload
- Progressive delay calculation:
(1200ms * order_count * offset) / BATCH_SIZE
Event Emitted
Use Cases
- Automated Payout Processing: Ensures all completed orders are processed for seller payouts
- Failed Payout Recovery: Catches orders that may have failed during initial payout processing
- Batch Operations: Handles large volumes of orders efficiently with batching and retry logic
Daily Payouts Summary Job
Overview
Thedaily-payouts-summary job sends daily payout summary notifications to sellers with active payout accounts. It runs every day at 8 AM, compiling payout information from the previous 24 hours and notifying sellers about their earnings.
Configuration
- Name:
daily-payouts-summary - Schedule:
0 8 * * *(Every day at 8 AM) - Source:
packages/modules/b2c-core/src/jobs/daily-payouts-summary.ts
Functionality
The job performs the following operations:- Fetch Active Accounts: Retrieves all payout accounts with
ACTIVEstatus along with seller information - Calculate Time Range: Determines the 24-hour period from the current time
- Query Payouts: For each active payout account, fetches payouts created within the time range
- Aggregate Data: Collects detailed payout and order information including:
- Payout ID, amount, and currency
- Associated order ID and display ID
- Creation timestamps
- Send Notifications: Emits notification events for sellers who received payouts in the period
Event Emitted
Use Cases
- Seller Communication: Keeps sellers informed about their daily earnings
- Transparency: Provides detailed breakdown of payouts and associated orders
- Email Notifications: Triggers email notifications with payout summaries
- Reporting: Enables sellers to track their payment history
Notes
Cron Expression Format
Both jobs use cron expressions to define their schedules. The format is:Event-Driven Architecture
These jobs follow an event-driven pattern:- They don’t process payouts directly
- They emit events that trigger workflows
- This separation allows for better error handling, retries, and monitoring
- Workflows can be executed independently if needed
Performance Considerations
Daily Payouts Job:- Implements batching (100 orders per batch) to manage memory and database load
- Uses progressive delays to prevent overwhelming the event system
- Configures retry attempts to handle transient failures
- Only processes accounts with active status
- Skips sellers with no payouts in the time period
- Aggregates notifications into a single event per seller
Monitoring
To monitor these jobs:- Check application logs for job execution timestamps
- Monitor event bus metrics for
payout.receivedandpayout.notification_sentevents - Track workflow execution success rates
- Set up alerts for job failures or unusual patterns