Deployment: Cron Job Configuration
Relevant source files
The following files were used as context for generating this wiki page:
The PriceBuddy application utilizes a cron-based system for handling recurring background tasks, primarily for fetching product prices and sending notifications. This system is integrated into the Docker deployment and managed by supervisord to ensure that scheduled tasks run reliably.
The core of the system relies on a standard cron daemon running inside the PHP container, which in turn executes Laravel's built-in task scheduler. This allows for a flexible and maintainable approach to defining and managing scheduled jobs directly within the Laravel application code.
Architecture Overview
The background task execution is managed by supervisord, a process control system. It is responsible for starting and managing the cron daemon, the apache2 web server, and the Laravel queue:work process within the main PHP container.
The following diagram illustrates the process management structure within the container.
Supervisor Configuration
supervisord is configured via docker/php/supervisord.conf to manage three main long-running programs. This ensures that the web server, cron daemon, and queue worker are all running and monitored.
| Program | Command | Description |
|---|---|---|
apache2 | apachectl -D "FOREGROUND" | Starts the Apache web server in the foreground. |
cron | /start-cron.sh | Executes the script to start the cron daemon. Logs are directed to /var/log/cron.log. |
queue-worker | php artisan queue:work | Starts the Laravel queue worker to process jobs from the queue. Output is directed to stdout. |
Sources: docker/php/supervisord.conf:5-24
Cron Job Setup
The cron job functionality is built into the Docker image during the build process as defined in docker/php.dockerfile.
- A cron definition file
schedule-cronis copied into the container's/etc/cron.d/directory. This file contains the schedule and the command to run. - The
start-cron.shscript, which simply runscron -fto start the daemon in the foreground, is copied to/start-cron.sh. - Executable permissions are set for
/start-cron.sh, and read permissions are set for/etc/cron.d/schedule-cron.
Sources: docker/php.dockerfile:23-27
The start-cron.sh script is responsible for starting the cron daemon when the cron program is started by supervisord.
#!/bin/sh
set -e
# Start cron
cron -f
Sources: docker/php/start-cron.sh:1-5
Laravel Task Scheduling
The system cron job is configured to run Laravel's command scheduler (php artisan schedule:run) every minute. The app/Console/Kernel.php file is where the application's specific scheduled tasks are defined.
Scheduled Commands
The schedule method within app/Console/Kernel.php defines all the commands that are executed by the scheduler.
| Command | Schedule | Description |
|---|---|---|
buddy:scrape | Daily at the time specified in scrape_schedule_time settings. | Scrapes product pages to fetch the latest prices. The time is configurable via the application settings. |
model:prune | Daily at 01:00. | Prunes models that are no longer needed, such as old UrlResearch entries. |
log:clear | Daily at 01:30. | Clears old log entries from the database based on the log_retention_days setting. |
Sources: app/Console/Kernel.php:21-34, app/Filament/Pages/AppSettingsPage.php:61-65
Execution Flow
The following sequence diagram shows the flow from the system cron triggering the Laravel scheduler to the execution of a defined command.
The scrape_schedule_time is a user-configurable setting found in the application's settings page, allowing administrators to define the time of day when product prices are fetched.
Sources: app/Filament/Pages/AppSettingsPage.php:61-65
Summary
The cron job configuration in PriceBuddy is a robust system managed by supervisord within a Docker container. It leverages a standard cron daemon to trigger Laravel's powerful and flexible task scheduler. This architecture allows developers to easily define and manage recurring background tasks like price scraping and database maintenance directly in the PHP code, while providing administrators with the ability to configure key schedule parameters through the application's UI.