예제 #1
0
 /**
  * @param int|string $minimumLogLevel
  * @return Config
  * @throws \Exception
  */
 public function setMinimumLogLevel($minimumLogLevel)
 {
     if (is_int($minimumLogLevel)) {
         $this->minimumLogLevel = $minimumLogLevel;
     } else {
         $this->minimumLogLevel = ConsoleLog::parseLogLevel($minimumLogLevel);
     }
     return $this;
 }
예제 #2
0
<?php

$opts = getopt("", array('zmq:', 'verbose', 'logLevel:'));
$zmq = isset($opts['zmq']) ? (string) $opts['zmq'] : false;
$verbose = isset($opts['verbose']) ? true : false;
$logLevel = isset($opts['logLevel']) ? (string) $opts['logLevel'] : false;
if ($verbose) {
    $logLevel = 'debug';
}
require_once DIRNAME(__FILE__) . '/../vendor/autoload.php';
if (!$zmq) {
    $zmq = 'tcp://127.0.0.1:5444';
}
Awdn\VigilantQueue\Consumer\ConsoleConsumer::factory($zmq, \Awdn\VigilantQueue\Utility\ConsoleLog::loggerFactory('ConsoleConsumer', $logLevel), $verbose)->consume();
if (pcntl_fork() == 0) {
    // Setup the gearman client and feed some data into the queue
    $gearmanClient = new GearmanClient();
    $gearmanClient->addServer($gIp, (int) $gPort);
    // Prepare Gearman jobs
    for ($i = 0; $i < $numMessages; $i++) {
        $workload = new stdClass();
        $workload->key = $keyPrefix . mt_rand(1, $keyDistribution);
        $workload->data = mt_rand(1000, 1100);
        $workload->type = 'count';
        // Create asynchronous background job.
        $gearmanClient->doBackground($gearmanQueueName, json_encode($workload));
        if ($sleepMicroSeconds) {
            usleep($sleepMicroSeconds);
        }
    }
    if ($verbose) {
        \Awdn\VigilantQueue\Utility\ConsoleLog::log("Finished creating {$numMessages} gearman jobs.\n");
    }
} else {
    // Setup the producer, which connects to the gearman queue and fetches the jobs, prepares them for
    // the deferred priority queue
    $producer = new \Awdn\VigilantQueue\Producer\GearmanProducer($zmq, \Awdn\VigilantQueue\Utility\ConsoleLog::loggerFactory('GearmanProducer', $logLevel), $verbose);
    $producer->addServer($gIp, (int) $gPort);
    // Callback method to transform a Gearman job workload into a DeferredQueueServer RequestMessage
    $producer->listenOn($gearmanQueueName, function ($workload) use($expMinMs, $expMaxMs) {
        $data = json_decode($workload);
        return new \Awdn\VigilantQueue\Server\RequestMessage($data->key, $data->data, mt_rand($expMinMs, $expMaxMs), $data->type);
    });
    $producer->produce();
}
예제 #4
0
<?php

$parameterList = array('zmqOut:' => '(string) Outbound ZMQ address. Default is tcp://127.0.0.1:5444. ', 'zmqIn:' => '(string) Outbound ZMQ address. Default is tcp://127.0.0.1:4444.', 'logLevel:' => 'Sets the minimum debug level. This is one of debug, info, warn, error.', 'evictionTickrate:' => '(int) Determines how often per second the eviction event is fired. Defaults to 1000.', 'dataModeAppend:' => '(string) Sets the data mode to append data for already existing entries within the queue instead of replacing them.');
$opts = getopt("", array_keys($parameterList));
if (empty($opts)) {
    echo "Usage: \n";
    foreach ($parameterList as $param => $description) {
        $_param = rtrim($param, ':');
        echo "  --{$_param}\n    {$description}\n";
    }
    exit(0);
}
require_once DIRNAME(__FILE__) . '/../vendor/autoload.php';
$config = new \Awdn\VigilantQueue\Server\Config();
$config->setMinimumLogLevel(isset($opts['logLevel']) ? (string) $opts['logLevel'] : 'error')->setEvictionTicksPerSec(isset($opts['evictionTickrate']) ? (int) $opts['evictionTickrate'] : 1000)->setZmqIn(isset($opts['zmqIn']) ? (int) $opts['zmqIn'] : 'tcp://127.0.0.1:4444')->setZmqOut(isset($opts['zmqOut']) ? (string) $opts['zmqOut'] : 'tcp://127.0.0.1:5444');
$server = Awdn\VigilantQueue\Server\DeferredQueue::factory($config, \Awdn\VigilantQueue\Utility\ConsoleLog::loggerFactory('DeferredQueue', $config->getMinimumLogLevel()), new \Awdn\VigilantQueue\Utility\NullMetricsHandler());
// For the following request message types, we change the behaviour of the queue to append the data to existing keys
// instead of replacing them.
$dataModeAppend = isset($opts['dataModeAppend']) ? (array) $opts['dataModeAppend'] : array();
foreach ($dataModeAppend as $messageType) {
    $server->setDataModeByType($messageType, \Awdn\VigilantQueue\Queue\PriorityHashQueue::DATA_MODE_APPEND);
}
$server->run();
예제 #5
0
<?php

$opts = getopt("", array('zmq:', 'verbose', 'logLevel:', 'simulate:', 'keyPrefix:', 'keyDistribution:', 'numMessages:', 'expMinMs:', 'expMaxMs:', 'sleepUs:'));
if (empty($opts)) {
    echo "Example: php console-producer.php --verbose --simulate 1 --keyPrefix mk --keyDistribution 100000 --numMessages 100000 --expMinMs 3000000 --expMaxMs 3000000 --sleepUs 1\n";
    exit(0);
}
$zmq = isset($opts['zmq']) ? (string) $opts['zmq'] : 'tcp://127.0.0.1:4444';
$stdIn = isset($opts['stdin']) ? true : false;
$verbose = isset($opts['verbose']) ? true : false;
$logLevel = isset($opts['logLevel']) ? (string) $opts['logLevel'] : 'error';
$simulate = isset($opts['simulate']) ? true : false;
$keyPrefix = isset($opts['keyPrefix']) ? (string) $opts['keyPrefix'] : false;
$keyDistribution = isset($opts['keyDistribution']) ? (int) $opts['keyDistribution'] : false;
$numMessages = isset($opts['numMessages']) ? (int) $opts['numMessages'] : false;
$expMinMs = isset($opts['expMinMs']) ? (int) $opts['expMinMs'] : false;
$expMaxMs = isset($opts['expMaxMs']) ? (int) $opts['expMaxMs'] : false;
$sleepMicroSeconds = isset($opts['sleepUs']) ? (int) $opts['sleepUs'] : false;
require_once DIRNAME(__FILE__) . '/../vendor/autoload.php';
if ($verbose) {
    $logLevel = 'debug';
}
$producer = Awdn\VigilantQueue\Producer\ConsoleProducer::factory($zmq, \Awdn\VigilantQueue\Utility\ConsoleLog::loggerFactory('ConsoleProducer', $logLevel), $verbose);
if ($simulate) {
    // Generate a bunch of messages and send them to the inbound queue of the deferred server.
    $producer->simulate($keyPrefix, $keyDistribution, $numMessages, $expMinMs, $expMaxMs, $sleepMicroSeconds);
} else {
    // Get messages from readline and write to inbound queue of the server.
    $producer->produce();
}