Golos/Steem PHP Event Listener [v1.0.0]

in #utopian-io5 years ago (edited)

golos-php-event-listener

PHP event listener for STEEM/GOLOS/VIZ/WHALESHARES blockchains event. It is scan blockchains for needed you transactions and handle it.

It is easy way catch some transactions and handle it.

Github or packagist with MIT license. Author @t3ran13

In Release v1.0.0

  • Process control rebuild to t3ran13/php-process-manager
  • DBManager was replaced with manager from t3ran13/php-process-manager
  • New structure for BlockchainExplorerProcess
  • New structure for EventHandler without EventListenerProcess

Process control rebuild to t3ran13/php-process-manager

It had own logic for working with processes. But it was not so convenient for development and using in projects. According this reasons lib uses outer process manager now - t3ran13/php-process-manager. It give new abstract layer and it is easy for understanding.

DBManager was replaced with manager from t3ran13/php-process-manager

DBManager is used for saving states of processes. t3ran13/php-process-manager has own DBManager witch is enough for our lib. Own DBManager was replaced with outer.

See t3ran13/php-process-manager to modify DBManager for your app, it is has method for saving some special data of events/process.

New stucture for Redis is

- 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:*
    

New structure for BlockchainExplorerProcess

Thanks to using outer proces manager it has more less code and easy to modify logic if you need. Also it has new method addEvent for events handlers.

See below the example of usage

<?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_STEEM))
    ->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();

Add this script to cron.

New structure for EventHandler without EventListenerProcess

The EventHandler is separate process now without central process (EventListenerProcess). It is easy to use and understanding.

Each event handler have to implements EventHandlerInterface and ProcessInterface, and each event have to be added to BlockchainExplorerProcess for detecting events and init as process for handling this events.

<?php
//....

$db = new RedisManager();

//Main process witch starts all other peocesses
$pm = (new ProcessManager($db));
//....

// 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 blockchain lestener
$BEP = (new BlockchainExplorerProcess($db,ConnectorInterface::PLATFORM_STEEM))
    ->addEvent($eh1); //adding event handler for detecting events
//...


$pm->addProcess($BEP)
    ->addProcess($eh1); //add event handler for handling events
$pm->start();


It is better with each commit

Commits were done by me for last 14 days

https://github.com/t3ran13/golos-php-event-listener/compare/v0.0.1rc3...v1.0.0

  • fix getting of last block in block explorer process
  • upd php-graphene-node-client version
  • upd otrder login in BlockchainExplorerProcess (last 14 day)
  • install composer t3ran13/php-process-manager (last 14 day)
  • upd RedisManager are replaced by manager from t3ran13/php-process-manager (last 14 day)
  • upd new EventHandlerAbstract (last 14 day)
  • upd new BlockchainExplorerProcess (last 14 day)
  • upd README (last 14 day)
  • upd fix checking of conditions in event handler (last 14 day)
  • upd hide logs output (last 14 day)
  • upd fix t3ran13/php-process-manager version (last 14 day)
  • upd sork events by adding order (last 14 day)
Sort:  
  • Very informative, yet a bit of a dry read. Try including context and purpose.
  • Could use some images to spice up the look.
  • I've used this comparison to see the modification of the last 14 days: https://github.com/t3ran13/golos-php-event-listener/compare/master@%7B14day%7D...master
  • This is the case when less is more, but I think more should have been omitted for this sentence. Also process takes two Ss. Spellchecker in chrome is free.

Thanks to using outer proces manager it has more less code and easy to modify logic if you need.

  • Do you have a list of projects that make use of this library?

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

thx for feedback and advices

Do you have a list of projects that make use of this library?

If you ask about process manager.
No, it was made by me recently. It is good for OOP - other abstraction layer. Process control is not main goal of this event listener and code of process manager diverts attention from business logic of lib. It is more clearly for understanding when you do not see side code.

if you ask about event listener - it is my rating.mysteemit.xyz site

Thank you for your review, @helo! Keep up the good work!

Hi @t3ran13!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Calling @originalworks :)
img credz: pixabay.com
Nice, you got an awesome upgoat, thanks to @t3ran13
BuildTeam wishes everyone a great Christmas and bullish Holidays
Want a boost? Minnowbooster's got your back!

Hey, @t3ran13!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!