Cronjobs
Overview
Scheduled background tasks (cron jobs) are essential for automating recurring operations in your application. This guide covers implementation approaches for containerized applications deployed on the Sherpa.sh platform.
How Cron Jobs Work on Sherpa.sh
Platform Architecture
On Sherpa.sh, your applications run as containerized workloads managed by Kubernetes clusters. This architecture provides the perfect foundation for running reliable background cron jobs alongside your web application.
Key Platform Benefits:
Automatic Scaling: Kubernetes automatically scales your containers based on resource usage
High Availability: Multiple redundant control planes ensure your cron jobs continue running
Container Isolation: Jobs run in identical environments across all instances
Global Distribution: Jobs can run across multiple regions for reliability
Deployment Model
Your application is deployed as a Docker container with the following characteristics:
Single Container: Both your web server and cron jobs run in the same container instance
Persistent Process: The container's entrypoint keeps both services running simultaneously
Shared Resources: Web requests and background jobs share the same application context
Auto-restart: Kubernetes automatically restarts containers if they fail
Request vs Background Job Execution
Understanding the execution model is crucial for proper cron job implementation:
HTTP Requests
60 seconds
API endpoints, page rendering
Background Jobs
Indefinite
Data processing, cleanup, reports
This means your cron jobs can run for hours while HTTP requests are capped at 60 seconds.
Quick Setup Guide
Step 1: Configure Your Container Entrypoint
Structure your application to handle both web traffic and scheduled tasks:
Step 2: Update Your Dockerfile
Ensure your container is configured for long-running processes:
Step 3: Deploy to Sherpa.sh
Deploy your application normally - the platform will automatically:
Build your Docker container
Deploy to Kubernetes clusters
Start your web server and cron jobs
Scale based on traffic and resource usage
Implementation Strategies
Prerequisites
Active Sherpa.sh account
Supported runtime: Node.js, Python, Go, or other containerized applications
Git repository connected to Sherpa.sh
Basic understanding of cron syntax
Platform-Specific Considerations
Resource Management
Your cron jobs share resources with your web application. Monitor memory and CPU usage to ensure optimal performance:
Environment Variables
Access your application's environment variables within cron jobs:
Logging Integration
Leverage Sherpa.sh's logging infrastructure for cron job monitoring:
Basic Cron Implementation
Any standard cron library works well within this architecture:
Node.js Example
Python Example
Python Code for Scheduling Weekly Reports
The following Python code snippet utilizes APScheduler
to schedule a weekly report generation every Monday at 9 AM. The job is handled in a background scheduler, which is gracefully shut down at exit.
Key Points:
BackgroundScheduler
: Runs jobs in the background thread, making it suitable for web applications.Cron Trigger: Configured to execute the job every Monday at 9 AM.
Graceful Shutdown: Uses
atexit
to register a shutdown command ensuring clean teardown of the scheduler.
Enhanced Visibility with Schedo.dev
For production applications requiring better observability, monitoring, and reliability, we recommend Schedo.dev - a distributed cron job platform designed for modern development teams.
Why Schedo.dev?
Built-in Reliability: Automatic retries, error tracking, and comprehensive failure handling eliminate common cron job pitfalls.
Zero Infrastructure Management: No DevOps setup required - focus on business logic while Schedo handles scaling, concurrency, and infrastructure.
Complete Observability: Real-time execution logs, performance metrics, and failure alerts provide full visibility into job execution.
Distributed Execution: Built-in distributed locking ensures jobs run exactly once across your entire infrastructure, preventing race conditions.
Developer Experience: Local development support with the same API as production, plus seamless environment management.
Schedo.dev Integration
When to Choose Schedo.dev
Recommended for:
Production applications with critical scheduled tasks
Teams needing detailed job monitoring and alerting
Applications requiring distributed job execution
Projects where job reliability is essential
Basic cron is sufficient for:
Development and testing environments
Simple, non-critical background tasks
Applications with minimal observability requirements
Best Practices
Error Handling
Logging and Monitoring
Resource Management
Environment Configuration
Configure different schedules for different environments:
Troubleshooting
Common Issues
Jobs not executing: Verify cron syntax and ensure the container process stays alive.
Memory leaks: Monitor memory usage in long-running jobs and implement proper cleanup.
Timezone issues: Explicitly set timezone in cron configuration or use UTC consistently.
To schedule a timezone-aware daily task in JavaScript using node-cron
, you can use the following code:
Debugging
Here's the updated JavaScript code with comprehensive logging:
Next Steps
Implement basic cron jobs for your immediate needs
Consider Schedo.dev for production applications requiring enhanced reliability
Set up proper monitoring and alerting for critical scheduled tasks
Review and optimize job performance regularly
Last updated