tomatom / jobqueuebundle
适用于 Symfony 6+ 的 JMSJobQueueBundle 的替代品
v1.1.1
2024-09-19 16:49 UTC
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^2
- doctrine/orm: ^2|^3
- symfony/framework-bundle: ^6.4
- symfony/messenger: ^6.4
- symfony/process: ^6.4
- symfony/translation: ^6.4
- twig/twig: ^3
README
Symfony Bundle,旨在用 Symfony messenger 替换 JMSJobQueueBundle 的控制台命令调度。
依赖关系
- php: >=8.1
- doctrine/doctrine-bundle: ^2
- doctrine/orm: ^2|^3
- symfony/framework-bundle: ^6.4
- symfony/messenger: ^6.4
- symfony/process: ^6.4
- symfony/translation: ^6.4
- twig/twig: ^3
安装
composer require tomatom/jobqueuebundle
配置
config/bundles.php
TomAtom\JobQueueBundle\JobQueueBundle::class => ['all' => true]
config/routes.yaml
job_queue: resource: path: '../vendor/tomatom/jobqueuebundle/src/Controller' namespace: TomAtom\JobQueueBundle\Controller type: attribute
config/packages/messenger.yaml
您可以为作业消息创建自己的传输 - 或者直接使用 async 传输
framework: messenger: # Your messenger config transports: # Your other transports job_message: dsn: "%env(MESSENGER_TRANSPORT_DSN)%" options: queue_name: job_message routing: 'TomAtom\JobQueueBundle\Message\JobMessage': job_message # or async
更新您的数据库,以便创建 ‘job_queue’ 表
php bin/console d:s:u --complete --force
或者如果您使用迁移,也可以通过迁移进行。
使用方法
手动在您的应用程序中创建作业
来自 CommandJobFactory 的 createCommandJob 函数接受
- 命令名称,
- 命令参数,
- 相关实体 ID(可选)
- 相关实体类名称 - self::class(可选)
并返回创建的作业 ID,例如
$commandName = 'app:your:command'; $params = [ '--param1=' . $request->get('param1'), '--param2=' . $request->get('param2'), ]; // Try to create the command job try { $job = $this->commandJobFactory->createCommandJob($commandName, $params, $entity->getId(), Entity::class); } catch (OptimisticLockException|ORMException|CommandJobException $e) { // Redirect back upon failure $this->logger->error('createCommandJob error: ' . $e->getMessage()); return $this->redirectToRoute('your_route'); } // Redirect to the command job detail return $this->redirectToRoute('job_queue_detail', ['id' => $job->getId()]);
通过浏览器界面创建作业
在 /command 网址上,您可以安排应用程序中的所有命令(包括 Symfony 命令)
在 /job/list 网址上,您可以看到所有作业
在 /job/{id} 网址上,您可以看到每个作业的详细信息
注意 - 设计可能会变得更好,但您可以创建自己的。
扩展模板可以这样做
{# templates/job/detail.html.twig #} {% extends '@JobQueue/job/detail.html.twig' %} {% block title %}...{% endblock %} {% block header %}...{% endblock %} {% block body %}...{% endblock %}
要更改或添加新区域的翻译,请在您的翻译/messages.{locale}.yaml 中使用这些翻译变量
(目前只有 en 和 cs 区域的翻译)
job: job_list: "Job list" create_job: "Create job" detail: self: "Detail" title: "Planned job" refresh: "Refresh" runtime: "Runtime" closed: "Closed" output: "Output" list: title: "Planned jobs" related_entity: "Entity ID" back_to_list: "Back to jobs" header: for_entity_with_id: "for entity with ID" for: "for" command: "Command" state: "State" created: "Created" runtime: hours: "hours" minutes: "minutes" seconds: "seconds" already_exists: "The same job is already planned." creation: success: "Job successfully planned." error: "An error occurred while planning the job" command: title: "Command schedule" select: "Select a command to run" select.label: "Command" schedule.job: "Schedule command job" params.label: "Write the command's parameters as you would normally in the console" available: "Available command" argument: "Argument" arguments: "arguments" option: "Option" options: "options" description: "Description" mode: "Mode" shortcut: "Shortcut"
待办事项
添加诸如表名之类的配置
贡献
如果您发现有任何错误或缺少您希望该包拥有的功能,请随时打开任何问题或拉取请求!