Example #1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('<info>Lykov bot started</info>');
     $loop = \React\EventLoop\Factory::create();
     $emitter = new \Evenement\EventEmitter();
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($loop);
     $pcntl->on(SIGTERM, function () use($emitter) {
         $emitter->emit('terminate');
     });
     $pcntl->on(SIGTERM, function () use($emitter) {
         $emitter->emit('terminate');
     });
     $emitter->on('error.fatal', function ($msg) use($loop, $output) {
         $loop->stop();
         $output->writeln('<fg="red">Fatal error occured: ' . $msg . '</fg>');
     });
     $emitter->on('terminate', function () use($loop, $emitter) {
         $emitter->emit('log', ['Terminating...', OutputInterface::VERBOSITY_NORMAL]);
         $loop->stop();
         die;
     });
     $emitter->on('log', function ($msg, $verbosity = OutputInterface::VERBOSITY_DEBUG) use($output) {
         if ($verbosity === OutputInterface::VERBOSITY_DEBUG) {
             $msg = 'DEBUG: ' . $msg;
         }
         if ($output->getVerbosity() >= $verbosity) {
             $output->writeln($msg);
         }
     });
     $apiClient = new SocketClient($loop, $emitter, self::TOKEN_LYKOV_TEST);
     $emitter->emit('send', ['id' => 2, 'type' => 'message', 'channel' => 'D04F44TFL', 'text' => 'hello']);
     $loop->run();
 }
Example #2
0
 /**
  * References the ReactPHP eventloop for later use
  *
  * @param CakeEvent $event
  */
 public function construct(CakeEvent $event)
 {
     if (!function_exists('pcntl_signal')) {
         $event->subject()->getShell()->out('<warning>Your configuration doesn\'t seem to support \'ext-pcntl\'. It is highly recomended that you install and configure it as it provides OS signaling support!</warning>');
     } elseif (!class_exists('MKraemer\\ReactPCNTL\\PCNTL')) {
         $event->subject()->getShell()->out('<warning>PCNTL support found but you didn\'t install mkraemer/react-pcntl!</warning>');
     } else {
         $pcntl = new MKraemer\ReactPCNTL\PCNTL($event->data['loop']);
         $pcntl->on(SIGTERM, function () use($event) {
             $event->data['loop']->stop();
         });
         $pcntl->on(SIGINT, function () use($event) {
             $event->data['loop']->stop();
         });
     }
 }
Example #3
0
 /**
  * @param \Peanut\Console\Application $app
  * @param array                       $config
  */
 public function exec(\Peanut\Console\Application $app, array $config)
 {
     $this->process('sudo -v', ['print' => false]);
     $mode = $app->getOption('attach') ? 'attach' : 'detach';
     $ispull = $app->getOption('pull') ? true : false;
     if ('attach' == $mode) {
         if (false === function_exists('pcntl_fork')) {
             $mode = 'detach';
             echo \Peanut\Console\Color::text('attach mode is not support, start detach mode', 'red', '') . PHP_EOL;
         }
         $this->loop = \React\EventLoop\Factory::create();
         try {
             $this->run();
             $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
             $pcntl->on(SIGTERM, function () {
                 // kill
                 echo 'SIGTERM' . PHP_EOL;
                 exit;
             });
             $pcntl->on(SIGHUP, function () {
                 // logout
                 echo 'SIGHUP' . PHP_EOL;
                 exit;
             });
             $pcntl->on(SIGINT, function () {
                 // ctrl+c
                 echo 'Terminated by console' . PHP_EOL;
                 posix_kill(posix_getpid(), SIGUSR1);
                 echo $this->process('docker rm -f $(docker ps -q)');
                 exit;
             });
             echo 'Started as PID ' . getmypid() . PHP_EOL;
             $this->loop->run();
         } catch (\Exception $e) {
             throw new \Peanut\Console\Exception($e);
         }
     } else {
         $this->run($mode, $ispull);
         echo PHP_EOL;
         $this->dockerLs();
     }
 }
Example #4
0
 /**
  * Starts the main loop. Blocks.
  */
 public function run()
 {
     Debug::enable();
     gc_disable();
     //necessary, since connections will be dropped without reasons after several hundred connections.
     $this->loop = \React\EventLoop\Factory::create();
     $this->controller = new \React\Socket\Server($this->loop);
     $this->controller->on('connection', array($this, 'onSlaveConnection'));
     $this->controller->listen(5500);
     $this->web = new \React\Socket\Server($this->loop);
     $this->web->on('connection', array($this, 'onWeb'));
     $this->web->listen($this->port, $this->host);
     $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     if ($this->isDebug()) {
         $this->loop->addPeriodicTimer(0.5, function () {
             $this->checkChangedFiles();
         });
     }
     $this->isRunning = true;
     $loopClass = (new \ReflectionClass($this->loop))->getShortName();
     $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>");
     for ($i = 0; $i < $this->slaveCount; $i++) {
         $this->newInstance(5501 + $i);
     }
     $this->loop->run();
 }
Example #5
0
use React\Promise\Deferred;
use React\EventLoop\Timer\Timer;
require "vendor/autoload.php";
$loop = React\EventLoop\Factory::create();
if (0) {
    $dnsResolverFactory = new React\Dns\Resolver\Factory();
    $dnsResolver = $dnsResolverFactory->createCached('127.0.0.1', $loop);
    $factory = new React\HttpClient\Factory();
    $client = $factory->create($loop, $dnsResolver);
}
$loop->addPeriodicTimer(0.25, function () {
    static $count = 0;
    $count++;
    echo $count, PHP_EOL;
    if ($count == 10) {
        posix_kill(posix_getpid(), SIGTERM);
    }
});
$pcntl = new MKraemer\ReactPCNTL\PCNTL($loop);
$pcntl->on(SIGTERM, function () {
    // Clear some queue
    // Write syslog
    // Do ALL the stuff
    echo 'Bye' . PHP_EOL;
    die;
});
$pcntl->on(SIGINT, function () {
    echo 'Terminated by console' . PHP_EOL;
    die;
});
$loop->run();
Example #6
0
 /**
  * Starts the main loop. Blocks.
  */
 public function run()
 {
     Debug::enable();
     //make whatever is necessary to disable all stuff that could buffer output
     ini_set('zlib.output_compression', 0);
     ini_set('output_buffering', 0);
     ini_set('implicit_flush', 1);
     ob_implicit_flush(1);
     $this->loop = \React\EventLoop\Factory::create();
     $this->controller = new React\Server($this->loop);
     $this->controller->on('connection', array($this, 'onSlaveConnection'));
     $this->controllerHost = $this->getNewControllerHost();
     $this->controller->listen(5500, $this->controllerHost);
     $this->web = new \React\Socket\Server($this->loop);
     $this->web->on('connection', array($this, 'onWeb'));
     $this->web->listen($this->port, $this->host);
     $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     $pcntl->on(SIGCHLD, [$this, 'handleSigchld']);
     $pcntl->on(SIGUSR1, [$this, 'restartWorker']);
     if ($this->isDebug()) {
         $this->loop->addPeriodicTimer(0.5, function () {
             $this->checkChangedFiles();
         });
     }
     $this->isRunning = true;
     $loopClass = (new \ReflectionClass($this->loop))->getShortName();
     $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>");
     for ($i = 0; $i < $this->slaveCount; $i++) {
         $this->newInstance(5501 + $i);
     }
     $this->loop->run();
 }
Example #7
0
<?php

require_once dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php';
$loop = \React\EventLoop\Factory::create();
$loop->addReadStream(STDIN, function ($stream) use($loop) {
    $data = fgets($stream);
    fwrite(STDOUT, $data);
    fwrite(STDERR, $data);
});
$loop->addPeriodicTimer(1000000, function () {
    pcntl_signal_dispatch();
});
$pcntl = new \MKraemer\ReactPCNTL\PCNTL($loop);
$pcntl->on(SIGTERM, function () {
    exit(0);
});
$loop->run();
Example #8
0
/**
 * @param $loop
 */
function registerSignalHandlers($loop)
{
    $pcntl = new MKraemer\ReactPCNTL\PCNTL($loop);
    $pcntl->on(SIGTERM, function () {
        echo 'Bye' . PHP_EOL;
        die;
    });
    $pcntl->on(SIGINT, function () {
        echo 'Terminated by console' . PHP_EOL;
        die;
    });
}
        $time = microtime(true);
    }
};
$restart = function () use($loop, $stop, $start, &$time) {
    $now = microtime(true);
    if ($now <= $time) {
        return;
    }
    $stop();
    $start();
    $time = microtime(true) + DELAY;
};
$exit = function () use($loop, $stop) {
    $stop();
    $loop->stop();
    exit;
};
// Begin monitoring:
$inotify = new Inotify($loop);
$inotify->add(__DIR__, IN_CLOSE_WRITE | IN_CREATE | IN_DELETE);
$inotify->add(__DIR__ . '/app', IN_CLOSE_WRITE | IN_CREATE | IN_DELETE);
$inotify->add(__DIR__ . '/src', IN_CLOSE_WRITE | IN_CREATE | IN_DELETE);
$inotify->on(IN_CLOSE_WRITE, $restart);
$inotify->on(IN_CREATE, $restart);
$inotify->on(IN_DELETE, $restart);
// Wait for exit signal:
$pcntl = new MKraemer\ReactPCNTL\PCNTL($loop);
$pcntl->on(SIGTERM, $exit);
$pcntl->on(SIGINT, $exit);
$start();
$loop->run();
Example #10
0
 /**
  * Connects to ProcessManager, master process.
  */
 public function run()
 {
     $this->loop = \React\EventLoop\Factory::create();
     $this->errorLogger = BufferingLogger::create();
     ErrorHandler::register(new ErrorHandler($this->errorLogger));
     $client = stream_socket_client($this->config['controllerHost']);
     $this->controller = new \React\Socket\Connection($client, $this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     register_shutdown_function([$this, 'shutdown']);
     $this->bindProcessMessage($this->controller);
     $this->controller->on('close', \Closure::bind(function () {
         $this->shutdown();
     }, $this));
     $this->server = new React\Server($this->loop);
     //our version for now, because of unix socket support
     $http = new HttpServer($this->server);
     $http->on('request', array($this, 'onRequest'));
     //port is only used for tcp connection. If unix socket, 'host' contains the socket path
     $port = $this->config['port'];
     $host = $this->config['host'];
     while (true) {
         try {
             $this->server->listen($port, $host);
             break;
         } catch (\React\Socket\ConnectionException $e) {
             usleep(500);
         }
     }
     $this->sendMessage($this->controller, 'register', ['pid' => getmypid(), 'port' => $port]);
     $this->loop->run();
 }
Example #11
0
 /**
  * Connects to ProcessManager, master process.
  */
 public function run()
 {
     $this->loop = \React\EventLoop\Factory::create();
     ErrorHandler::register(new ErrorHandler(new BufferingLogger()));
     $this->client = stream_socket_client('tcp://127.0.0.1:5500');
     $this->connection = new \React\Socket\Connection($this->client, $this->loop);
     $this->connection->on('error', function ($data) {
         var_dump($data);
     });
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     register_shutdown_function([$this, 'shutdown']);
     $this->bindProcessMessage($this->connection);
     $this->connection->on('close', \Closure::bind(function () {
         $this->shutdown();
     }, $this));
     $this->server = new \React\Socket\Server($this->loop);
     $this->server->on('error', function ($data) {
         var_dump($data);
     });
     $http = new HttpServer($this->server);
     $http->on('request', array($this, 'onRequest'));
     $http->on('error', function ($data) {
         var_dump($data);
     });
     $port = $this->config['port'];
     while (true) {
         try {
             $this->server->listen($port);
             break;
         } catch (\React\Socket\ConnectionException $e) {
             usleep(500);
         }
     }
     $this->sendMessage($this->connection, 'register', ['pid' => getmypid(), 'port' => $port]);
     $this->loop->run();
 }