β° Scheduled Tasks (Cron Jobs) in Squehub
Squehub supports running scheduled tasks using web-accessible cron routes instead of traditional CLI cron jobs. This approach works well on shared hosting or servers without shell access.
Best practice: Create a dedicated route file (e.g., project/routes/cron.php
) exclusively for defining all your cron or task URLs to keep your routing organized and secure.
βοΈ Defining Tasks with Routes
To create a task, define it inside a route closure using Task::schedule('task_name', callable $callback)
. Then call Task::run('task_name')
to execute the task. This keeps task definition and execution neatly encapsulated.
<?php
use App\Core\Task;
// In project/routes/cron.php (dedicated cron routes file)
$router->add('GET', '/crons/investments', function () {
Task::schedule('investments_update', function () {
// Task logic: update DB, send emails, etc.
echo "Running investments update task...\n";
});
Task::run('investments_update');
});
π Securing Your Cron Routes
Cron URLs trigger important operations and must be secured to prevent unauthorized use. Common security methods include:
- Using secret tokens or API keys as query parameters.
- Restricting access by IP address.
- Adding HTTP Basic Authentication.
<?php
$router->add('GET', '/crons/investment-secure', function () {
$expectedToken = 'super_secret_token_123';
$providedToken = $_GET['token'] ?? '';
if ($providedToken !== $expectedToken) {
http_response_code(403);
echo 'Forbidden: Invalid token';
return;
}
Task::schedule('investments_secure_task', function () {
echo "Running secured investments cron...\n";
});
Task::run('investments_secure_task');
});
π Setting Up Cron Jobs on Your Server
To automate your Squehub scheduled tasks, configure your server's cron scheduler (like crontab
) or control panel (such as cPanel) to hit your cron URLs at desired intervals.
cPanel Cron Setup
- Log in to your cPanel dashboard.
- Navigate to the Cron Jobs section.
- Create a new cron job and set the desired schedule (e.g., every 5 minutes, hourly).
- Set the command to use
wget
orcurl
to call your cron URL securely:
wget -q -O - "https://yourdomain.com/crons/investment-secure?token=super_secret_token_123"
# OR
curl --silent "https://yourdomain.com/crons/investment-secure?token=super_secret_token_123"
Linux Server Crontab Example
# Edit crontab with: crontab -e
# Run every day at 2am
0 2 * * * /usr/bin/curl --silent "https://yourdomain.com/crons/investment-secure?token=super_secret_token_123" > /dev/null 2>&1
π οΈ Advanced Usage & Best Practices
- Define multiple tasks: Schedule and run multiple named tasks in the same or separate routes.
- Extract complex logic: Keep routes clean by moving task logic to dedicated service classes or helper methods.
- Error handling: Wrap task code in try-catch blocks and log exceptions to avoid silent failures.
- Security first: Never expose cron URLs publicly without authentication or secret tokens.
- Idempotency: Design tasks so repeated runs donβt cause inconsistent data or side effects.
- Logging: Write task results and errors to log files or monitoring systems.
π Summary
- Create a dedicated routes file (e.g.,
project/routes/cron.php
) for all cron/task endpoints to organize your codebase. - Define and run tasks via
Task::schedule()
andTask::run()
inside route closures. - Secure routes with tokens, IP restrictions, or authentication.
- Configure your server or hosting panel to regularly call your cron URLs using
curl
orwget
. - Follow best practices for error handling, logging, and idempotent task design.