Overview
The Job Queue system in S-PHP allows you to queue tasks for later execution, particularly useful for time-consuming operations like sending emails. Jobs are stored in a JSON file and can be executed asynchronously.Creating Jobs
Job Class
TheJob class represents a single task to be executed:
Job Structure
Each job contains:taskName- The name of the task (e.g., “Send Email”)taskData- Array of data needed to execute the task
JobQueue Service
TheJobQueue class manages the job queue:
Adding Jobs to Queue
Executing Jobs
Execute all queued jobs:- Retrieves all jobs from the queue
- Executes each job based on its task name
- Removes successfully executed jobs from the queue
Retrieving Jobs
Get all jobs currently in the queue:Removing Jobs
Remove a specific job by ID:Built-in Task Types
Send Email Task
The framework includes built-in support for email sending:- Uses the SMTPMailer service
- Sends from
[email protected]by default - Supports HTML email content
Storage
Jobs are stored in:Custom Task Types
Extend theJob class to add custom task types:
Error Handling
Jobs handle errors gracefully:Best Practices
- Queue Long-Running Tasks - Use the job queue for operations that take time
- Error Logging - Always wrap job execution in try-catch blocks
- Queue Cleanup - Failed jobs should be logged and removed from the queue
- Data Validation - Validate taskData before adding jobs to the queue
Integration with SendMail
TheSendMail service automatically uses the job queue:
- Creates a job
- Adds it to the queue
- Executes the job immediately