vtitar/background-tasks-symfony 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vtitar/background-tasks-symfony

最新稳定版本:v0.1.2

Composer 安装命令:

composer require vtitar/background-tasks-symfony

包简介

Symfony BackgroundTasksBundle. Create and process tasks that will be executed via cron. Could be configured time when execute, separate process for specific task groups.

README 文档

README

Symfony BackgroundTasksBundle. Create and process tasks that will be executed via cron. Could be configured time when task should be executed, separate process for specific task groups.

2. Installation

Install bundle

composer require vtitar/background-tasks-symfony

Create db after installation

./bin/console doctrine:schema:update

3. Configuration

  • Add new cron config to crontab - will execute all tasks without specific group

* * * * * /usr/bin/flock -n /home/project/path/var/locks/cron-background-tasks.lock /home/project/path/bin/console tit:background-task:run > /home/project/path/var/log/cron-background-tasks.log 2>&1

  • In case you need to have separate process for some tasks to not block/wait main process - configure separate cron for tasks with specific group only

* * * * * /usr/bin/flock -n /home/project/path/var/locks/cron-background-tasks-group-test.lock /home/project/path/bin/console tit:background-task:run --group_code=test-group > /home/project/path/var/log/cron-background-tasks-group-test.log 2>&1

4. How to use

4.1 Add new entity to execution list

4.1.1 Add via entityManager

New entity could be added via entityManager like

$task = new BackgroundTask();
$task->setCreatedAt(new DateTimeImmutable());
...
$this->entityManager->persist($task);
$this->entityManager->flush();

4.1.2 Add via BackgroundTaskManager

Or BackgroundTaskManager could be used. Add tit.background_tasks.manager.background_task service to your service where you want to use it. Add to controller like

public function __construct(
    private readonly BackgroundTaskManager      $backgroundTaskManager
){}

And add new task

$params = [
    'request_id' => 123
];

$this->backgroundTaskManager->addNewTask(
    'tit.test.test', # service that will process task
    'handle', # function that will process task
    $params,
    '',
    null,
    50
);

$this->backgroundTaskManager->saveToDb();

4.2 Add service and function that should process task

4.2.1 Service should be public

<service id="tit.test.test" class="Tit\Bundle\Service\Test" public="true" >
    <argument type="service" id="Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface" />
</service>

4.2.2 Add function that will process task

public function handle(array $params): void
{
    $requestId = $params['request_id'];
    // do whatever you need
}

4.3 Errors checking

You could easily check is task executed okay via db background_task.status column.

  • error - status should be -1. Error should be saved in background_task.last_error column.
  • if you want to add task back to queue - set status to 0

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-25