efabrica/hermes-extension
Composer 安装命令:
composer require efabrica/hermes-extension
包简介
Extension for tomaj/hermes
README 文档
README
Extension for tomaj/hermes. It contains:
HermesWorker(symfony command)- Drivers (
RedisProxybased):RedisProxySetDriverRedisProxySortedSetDriverRedisProxyListDriverRedisProxyStreamDriver
- Other drivers:
DummyDriver(for testing purposes)
- Heartbeat functionality with drivers
RedisProxyStorageMemoryStorage(for testing purposes)
Installation
composer require efabrica/hermes-extension
Quick setup
use Efabrica\HermesExtension\Driver\RedisProxySetDriver; use Efabrica\HermesExtension\Heartbeat\RedisProxyStorage; use RedisProxy\RedisProxy; use Tomaj\Hermes\Dispatcher; use Tomaj\Hermes\Handler\EchoHandler; use Tomaj\Hermes\Shutdown\SharedFileShutdown; $redisProxy = new RedisProxy(/*...*/); // Register driver $driver = new RedisProxySetDriver($redisProxy, 'hermes'); // Optionally register shutdown and / or heartbeat $driver->setShutdown(new SharedFileShutdown('/tmp/hermes_shared_file')); $driver->setHeartbeat(new RedisProxyStorage($redisProxy, 'hermes_heartbeat')); // Create Dispatcher $dispatcher = new Dispatcher($driver); // Register handlers $dispatcher->registerHandler('event_type', new EchoHandler());
Register hermes worker to symfony console application:
// file: bin/command use Efabrica\HermesExtension\Command\HermesWorker; use Symfony\Component\Console\Application; $application = new Application(); $application->add(new HermesWorker($dispatcher)); $application->run();
Run command:
php bin/command hermes:worker
Submit message:
use Tomaj\Hermes\Emitter; use Tomaj\Hermes\Message; // Create Emitter $emitter = new Emitter($driver); // Send message: $emitter->emit(new Message( 'event_type', ['body' => 'This is a message!'] ));
RedisProxy drivers
Common driver properties:
- can define one (default) or more priority queues
- can have shutdown functionality defined to stop their work
- can have heartbeat functionality defined to track activity
- can be stopped automatically after processing a given number of messages
- can be stopped by system signals (
SIGTERM,SIGINT,SIGQUITandSIGHUP) - can be configured to fork before message handler execution (handler runs in child process) [POSIX systems only]
- handlers executed by drivers can access the message data using the
HermesDriverAccessorsingleton class
RedisProxySetDriver
- Simple driver that uses Redis set(s) to deliver/process messages.
- This driver can't use delayed execution.
- A message acquired by the
Dispatcheris immediately taken out of the queue and possibly lost when the dispatcher crashes or is force-stopped. - There is no real-time monitoring.
RedisProxySortedSetDriver
- Simple driver that uses Redis sorted set(s) to deliver/process messages.
- This driver can use delayed execution (message is executed after the
executeAttimestamp at any moment). The message's priority is lost in this process; a message that moves from the delayed queue to the processing queue will have default priority assigned. - A message acquired by the
Dispatcheris immediately taken out of the queue and possibly lost when the dispatcher crashes or is force-stopped. There is a small but real chance that a delayed message may be lost if the process is force-stopped. - There is no real-time monitoring.
RedisProxyListDriver
- More complex driver that uses Redis list(s) to deliver/process messages.
- This driver can't use delayed execution.
- If message reliability is enabled, the messages are re-queued when the dispatcher crashes or is force-stopped. Otherwise, messages can be lost in these cases.
- If message reliability is enabled, on POSIX-enabled systems (loaded
pcntlandposixextensions), the message is monitored by a background heartbeat process (notifying that work is being done on the message approximately every 1 second). If the system is not POSIX-enabled, message processing can still be tracked by usingHermesDriverAccessor. Notifications from heartbeat orHermesDriverAccessorreset the message processing timer. Each message gets processing protection by the monitor for a givenkeepAliveTTL; when this protection expires, the message is re-queued. - If priorities are defined, this driver has different behavior among the other
RedisProxydrivers. The messages are processed from highest to lowest priority, but the driver continues to take messages from the current priority level without returning to the high priority first. This default behavior can be changed to match the behavior that other drivers use by callingsetUseTopPriorityFallback(true)on the driver object during driver setup. - If message reliability is enabled, the message can be duplicated in some circumstances. The user has to implement custom message completion tracking if the message has to be processed only once.
RedisProxyStreamDriver
- More complex driver that uses Redis stream(s) to deliver/process messages.
- This driver can use delayed execution (message is executed after the
executeAttimestamp at any moment). This driver preserves the message priority through the delayed queue; a message moved to the processing queue retains its original priority. - The driver registers a consumer inside Redis streams. Due to this, the driver is always monitored.
Monitoring is almost identical to the monitoring implemented in
RedisProxyListDriver(when reliability is turned on). Monitoring here monitors not only messages but the consumers too, clearing outdated consumers from Redis memory. A message that is held in the stream group pending list for more thankeepAliveTTLis claimed by the next consumer (keeps priority order). Message claiming by another consumer afterkeepAliveTTLprotection expires can be restricted to a number of reclaims (defaults to 3); this can be set to 0 to throw the message away. Claimed message is processed immediately, not re-queued for later processing. - A message can be duplicated in some circumstances. The user has to implement custom message completion tracking if the message has to be processed only once.
HermesDriverAccessor
- Singleton object that carries the message/stream message envelope and priority of the message.
- If a handler is executed by a dispatcher with RedisProxyListDriver (in reliability mode only) or RedisProxyStreamDriver (always), user code can call:
signalProcessingUpdate()- manual monitor notification (resets protection expiration tokeepAliveTTL),setProcessingStatus($status, $percent)- manual monitor notification with status or percentage of completion of the task set to the monitor, both values are optional.
- User code cannot call setters or clear the data of the
HermesDriverAccessor.
efabrica/hermes-extension 适用场景与选型建议
efabrica/hermes-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73.89k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「driver」 「worker」 「heartbeat」 「redis-proxy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 efabrica/hermes-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 efabrica/hermes-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 efabrica/hermes-extension 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Queue
SlimQ Enables You to push jobs to background workers
Monitor Laravel queue and schedule status
A CakePHP plugin providing an application heartbeat status page with configurable sensors
Galera cluster driver for Doctrine
A lightweight task queue on top of rybakit/phive-queue
统计信息
- 总下载量: 73.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-05