Scheduled Tasks Guide
To create a new Schedule:
- Run
npx green_dot generate
- Select “Schedule”
- Follow the prompts
Creating Scheduled Tasks
Section titled “Creating Scheduled Tasks”Scheduled tasks are created using the schedule()
function. Here’s a basic example:
import { schedule, db } from 'green_dot'
export const mySchedule = schedule({ schedule: { frequency: '0 * * * *', // Every hour frequencyDevEnv: 'never', // Disable in development }, async main(ctx) { // Your scheduled task logic here const users = await db.user.getAll(ctx.GM) // Process users... }})
Schedule Configuration
Section titled “Schedule Configuration”Frequency Options
Section titled “Frequency Options”The schedule
property accepts either a string or an object with the following options:
schedule: { frequency: '0 * * * *', // Main frequency (CRON format) frequencyTestEnv?: string, // Optional frequency for test environment frequencyDevEnv?: string // Optional frequency for development environment}
Frequency Values
Section titled “Frequency Values”'server.start'
: Run once when the server starts'never'
: Disable the schedule- CRON expressions:
'* * * * *'
: Every minute'0 * * * *'
: Every hour'0 */12 * * *'
: Twice a day'0 5 * * *'
: Once a day at 05:00'0 5 1 * *'
: At 05:00 on day-of-month 1
Environment-Specific Frequencies
Section titled “Environment-Specific Frequencies”You can configure different frequencies for different environments:
schedule: { frequency: '0 5 * * *', // Production: Daily at 5 AM frequencyDevEnv: 'never', // Development: Disabled frequencyTestEnv: '* * * * *' // Testing: Every minute}
Schedule Context (ctx)
Section titled “Schedule Context (ctx)”The main
function receives a system context (ctx
) with full privileges:
async main(ctx) { // ctx is a system context (ctx.GM) // You have full access to all databases and operations const users = await ctx.db.user.getAll(ctx) // Process data...}
Best Practices
Section titled “Best Practices”- Use appropriate frequencies for different environments
- Avoid using
'* * * * *'
in production - Handle errors properly in scheduled tasks
- Log important operations and errors
- Consider timezone implications
- Use descriptive names for scheduled tasks
- Document the purpose and frequency of each task
Example Use Cases
Section titled “Example Use Cases”Data Cleanup
Section titled “Data Cleanup”export const cleanupOldData = schedule({ schedule: { frequency: '0 0 * * *', // Daily at midnight frequencyDevEnv: 'never' }, async main(ctx) { const oldDate = new Date() oldDate.setDate(oldDate.getDate() - 30)
await db.logs.deleteMany(ctx.GM, { createdAt: { $lt: oldDate } }) }})
Report Generation
Section titled “Report Generation”export const generateDailyReport = schedule({ schedule: { frequency: '0 6 * * *', // Daily at 6 AM frequencyDevEnv: 'server.start' }, async main(ctx) { const yesterday = new Date() yesterday.setDate(yesterday.getDate() - 1)
const report = await db.orders.getAll(ctx.GM, { createdAt: { $gte: yesterday } })
// Send report... }})
Error Handling
Section titled “Error Handling”Scheduled tasks should include proper error handling:
export const mySchedule = schedule({ schedule: { frequency: '0 * * * *' }, async main(ctx) { try { // Task logic } catch (error) { // Log error console.error('Schedule error:', error) // Notify administrators await notifyAdmins(error) } }})
Monitoring
Section titled “Monitoring”You can monitor scheduled tasks through:
- Application logs
- Database audit trails
- System monitoring tools
- Custom monitoring solutions
Security Considerations
Section titled “Security Considerations”- Use system context (
ctx.GM
) for database operations - Implement proper error handling
- Log sensitive operations
- Use appropriate frequencies for different environments
- Consider resource usage and impact