Example #1
0
 /**
  * Simulates messages on the producer side.
  *
  * @param string $keyPrefix Prefix for key generation.
  * @param int $keyDistribution Number of different keys to generate.
  * @param int $numMessages Number of messages to send
  * @param int $expMinMs Start value for random expiration in microseconds.
  * @param int $expMaxMs End value for random expiration in microseconds.
  * @param int $sleepMicroSeconds Sleep duration between each send call in microseconds.
  */
 public function simulate($keyPrefix, $keyDistribution, $numMessages, $expMinMs, $expMaxMs, $sleepMicroSeconds)
 {
     $this->logger->info("Starting console producer simulation. Generating {$numMessages} messages with a key " . "distribution of {$keyDistribution}. The key prefix is {$keyPrefix} and the expiration " . "timeout is between {$expMinMs} and {$expMaxMs} microseconds.");
     $client = new Client($this->getZmqOut(), $this->logger);
     $client->setVerbose($this->isVerbose());
     $client->connect();
     // Sleep until socket is ready
     // @todo Figure out a better way to check if the socket is ready.
     usleep(1000000);
     $this->logger->info("Starting to generate packets...");
     for ($i = 0; $i < $numMessages; $i++) {
         $key = $keyPrefix . '_' . mt_rand(1, $keyDistribution);
         $expire = mt_rand($expMinMs, $expMaxMs);
         $data = serialize(['a' => mt_rand(1, 10), 'b' => mt_rand(1, 10)]);
         $message = new RequestMessage($key, $data, $expire, 'aggregate');
         $client->send((string) $message);
         if ($sleepMicroSeconds) {
             usleep($sleepMicroSeconds);
         }
     }
 }
Example #2
0
 /**
  * Registers the gearman queues where the worker has to listen on.
  * The given callback has to transform the workload into a RequestMessage object.
  *
  * @param string $callbackName
  * @param callable $workloadToMessageCallback The return value of
  */
 public function listenOn($callbackName, callable $workloadToMessageCallback)
 {
     $this->addCallback($callbackName, function ($job) use($workloadToMessageCallback) {
         $message = call_user_func($workloadToMessageCallback, $job->workload());
         if ($message instanceof RequestMessageInterface) {
             if ($this->verbose) {
                 $this->logger->debug("Sending message to queue: " . (string) $message);
             }
             $this->metrics->increment('message');
             $this->client->message($message);
         } else {
             throw new \Exception("Invalid return type.");
         }
     });
 }