Files
jobifygarden/CRON_SETUP.md
T
2026-04-04 18:52:19 +03:00

261 lines
7.0 KiB
Markdown

# Cron Job Setup for JobifyGarden
Since your hosting provider doesn't have Supervisor, you'll use cron jobs to handle scheduled tasks and queue processing.
## Prerequisites
1. SSH access to your server
2. Access to crontab configuration
3. Verify your application path (likely `/home/jobifyg/public_html` or similar)
## Setup Instructions
### Step 1: Make Queue Worker Script Executable
```bash
cd /home/jobifyg/public_html
chmod +x queue-worker.sh
```
### Step 2: Configure Crontab
Edit your crontab:
```bash
crontab -e
```
Add these lines (replace `/home/jobifyg/public_html` with your actual path):
```bash
# =============================================================================
# JobifyGarden Cron Jobs
# =============================================================================
# Laravel Scheduler - REQUIRED
# Runs every minute to process scheduled commands
* * * * * cd /home/jobifyg/public_html && php artisan schedule:run >> /dev/null 2>&1
# Queue Worker - REQUIRED
# Option 1: Using the helper script (recommended)
* * * * * /home/jobifyg/public_html/queue-worker.sh
# Queue Worker - Option 2: Direct command (if script doesn't work)
# * * * * * cd /home/jobifyg/public_html && php artisan queue:work --max-time=50 --stop-when-empty --sleep=3 --tries=3 >> storage/logs/queue.log 2>&1
# Queue Restart - OPTIONAL but RECOMMENDED
# Restarts queue workers every hour to prevent memory leaks
0 * * * * cd /home/jobifyg/public_html && php artisan queue:restart >> /dev/null 2>&1
# Clear old failed jobs - OPTIONAL
# Clears failed jobs older than 7 days, runs daily at 4 AM
0 4 * * * cd /home/jobifyg/public_html && php artisan queue:flush >> /dev/null 2>&1
```
### Step 3: Verify Cron Jobs Are Running
After saving your crontab, verify it's active:
```bash
# List your cron jobs
crontab -l
# Check if cron jobs are running (wait a few minutes)
tail -f /home/jobifyg/public_html/storage/logs/laravel.log
# Check queue worker logs
tail -f /home/jobifyg/public_html/storage/logs/queue-worker.log
```
## What Each Cron Job Does
### 1. Laravel Scheduler (`* * * * * ... schedule:run`)
**Frequency:** Every minute
**Purpose:** Checks and runs scheduled tasks defined in `bootstrap/app.php`
**Scheduled Tasks:**
- `jobs:fetch` - Fetches new jobs from APIs every 2 hours
- `jobs:enrich` - Auto-categorizes and enhances jobs every 6 hours
- `jobs:clean` - Removes old jobs (45+ days) daily at 2 AM
- `jobs:deduplicate` - Removes duplicate jobs daily at 3 AM
- `cache:prune-stale-tags` - Cleans cache hourly
For detailed explanations and customization options, see [SCHEDULED_TASKS.md](SCHEDULED_TASKS.md)
### 2. Queue Worker (`* * * * * ... queue:work`)
**Frequency:** Every minute
**Purpose:** Processes background jobs from the queue
**Jobs Processed:**
- Fetching jobs from external APIs
- Processing individual job listings
- Email notifications (if configured)
- Cache warming
- Other background tasks
**Options Explained:**
- `--max-time=50` - Worker runs for max 50 seconds then exits
- `--stop-when-empty` - Exits if no jobs are in queue
- `--sleep=3` - Waits 3 seconds between jobs
- `--tries=3` - Retries failed jobs up to 3 times
### 3. Queue Restart (`0 * * * * ... queue:restart`)
**Frequency:** Every hour
**Purpose:** Gracefully restarts queue workers to prevent memory leaks
### 4. Queue Flush (`0 4 * * * ... queue:flush`)
**Frequency:** Daily at 4 AM
**Purpose:** Clears old failed jobs from the database
## Troubleshooting
### Cron jobs not running?
1. **Check crontab is saved:**
```bash
crontab -l
```
2. **Check PHP path:**
```bash
which php
# Use the full path in crontab if needed: /usr/bin/php artisan ...
```
3. **Check application path:**
```bash
pwd
# Make sure you're using the correct path in crontab
```
4. **Check file permissions:**
```bash
ls -la /home/jobifyg/public_html/artisan
chmod +x /home/jobifyg/public_html/artisan
```
### Jobs not being processed?
1. **Check queue worker logs:**
```bash
tail -f storage/logs/queue-worker.log
```
2. **Check Laravel logs:**
```bash
tail -f storage/logs/laravel.log
```
3. **Manually run queue worker to see errors:**
```bash
php artisan queue:work --verbose
```
4. **Check failed jobs:**
```bash
php artisan queue:failed
```
5. **Retry failed jobs:**
```bash
php artisan queue:retry all
```
### Jobs are fetched but not appearing?
1. **Check if job fetch is scheduled:**
```bash
php artisan schedule:list
```
2. **Manually test job fetching:**
```bash
php artisan jobs:fetch --limit=5
```
3. **Check queue:**
```bash
php artisan queue:work --once --verbose
```
## Performance Tuning
### Low Traffic Site
If your site has low traffic, you can reduce frequency:
```bash
# Run queue worker every 5 minutes instead
*/5 * * * * cd /home/jobifyg/public_html && php artisan queue:work --max-time=240 --stop-when-empty --sleep=3 --tries=3 >> storage/logs/queue.log 2>&1
```
### High Traffic Site
If you need faster job processing:
```bash
# Run multiple queue workers
* * * * * cd /home/jobifyg/public_html && php artisan queue:work --max-time=50 --stop-when-empty --queue=high,default --sleep=1 --tries=3 >> storage/logs/queue-worker-1.log 2>&1
* * * * * cd /home/jobifyg/public_html && php artisan queue:work --max-time=50 --stop-when-empty --queue=default --sleep=1 --tries=3 >> storage/logs/queue-worker-2.log 2>&1
```
## Monitoring
### Check Cron Execution
Your hosting control panel may have a cron job log. Check there for execution history.
### Check Application Health
```bash
# Visit this URL in your browser (Laravel health check)
https://jobifygarden.com/up
# Check recent jobs
php artisan tinker
>>> \App\Models\Job::latest()->take(5)->get(['id', 'title', 'created_at']);
```
### Monitor Logs
```bash
# Watch all logs in real-time
tail -f storage/logs/*.log
# Check for errors
grep -i error storage/logs/laravel.log | tail -20
```
## Important Notes
1. **Queue Worker Must Keep Running**: The queue worker cron ensures jobs are processed continuously. Without it, jobs will queue but never execute.
2. **Schedule Must Run Every Minute**: The scheduler cron must run every minute. Laravel internally determines which scheduled tasks should run.
3. **Time Zone**: Make sure your server's timezone matches your Laravel app timezone (check `config/app.php`).
4. **Testing**: After setup, wait a few minutes and check logs to verify everything is working.
5. **Production**: Make sure `APP_ENV=production` and `APP_DEBUG=false` in your `.env` file.
## Quick Test
After setting up cron, test immediately:
```bash
# 1. Manually run scheduler
php artisan schedule:run
# 2. Queue a test job
php artisan jobs:fetch Remotive --limit=2
# 3. Process the queue
php artisan queue:work --once --verbose
# 4. Check if jobs were created
php artisan tinker
>>> \App\Models\Job::count();
```
## Support
If you encounter issues:
1. Check the troubleshooting section above
2. Review logs in `storage/logs/`
3. Ensure Redis is working: `php artisan tinker` then `Cache::get('test')`
4. Verify database connection works