Delayed Job PHP
Simple, efficient background processing for PHP uses threads to handle many jobs., (*1)
Requirements
- PHP 8.1+
- Redis 2.2+
- Composer
Installation
composer require pnixx/delayed_job
Worker process
Simple run worker process in background:, (*2)
bin/run start
Extend run worker process:, (*3)
bin/run start --process 5 --queue mailer -i /path/to/init.php
For list all commands, please use --help or -h argument., (*4)
For restart process after deploy use --restart or -r argument. A new process will be waiting finish all running processes., (*5)
Jobs
Job class required include perform method:, (*6)
class TestJob extends PNixx\DelayedJob\Job {
public function perform($args = []): void {
//Work process
}
}
Any exception thrown by a job will be returned job to work with timeout.
If you want set limit attempt for retries job, set $attempt in you class., (*7)
Jobs can also have setup and completed methods. If a setup method is defined, it will be called before the perform method.
The completed method will be called after success job., (*8)
class TestJob extends PNixx\DelayedJob\Job {
/**
* Queue for publishing Job
*/
public static string $queue = 'mailer';
/**
* Attempt count used for only delayed tasks
* default: 0 - always repeat until it reach success
*/
public static $attempt = 0;
public function setup() {
//Setup this job
}
public function perform($args = []): void {
//Work process
}
public function completed() {
//Complete job callback
}
}
Queueing jobs
Jobs can run in current thread uses now method. If you use this, you can handle the exceptions in a job failing., (*9)
//Run job in this thread without arguments
TestJob::now();
//Run job in this thread with arguments
TestJob::now(['name' => 'Jane']);
Jobs can run in a background thread or add to scheduler., (*10)
//Run job in a background
TestJob::later();
//Run job in a background with arguments
TestJob::later(['name' => 'Jane']);
//Add job in a scheduler.
TestJob::later(['name' => 'Jane'], strtotime('+1 day'));
Signals
-
QUIT - Wait for job to finish processing then exit
-
TERM / INT - Immediately kill job then exit without saving data
Author
Sergey Odintsov, @pnixx, (*11)