public function testListensForEvents()
 {
     $listener = new EventListener();
     $es = fopen('php://memory', 'w+');
     $is = fopen('php://memory', 'w+');
     $os = fopen('php://memory', 'w+');
     $listener->setErrorStream($es)->setOutputStream($os)->setInputStream($is);
     // Make sure blank lines are ignored
     fwrite($is, "\n");
     fwrite($is, "ver:3.0 server:supervisor serial:313 pool:event_listener poolserial:313 eventname:TICK_5 len:15\n");
     fwrite($is, "when:1313021985");
     fwrite($is, "ver:3.0 server:supervisor serial:313 pool:event_listener poolserial:313 eventname:TICK_60 len:15\n");
     fwrite($is, "when:1313021990");
     fwrite($is, "ver:3.0 server:supervisor serial:313 pool:event_listener poolserial:313 eventname:TICK_3600 len:15\n");
     fwrite($is, "when:1313021995");
     rewind($is);
     $total = 0;
     $listener->listen(function (EventListener $listener, EventNotification $event) use(&$total) {
         $listener->log((string) $event);
         if (++$total == 1) {
             return true;
         } else {
             if ($total == 2) {
                 return false;
             } else {
                 return 'quit';
             }
         }
     });
     rewind($es);
     rewind($os);
     // Ensure that the correct output was given
     $this->assertEquals("READY\nRESULT 2\nOKREADY\nRESULT 4\nFAILREADY\n", stream_get_contents($os));
     // Ensure that the messages were logged properly
     $this->assertEquals(6, count(explode("\n", trim(stream_get_contents($es)))));
     // Make sure the entire stream was read
     $this->assertEquals('', stream_get_contents($is));
 }
Ejemplo n.º 2
0
<?php

/**
 * Here is a simple example of creating a supervisor event listener
 */
require_once __DIR__ . '/../vendor/autoload.php';
use Mtdowling\Supervisor\EventListener;
use Mtdowling\Supervisor\EventNotification;
$listener = new EventListener();
$listener->listen(function (EventListener $listener, EventNotification $event) {
    // Log information about the event
    $listener->log($event->getEventName());
    $listener->log($event->getServer());
    $listener->log($event->getPool());
    // Try messing around with supervisorctl to restart processes and see what
    // data is available
    $listener->log(var_export($event->getData(), true));
    return true;
});
Ejemplo n.º 3
0
 * command=php /path/to/examples/processes.php /tmp/processes.txt
 * process_name=%(program_name)s_%(process_num)02d
 * events=PROCESS_STATE_STARTING,PROCESS_STATE_STOPPING,PROCESS_STATE_STOPPED,PROCESS_STATE_FATAL,PROCESS_STATE_UNKNOWN,PROCESS_STATE_EXITED,PROCESS_STATE_BACKOFF,PROCESS_STATE_RUNNING
 * numprocs=1
 * autostart=true
 * autorestart=unexpected
 */
require_once __DIR__ . '/../vendor/autoload.php';
use Mtdowling\Supervisor\EventListener;
use Mtdowling\Supervisor\EventNotification;
// Requires a file name as an argument
if (!isset($argv[1])) {
    echo 'This script requires that a file name be passed as a single parameter';
    exit(1);
}
$filename = $argv[1];
$f = fopen($filename, 'a');
if (!$f) {
    echo 'Unable to open ' . $filename . ' for writing';
    exit(1);
}
$listener = new EventListener();
$listener->listen(function (EventListener $listener, EventNotification $event) use($f) {
    $state = $event->getEventName();
    if (strpos($state, 'PROCESS_STATE_') !== false) {
        $name = $event->getData('groupname') ? $event->getData('groupname') : $event->getData('processname');
        fwrite($f, "{$name},{$state}\n");
    }
    return true;
});
fclose($f);