Yii2 队列扩展。支持 DB、Redis、RabbitMQ、Beanstalk 和 Gearman
[!重要]
php composer.phar require --prefer-dist yiisoft/yii2-queue发送到队列中的每个任务都应定义为一个独立的类。 例如,如果您需要下载并保存文件,类的代码可能如下所示:
class DownloadJob extends BaseObject implements \yii\queue\JobInterface
{
public $url;
public $file;
public function execute($queue)
{
file_put_contents($this->file, file_get_contents($this->url));
}
}以下是如何将任务发送到队列的过程:
Yii::$app->queue->push(new DownloadJob([
'url' => 'http://example.com/image.jpg',
'file' => '/tmp/image.jpg',
]));要将一个任务推入队列,该任务应在 5 分钟后运行:
// 将作业推入队列并获取消息 ID。
$id = Yii::$app->queue->push(new SomeJob());
// 检查作业是否正在等待执行。
Yii::$app->queue->isWaiting($id);
// 检查工作者是否从队列中获取了作业并进行了执行。
Yii::$app->queue->isReserved($id);
// 检查工作者是否已完成作业。暂无开放 Issues,或尚未同步最近议题。