This document explains how the cron service is integrated into the CService API using the robfig/cron library for automated password reset token cleanup and other scheduled tasks.
The cron service provides:
- Password Reset Token Cleanup: Automatically removes expired password reset tokens
- Configurable Scheduling: Uses standard cron expressions
- Service Integration: Seamlessly integrates with existing application services
- Graceful Shutdown: Properly stops when the application shuts down
- Production Ready: Built on the battle-tested robfig/cron library
The cron service is configured through the main application configuration file. Add these settings to your config.yml:
# config.yml
service:
# Cron job configuration for scheduled tasks
cron:
# Enable/disable cron service (default: false)
enabled: true
# Cron expression for password reset token cleanup (default: "0 0 * * *" - daily at midnight)
# Examples:
# "*/5 * * * *" - Every 5 minutes
# "0 */6 * * *" - Every 6 hours
# "0 0 * * *" - Daily at midnight
# "0 0 * * 0" - Weekly on Sunday at midnight
password_reset_cleanup: "0 */6 * * *" # Every 6 hours
# Timezone for cron jobs (default: "UTC")
timezone: "UTC"
# Password reset token configuration
password_reset:
# Length of generated password reset tokens (default: 32)
token_length: 32
# How long password reset tokens are valid in minutes (default: 60)
token_lifetime_minutes: 60
# How often to clean up expired tokens in hours (default: 24)
cleanup_interval_hours: 24
# Maximum number of active tokens per user (default: 3)
max_tokens_per_user: 3You can also configure via environment variables:
# Enable cron service
export CSERVICE_SERVICE_CRON_ENABLED=true
# Set cleanup schedule (every 5 minutes for testing)
export CSERVICE_SERVICE_CRON_PASSWORD_RESET_CLEANUP="*/5 * * * *"
# Set timezone
export CSERVICE_SERVICE_CRON_TIMEZONE="America/New_York"The cron service is integrated into the main application startup in cmd/cservice-api/main.go:
- Configuration Loading: Cron config is loaded from Viper
- Service Creation: Cron service is created with the configuration
- Job Setup: Password reset cleanup job is configured
- Service Start: Cron scheduler starts and begins running jobs
- Graceful Shutdown: Service is properly stopped during application shutdown
During application shutdown, the cron service is stopped first before other services:
- Cron Stop: All scheduled jobs are stopped
- HTTP Server Shutdown: Server stops accepting new requests
- Background Services: Mail workers and other services stop
- Database Cleanup: Connections are closed gracefully
The application logs provide information about cron service status:
INFO Cron service started successfully jobs=1
INFO Password reset token cleanup job scheduled cron="0 */6 * * *" cleanup_interval=24h0m0s
You can view scheduled job information through the service:
// Get information about all scheduled jobs
entries := cronService.GetJobEntries()
for _, entry := range entries {
fmt.Printf("Job %d: Next run at %v, Schedule: %s\n",
entry.ID, entry.Next, entry.Schedule)
}For testing or manual execution, you can run the cleanup once:
ctx := context.Background()
err := cronService.RunPasswordResetCleanupOnce(ctx, queries)
if err != nil {
log.Printf("Manual cleanup failed: %v", err)
}The service supports standard 5-field cron expressions:
| Expression | Description |
|---|---|
"*/5 * * * *" |
Every 5 minutes |
"0 * * * *" |
Every hour (at minute 0) |
"0 */6 * * *" |
Every 6 hours |
"0 0 * * *" |
Daily at midnight |
"0 0 * * 0" |
Weekly on Sunday at midnight |
"0 0 1 * *" |
Monthly on the 1st at midnight |
"0 2 * * 1-5" |
Weekdays at 2 AM |
cron.Service: Main service that manages the scheduler and jobscron.Scheduler: Wrapper around robfig/cron with additional functionalityreset.CleanupService: Handles password reset token cleanup logicreset.TokenManager: Manages password reset token operations
main.go
├── Load Configuration
├── Create Cron Service
├── Setup Jobs (Password Reset Cleanup)
├── Start Scheduler
├── Register with Shutdown Manager
└── Start HTTP Server
Shutdown:
├── Stop Cron Service
├── Stop HTTP Server
├── Stop Background Services
└── Close Database Connections
The cron service includes comprehensive error handling:
- Configuration Errors: Invalid cron expressions or missing config
- Database Errors: Connection issues during cleanup operations
- Job Execution Errors: Individual job failures don't crash the service
- Shutdown Errors: Graceful degradation if shutdown fails
- Non-blocking: Cron jobs run in separate goroutines
- Database Efficient: Cleanup operations are optimized with proper indexing
- Memory Safe: No memory leaks in long-running operations
- Timezone Aware: Proper timezone handling for global deployments
- No External Dependencies: Uses only the trusted robfig/cron library
- Database Security: All database operations use parameterized queries
- Logging Security: No sensitive data logged in cron operations
- Resource Limits: Jobs are designed to be resource-efficient
# Test the cron package
go test ./internal/cron/...
# Test the reset package
go test ./internal/auth/reset/...
# Test the entire application
go test ./...For development, you might want more frequent cleanup:
service:
cron:
enabled: true
password_reset_cleanup: "*/1 * * * *" # Every minute for testing- Service Not Starting: Check that
service.cron.enabledis set totrue - Invalid Cron Expression: Verify the cron expression format using online validators
- Database Connection: Ensure the database is accessible for cleanup operations
- Timezone Issues: Verify the timezone setting matches your deployment environment
The service provides detailed logging for troubleshooting:
INFO Cron service started successfully
INFO Password reset token cleanup job scheduled
INFO Running password reset token cleanup
INFO Token cleanup completed deleted_count=42 duration=125ms
If migrating from a custom cron implementation:
- Configuration: Update config to use the new cron section
- Dependencies: Ensure
github.com/robfig/cron/v3is installed - Custom Jobs: Use
cronService.AddCustomJob()for additional scheduled tasks - Testing: Verify all existing functionality works with the new implementation
This integration provides a robust, production-ready scheduled task system for the CService API.