daanbiesterbos/job-queue-bundle
Composer 安装命令:
composer require daanbiesterbos/job-queue-bundle
包简介
Allows to run and schedule Symfony console commands as background jobs.
关键字:
README 文档
README
Installation
composer require daanbiesterbos/job-queue-bundle
Register Bundle
Add the bundle to bundles.php:
JMS\JobQueueBundle\JMSJobQueueBundle::class => ['all' => true],
Prepare Console Executable
Copy bin/console to bin/job-queue and use the JMSJobQueueBundle application instead the standard Symfony application.
# # Copy the console executable # cp bin/console bin/job-queue # # Open bin/job-queue in a text editor # # Look for this line: # use Symfony\Bundle\FrameworkBundle\Console\Application; # # And change it to: # use JMS\JobQueueBundle\Console\Application; vim bin/job-queue
Note that this is part of the original bundle. I would prefer a better solution that does not require an extra console. However, for now backward compatibility is more important considering the purpose of the fork.
Usage
Creating Jobs
Creating jobs is simple, you just need to persist an instance of Job:
$em->persist($job); $em->flush($job);
Adding Dependencies Between Jobs
If you want to have a job run after another job finishes, you can also achieve this quite easily:
$job->addDependency($job); $em->persist($job); $em->persist($dependentJob); $em->flush();
Schedule Job
If you want to schedule a job :
$job->add(new DateInterval('PT30M')); $job->setExecuteAfter($date); $em->persist($job); $em->flush();
Fine-grained Concurrency Control through Queues
If you would like to better control the concurrency of a specific job type, you can use queues: Queues allow you to enforce stricter limits as to how many jobs are running per queue. By default, the number of jobs per queue is not limited as such queues will have no effect (jobs would just be processed in the order that they were created in). To define a limit for a queue, you can use the bundle?s configuration:
jms_job_queue: queue_options_defaults: max_concurrent_jobs: 3 # This limit applies to all queues (including the default queue). # So each queue may only process 3 jobs simultaneously. queue_options: foo: max_concurrent_jobs: 2 # This limit applies only to the "foo" queue.
__ **Note: **Queue settings apply for each instance of the jms-job-queue:run command separately. There is no way to specify a global limit for all instances.
Prioritizing Jobs
By default, all jobs are executed in the order in which they are scheduled (assuming they are in the same queue). If you would like to prioritize certain jobs in the same queue, you can set a priority:
$job = new Job('a', array(), true, Job::DEFAULT_QUEUE, Job::PRIORITY_HIGH); $em->persist($job); $em->flush();
The priority is a simple integer - the higher the number, the sooner a job is executed.
Scheduled Jobs - JMSJobQueueBundle Documentation
This bundle also allows you to have scheduled jobs which are executed in certain intervals. This can either be achieved by implementing JMSJobQueueBundleCommandCronCommand in your command, or implementing JMSJobQueueBundleCronJobScheduler in a service and tagging the service with jms_job_queue.scheduler.
The jobs are then scheduled with the jms-job-queue:schedule command that is run as an additional background process. You can also run multiple instances of this command to ensure high availability and avoid a single point of failure.
Implement CronCommand
class MyScheduledCommand extends Command implements CronCommand { // configure, execute, etc. ... public function shouldBeScheduled(DateTime $lastRunAt) { return time() - $lastRunAt->getTimestamp() >= 60; // Executed at most every minute. } public function createCronJob(DateTime $lastRunAt) { return new Job('my-scheduled-command'); } }
For common intervals, you can also use one of the provided traits:
class MyScheduledCommand extends ContainerAwareCommand implements CronCommand { use ScheduleEveryMinute; // ... }
Implement JobScheduler
This is useful if you want to run a third-party command or a Symfony command as a scheduled command via this bundle.
class MyJobScheduler implements JobScheduler { public function getCommands(): array { return ['my-command']; } public function shouldSchedule($commandName, DateTime $lastRunAt) { return time() - $lastRunAt->getTimestamp() >= 60; // Executed at most every minute. } public function createJob($commandName, DateTime $lastRunAt) { return new Job('my-command'); } }
Links
- Documentation: Github Repository
- License: LICENSE
- Forked from: JMSJobQueueBundle
daanbiesterbos/job-queue-bundle 适用场景与选型建议
daanbiesterbos/job-queue-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.75k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「cron」 「background」 「job」 「scheduled」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 daanbiesterbos/job-queue-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daanbiesterbos/job-queue-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 daanbiesterbos/job-queue-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle to monitor and execute commands
Provides a background task processing module.
PHP AMQP Binding Library
A Laravel package to monitor queue jobs.
Scheduling extension for Yii2 framework
Monitoring for scheduled jobs
统计信息
- 总下载量: 19.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2021-04-19