t3ran13 / golos-php-event-listener
STEEM/GOLOS 区块链的 PHP 事件监听器
v1.0.0
2018-12-21 16:30 UTC
Requires
- php: ^7.0
- predis/predis: 1.1.1
- t3ran13/php-graphene-node-client: ^5.0.0
- t3ran13/php-process-manager: ^0.1.2
This package is auto-updated.
Last update: 2024-09-23 07:31:50 UTC
README
STEEM/GOLOS 区块链的 PHP 事件监听器
通过 Composer 安装
composer require t3ran13/golos-php-event-listener
基本用法
为了运行您的应用程序,您需要
- 数据库管理器
- 事件处理器
- 用于 cron 的启动脚本
以下示例
<?php namespace MyApp; use GolosPhpEventListener\app\process\BlockchainExplorerProcess; use GolosPhpEventListener\app\process\EventsHandlersProcess; use GrapheneNodeClient\Connectors\ConnectorInterface; use ProcessManager\db\RedisManager; use ProcessManager\ProcessManager; ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); define('PATH', __DIR__); require __DIR__ . "/Autoloader.php"; // only in GrapheneNodeClient project require __DIR__ . '/vendor/autoload.php'; echo PHP_EOL . '------ start GOLOS EVENT LISTENER ------' . PHP_EOL; $db = new RedisManager(); //Main process witch starts all other peocesses $pm = (new ProcessManager($db)) ->setProcessName('MainProcess') ->setMaxRunningProcesses(3); //it is 4 total with MainProcess, MAX 512 MB RAM by default if ($pm->hasState()) { $pm->loadState(); } else { $pm->setPriority(25) ->setExecutionStep(1) ->setMaxNTriesOfRun(0) ->setSecondsBetweenRuns(55) ->setMaxLifetimeWithoutResults(20) ->saveState(); } // creating event handler $eh1 = (new PostIsCreatedEventHandler($db)) ->setProcessName('GEV:votesOffafnur:1') ->generateIdFromProcessName() ->addCondition('op:1:voter','golosboard') //event trigger 1 ->addCondition('op:0','vote'); //event trigger 2 if ($eh1->hasState()) { $eh1->loadState(); } else { $eh1->setPriority(35) ->setExecutionStep(1) ->setMaxNTriesOfRun(0) ->setSecondsBetweenRuns(10) ->setMaxLifetimeWithoutResults(15) ->saveState(); } // creating event handler $eh2 = (new PostIsCreatedEventHandler($db)) ->setProcessName('GEV:allComments:2') ->generateIdFromProcessName() ->addCondition('op:0','comment'); //event trigger 1 if ($eh2->hasState()) { $eh2->loadState(); } else { $eh2->setPriority(35) ->setExecutionStep(1) ->setMaxNTriesOfRun(0) ->setSecondsBetweenRuns(10) ->setMaxLifetimeWithoutResults(15) ->saveState(); } // creating event handler $eh3 = (new PostIsCreatedEventHandler($db)) ->setProcessName('GEV:allVotes:3') ->generateIdFromProcessName() ->addCondition('op:0','vote'); //event trigger 1 if ($eh3->hasState()) { $eh3->loadState(); } else { $eh3->setPriority(35) ->setExecutionStep(1) ->setMaxNTriesOfRun(0) ->setSecondsBetweenRuns(10) ->setMaxLifetimeWithoutResults(15) ->saveState(); } // Creating blockchain lestener $BEP = (new BlockchainExplorerProcess($db,ConnectorInterface::PLATFORM_GOLOS)) ->setProcessName('GEV:BlockchainExplorer') ->setLastBlock(16146488) ->addEvent($eh1) //do not forget add eventhandlers to explorer process ->addEvent($eh2) //adding event handler for detecting events ->addEvent($eh3); if ($BEP->hasState()) { $BEP->loadState(); } else { $BEP->setPriority(30) ->setExecutionStep(1) ->setMaxNTriesOfRun(0) ->setSecondsBetweenRuns(30) ->setMaxLifetimeWithoutResults(20) ->saveState(); } $pm->addProcess($BEP) ->addProcess($eh1)//add event listener for handling events ->addProcess($eh2) ->addProcess($eh3); $pm->start();
将此脚本添加到 cron。
数据库管理器
使用来自 t3ran13/php-process-manager composer 包的 RedisManager 库即可使用,以下为数据库结构
- DB0
- PM:GEV:{ProcessName}:{id}:className
- PM:GEV:{ProcessName}:{id}:processName
- PM:GEV:{ProcessName}:{id}:priority
- PM:GEV:{ProcessName}:{id}:pid
- PM:GEV:{ProcessName}:{id}:executionStep
- PM:GEV:{ProcessName}:{id}:isRunning
- PM:GEV:{ProcessName}:{id}:nTriesOfRun
- PM:GEV:{ProcessName}:{id}:maxNTriesOfRun
- PM:GEV:{ProcessName}:{id}:secondsBetweenRuns
- PM:GEV:{ProcessName}:{id}:maxLifetimeWithoutResults
- PM:GEV:{ProcessName}:{id}:lastUpdateDatetime
- PM:GEV:{ProcessName}:{id}:data:*
- PM:GEV:{ProcessName}:{id}:data:events:*
- PM:GEV:{ProcessName}:{id}:errors:*
如何创建自己的数据库管理器,请参阅 t3ran13/php-process-manager
事件处理器
每个处理器必须实现 EventHandlerInterface 和 ProcessManager\process\ProcessInterface,您可以创建自己的或从 EventHandlerAbstract 继承。当发现任何事件时,BlockchainExplorer 会使用 isTrxSatisfiesConditions 函数进行检查,如果满足条件则将其保存到处理列表中。当 EventHandlerProcess 启动时,它会处理队列中的所有事件。
<?php namespace MyApp; use GolosPhpEventListener\app\process\handlers\EventHandlerAbstract; class VoteHandler extends EventHandlerAbstract { public function start() { $events = $this->getEvents(); //TODO FIXME echo PHP_EOL . date('Y-m-d H:i:s') . $this->getProcessName() . ' is running and have total events=' . count($events); foreach ($events as $key => $event) { // some code $this->setLastUpdateDatetime(date('Y-m-d H:i:s')) ->removeEventByKey($key) ->saveState(); } } /** * ask process to start * * @return bool */ public function isStartNeeded(): bool { return parent::isStartNeeded() && count($this->getEvents()) > 0; } }
示例
您可以在以下链接中查看基于 golos-php-event-listener 的应用程序示例 https://github.com/t3ran13/golos-rating-auto-reward