text-media/background-task-bundle

Symfony BackgroundTaskBundle

安装量: 3,728

依赖项: 0

建议者: 0

安全: 0

类型:symfony-bundle

v2.0.0 2022-04-20 12:55 UTC

This package is auto-updated.

Last update: 2024-09-22 13:22:09 UTC


README

Packagist Packagist

Symfony用于后台任务的模块

安装

模块通过在composer中添加依赖项进行安装

composer require text-media/background-task-bundle

在doctrine配置中,需要描述一个名为 background_task 的Entry Manager,并单独配置连接

Kernel::registerBundles 中激活模块

自动或手动执行迁移

php ./bin/console doctrine:schema:update --em=background_task --dump-sql

使用

容器中至少应注册一个实现 ProviderInterface 接口的服务

<?php

namespace MyBundle;

use TextMedia\BackgroundTaskBundle\ProviderInterface;

class MyService implements ProviderInterface
{
    /**
     * Получить список обработчиков по именам задач.
     *
     * @return callable[]
     */
    public function getBackgroundCallbacks(): array
    {
        return [
            'task:trim' => 'trim',
            'task:pow' => 'pow'
        ];
    }
}

services:
    my_service:
        class: MyBundle\MyService
        tags:
            - { name: "background_task.provider", alias: "my_service"}

可以使用 TaskManagerInterface::addTask 方法创建后台任务

/** @var \TextMedia\BackgroundTaskBundle\TaskManagerInterface $taskManager */
$taskManager = $container->get('background_task.manager');
$taskManager->addTask(1, 'task:pow', [2,3]);

使用命令执行后台任务

php ./bin/console background:task:run

命令参数信息

php ./bin/console background:task:run -h

如果回调抛出 DeferredException 异常,则任务将被延迟