glorpen/queue-bundle

任务队列

v0.3.3 2015-05-08 05:38 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:21:45 UTC


README

注意:该仓库不再维护。

用于将工作卸载到服务器端的组件。可以将服务任务/作业添加到队列中,以供稍后执行。

队列运行可以安全重叠,因为任务在被运行者获取时会锁定。

关于分叉和其他有趣的事情

安装

  • 将需求添加到 composer.json
{
    "require": {
        "glorpen/queue-bundle": "*"
    }
}
  • 在您的 AppKernel 类中启用该组件

app/AppKernel.php

<?php

class AppKernel extends AppKernel
{
   public function registerBundles()
   {
       $bundles = array(
           ...
           new Glorpen\QueueBundle\GlorpenQueueBundle(),
           ...
       );
   }
}
  • 在 config.yml 中选择后端

后端

Propel

组件配置

glorpen_queue:
    backend: propel

使用方法

要添加新任务

<?php
$queue = $container->get("glorpen.queue");
$queue->create('my.tasks_container', 'myMethod', array("arg1", 2));

然后执行使用 app/console queue:run

Starting task test:method
Task 1:test:method ended after 0 seconds with status "failed"

请记住

  • 数据集在任务执行时获取,因此创建任务后可能会发生变化。
  • 任务参数将被序列化并存储在所选后端

其他有用的命令

  • queue:restart-failed 将失败的任务简单地标记为挂起
  • queue:update 将崩溃的任务(例如,在 OOM 时)标记为失败并删除成功的任务

命名任务

要添加新的命名任务

<?php
$queue = $container->get("glorpen.queue");
$queue->create('my.tasks_container', 'myMethod', array("arg1", 2), 'now', 'my_named_task');

然后您可以使用以下方式检索它

<?php
$queue = $container->get("glorpen.queue");
$task = $queue->getTask('my_named_task');
echo $task->getStatus();
echo $task->getProgress();

在创建命名任务时

  • 具有相同名称的已完成或未开始的任务将被删除
  • 如果旧任务目前正在运行,则会抛出异常

元数据

在执行任务时,您可以为其实时设置其 当前进度

<?php
$queue = $container->get("glorpen.queue");
$queue->setCurrentTaskProgress(50);