vtitar/background-tasks-symfony
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_errorcolumn. - if you want to add task back to queue - set
statusto 0
vtitar/background-tasks-symfony 适用场景与选型建议
vtitar/background-tasks-symfony 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 vtitar/background-tasks-symfony 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vtitar/background-tasks-symfony 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-25