efabrica/hermes-extension 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

efabrica/hermes-extension

Composer 安装命令:

composer require efabrica/hermes-extension

包简介

Extension for tomaj/hermes

README 文档

README

PHPStan level PHP static analysis Latest Stable Version Total Downloads

Extension for tomaj/hermes. It contains:

  • HermesWorker (symfony command)
  • Drivers (RedisProxy based):
    • RedisProxySetDriver
    • RedisProxySortedSetDriver
    • RedisProxyListDriver
    • RedisProxyStreamDriver
  • Other drivers:
    • DummyDriver (for testing purposes)
  • Heartbeat functionality with drivers
    • RedisProxyStorage
    • MemoryStorage (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, SIGQUIT and SIGHUP)
  • 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 HermesDriverAccessor singleton 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 Dispatcher is 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 executeAt timestamp 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 Dispatcher is 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 pcntl and posix extensions), 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 using HermesDriverAccessor. Notifications from heartbeat or HermesDriverAccessor reset the message processing timer. Each message gets processing protection by the monitor for a given keepAliveTTL; when this protection expires, the message is re-queued.
  • If priorities are defined, this driver has different behavior among the other RedisProxy drivers. 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 calling setUseTopPriorityFallback(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 executeAt timestamp 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 than keepAliveTTL is claimed by the next consumer (keeps priority order). Message claiming by another consumer after keepAliveTTL protection 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 to keepAliveTTL),
    • 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 efabrica/hermes-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 73.89k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-05