fyre/queue
Composer 安装命令:
composer require fyre/queue
包简介
A Queue library.
README 文档
README
FyreQueue is a free, open-source queue library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/queue
In PHP:
use Fyre\Queue\QueueManager;
Basic Usage
$queueManager = new QueueManager($container, $config);
Default configuration options will be resolved from the "Queue" key in the Config.
Autoloading
It is recommended to bind the QueueManager to the Container as a singleton.
$container->singleton(QueueManager::class);
Any dependencies will be injected automatically when loading from the Container.
$queueManager = $container->use(QueueManager::class);
Methods
Build
Build a Queue.
$optionsis an array containing configuration options.
$queue = $queueManager->build($options);
Queue dependencies will be resolved automatically from the Container.
Clear
Clear all instances and configs.
$queueManager->clear();
Get Config
Set a Queue config.
$keyis a string representing the Queue key.
$config = $queueManager->getConfig($key);
Alternatively, if the $key argument is omitted an array containing all configurations will be returned.
$config = $queueManager->getConfig();
Has Config
Determine whether a Queue config exists.
$keyis a string representing the Queue key, and will default toQueueManager::DEFAULT.
$hasConfig = $queueManager->hasConfig($key);
Is Loaded
Determine whether a Queue instance is loaded.
$keyis a string representing the Queue key, and will default toQueueManager::DEFAULT.
$isLoaded = $queueManager->isLoaded($key);
Push
Push a job to a Queue.
$classNameis a string representing the job class.$argumentsis an array containing arguments that will be passed to the job.$optionsis an array containing options for the Message.configis a string representing the configuration key, and will default toQueueManager::DEFAULT.queueis a string representing the queue name, and will default toQueueManager::DEFAULT.methodis a string representing the class method, and will default to "run".delayis a number representing the number of seconds before the job should run, and will default to 0.expiresis a number representing the number of seconds after which the job will expire, and will default to 0.retryis a boolean indicating whether the job should be retried if it fails, and will default to true.maxRetriesis a number indicating the maximum number of times the job should be retried, and will default to 5.uniqueis a boolean indicating whether the job should be unique, and will default to false.
$queueManager->push($className, $arguments, $options);
Job dependencies will be resolved automatically from the Container.
Set Config
Set the Queue config.
$keyis a string representing the Queue key.$optionsis an array containing configuration options.
$queueManager->setConfig($key, $options);
Unload
Unload a Queue.
$keyis a string representing the Queue key, and will default toQueueManager::DEFAULT.
$queueManager->unload($key);
Use
Load a shared Queue instance.
$keyis a string representing the Queue key, and will default to "default".
$queue = $queueManager->use($key);
Queue dependencies will be resolved automatically from the Container.
Queues
You can load a specific queue by specifying the className option of the $options variable above.
Custom queues can be created by extending \Fyre\Queue\Queue, ensuring all below methods are implemented.
Clear
Clear all items from the queue.
$queueNameis a string representing the queue name, and will default toQueueManager::DEFAULT.
$queue->clear($queueName);
Complete
Mark a job as completed.
$messageis a Message.
$queue->complete($message);
Fail
Mark a job as failed.
$messageis a Message.
$queue->fail($message);
Pop
Pop the last item off the queue.
$queueNameis a string representing the queue name.
$message = $queue->pop($queueName);
Push
Push a job onto the queue.
$messageis a Message.
$queue->push($message);
Queues
Get all the active queues.
$queues = $queue->queues();
Reset
Reset the queue statistics.
$queueNameis a string representing the queue name, and will default toQueueManager::DEFAULT.
$queue->reset($queueName);
Stats
Get the statistics for a queue.
$queueNameis a string representing the queue name, and will default toQueueManager::DEFAULT.
$stats = $queue->stats($queueName);
Redis
The Redis queue can be loaded using custom configuration.
$optionsis an array containing configuration options.classNamemust be set to\Fyre\Queue\Handlers\RedisQueue.hostis a string representing the Redis host, and will default to "127.0.0.1".passwordis a string representing the Redis passwordportis a number indicating the Redis port, and will default to 6379.databaseis a string representing the Redis database.timeoutis a number indicating the connection timeout.persistis a boolean indicating whether to use a persistent connection, and will default to true.tlsis a boolean indicating whether to use a tls connection, and will default to true.sslis an array containing SSL options.keyis a string representing the path to the key file.certis a string representing the path to the certificate file.cais a string representing the path to the certificate authority file.
$container->use(Config::class)->set('Queue.redis', $options);
Workers
Workers are long running tasks that will consume and execute jobs from the queue.
use Fyre\Queue\Worker;
$containeris a Container.$queueManageris a QueueManager.$eventManageris an EventManager.$optionsis an array containing configuration options.configis a string representing the configuration key, and will default toQueueManager::DEFAULT.queueis a string representing the queue name, and will default toQueueManager::DEFAULT.maxJobsis a number representing the maximum number of jobs to execute, and will default to 0.maxRuntimeis a number representing the maximum number of seconds the worker should run, and will default to 0.restis a number representing the number of microseconds to rest after processing a job, and will default to 10000.sleepis a number representing the number of microseconds to sleep if no jobs are in the queue, and will default to 100000.
$worker = new Worker($container, $queueManager, $eventManager, $options);
Run
Run the worker.
$worker->run();
Messages
Messages are used internally to pass data between the Queue and Worker.
use Fyre\Queue\Message;
$optionsis an array containing options for the message.classNameis a string representing the job class.argumentsis an array containing arguments that will be passed to the job.configis a string representing the configuration key, and will default toQueueManager::DEFAULT.queueis a string representing the queue name, and will default toQueueManager::DEFAULT.methodis a string representing the class method, and will default to "run".delayis a number representing the number of seconds before the job should run, and will default to 0.expiresis a number representing the number of seconds after which the job will expire, and will default to 0.retryis a boolean indicating whether the job should be retried if it fails, and will default to true.maxRetriesis a number indicating the maximum number of times the job should be retried, and will default to 5.uniqueis a boolean indicating whether the job should be unique, and will default to false.
$message = new Message($options);
Get After
Get the timestamp when the message can be sent.
$after = $message->getTimestamp();
Get Config
Get the message config.
$config = $message->getConfig();
Get Hash
Get the message hash.
$hash = $message->getHash();
Get Queue
Get the message queue.
$queueName = $message->getQueue();
Is Expired
Determine whether the message has expired.
$isExpired = $message->isExpired();
Is Ready
Determine whether the message is ready.
$isReady = $message->isReady();
Is Unique
Determine whether the message is unique.
$isUnique = $message->isUnique();
Is Valid
Determine whether the message is valid.
$isValid = $message->isValid();
Should Retry
Determine whether the message should be retried.
$shouldretry = $message->shouldRetry();
Commands
Stats
Display stats for the queue.
--configis a the configuration key, and will default toQueueManager::DEFAULT.--queueis a the queue name, and will default toQueueManager::DEFAULT.
$commandRunner->run('queue:stats', ['--config', 'default', '--queue', 'default']);
Worker
Start a background queue worker.
--configis a the configuration key, and will default toQueueManager::DEFAULT.--queueis a the queue name, and will default toQueueManager::DEFAULT.--max-jobsis the maximum number of jobs to execute, and will default to 0.--max-runtimeis the maximum number of seconds the worker should run, and will default to 0.
$commandRunner->run('queue:worker', ['--config', 'default', '--queue', 'default', '--max-jobs', '99', '--max-runtime', '60']);
fyre/queue 适用场景与选型建议
fyre/queue 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 86 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 fyre/queue 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fyre/queue 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 86
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-08