/** * @param string $queue Name of the queue to receive the job * @param string $command FQCN for Command class to be executed by the job * @param array $options Options for the Command class instance */ public function publish($queue, $command, array $options = []) { if (!is_subclass_of($command, CommandInterface::class)) { throw new \RuntimeException('Class does not implement CommandInterface: ' . $command); } $job = json_encode(['command' => $command, 'options' => $options]); $this->client->rpush($queue, $job); }
public function append(EventStream $events) { foreach ($events as $event) { $data = $this->serializer->serialize($event, 'json'); $event = $this->serializer->serialize(['type' => get_class($event), 'created_on' => (new DateTimeImmutable('now', new DateTimeZone('UTC')))->getTimestamp(), 'data' => $data], 'json'); $this->predis->rpush('events:' . $events->aggregateId(), $event); $this->predis->rpush('published_events', $event); } }
/** * @param string $key * @param string $value * @param QueuePriority $priority * @return bool */ public function put($key, $value, QueuePriority $priority = null) { if (!$priority) { $priority = QueuePriority::PRIORITY_NORMAL; } if ($this->checkConnection()) { return $this->predis->rpush(self::QUEUE_PREFIX . ':' . $priority . ':' . $key, $value); } return false; }
/** * Make sure, the Redis keys exist. * * @return void */ private function initializeRedis() { if ($this->initialized) { return; } $this->hash = substr(sha1(serialize($this->items)), 0, 8); $positionKey = $this->getKeyForCurrentPosition(); $existing = !$this->redis->setnx($positionKey, count($this->items) - 1); if (!$existing && count($this->items)) { $listKey = $this->getKeyForItemList(); $this->itemCount = $this->redis->rpush($listKey, $this->items); } $this->initialized = true; }
/** * Start the worker and wait for requests */ public function listen() { $context = new \ZMQContext(); $server = new \ZMQSocket($context, \ZMQ::SOCKET_PULL); $server->bind('tcp://127.0.0.1:' . ($this->defaultPort + $this->client->getId() - 1)); $this->logger->info('Client worker ' . $this->client . ' is ready'); while (true) { $request = $server->recv(); $this->logger->debug('Client worker ' . $this->client . ' receiving request : ' . $request); // Check if the input is valid, ignore if wrong $request = json_decode($request, true); if (!$this->isValidInput($request)) { $this->logger->error('Client worker ' . $this->client . ' received an invalid input'); continue; } try { // Call the right method in the client and push to redis the result $result = call_user_func_array(array($this->client, $request['command']), $request['parameters']); } catch (ClientNotReadyException $e) { $this->logger->warning('Client worker ' . $this->client . ' received a request (#' . $request['invokeId'] . ') whereas the client is not ready. This is normal in case of client reconnection process. Ignoring.'); continue; } $key = $this->key . '.client.commands.' . $request['invokeId']; $this->redis->rpush($key, serialize($result)); $this->redis->expire($key, $this->expire); } }
/** * Creates or modifies keys * * If $key already exists: * * - Strings: its value will be overwritten with $value * - Other types: $value items will be appended to its value * * Examples: * * ``` php * <?php * // Strings: $value must be a scalar * $I->haveInRedis('string', 'Obladi Oblada'); * * // Lists: $value can be a scalar or an array * $I->haveInRedis('list', ['riri', 'fifi', 'loulou']); * * // Sets: $value can be a scalar or an array * $I->haveInRedis('set', ['riri', 'fifi', 'loulou']); * * // ZSets: $value must be an associative array with scores * $I->haveInRedis('set', ['riri' => 1, 'fifi' => 2, 'loulou' => 3]); * * // Hashes: $value must be an associative array * $I->haveInRedis('hash', ['obladi' => 'oblada']); * ``` * * @param string $type The type of the key * @param string $key The key name * @param mixed $value The value * * @throws ModuleException */ public function haveInRedis($type, $key, $value) { switch (strtolower($type)) { case 'string': if (!is_scalar($value)) { throw new ModuleException($this, 'If second argument of haveInRedis() method is "string", ' . 'third argument must be a scalar'); } $this->driver->set($key, $value); break; case 'list': $this->driver->rpush($key, $value); break; case 'set': $this->driver->sadd($key, $value); break; case 'zset': if (!is_array($value)) { throw new ModuleException($this, 'If second argument of haveInRedis() method is "zset", ' . 'third argument must be an (associative) array'); } $this->driver->zadd($key, $value); break; case 'hash': if (!is_array($value)) { throw new ModuleException($this, 'If second argument of haveInRedis() method is "hash", ' . 'third argument must be an array'); } $this->driver->hmset($key, $value); break; default: throw new ModuleException($this, "Unknown type \"{$type}\" for key \"{$key}\". Allowed types are " . '"string", "list", "set", "zset", "hash"'); } }
/** * @inheritdoc */ public function release(array $message, $delay = 0) { if ($delay > 0) { $this->redis->zadd($message['queue'] . ':delayed', [$message['body'] => time() + $delay]); } else { $this->redis->rpush($message['queue'], [$message['body']]); } }
/** * @param Task $task * @return null */ function addTask(Task $task) { $serialized = serialize($task); $existingStatus = $this->getStatus($task); if ($existingStatus) { //TODO - what should happen here? return null; } $taskKey = $task->getKey(); $this->redisClient->set($taskKey, $serialized); $this->redisClient->rpush($this->announceListKey, $taskKey); $this->setStatus($taskKey, TaskQueue::STATE_INITIAL); }
/** * @param Task $task * @return null */ public function addTask(Task $task) { $taskKey = $task->getKey(); $taskSpecificKey = $this->taskListKey . $taskKey; $serialized = serialize($task); $existingStatus = $this->getStatus($task); if ($existingStatus) { //TODO - what should happen here? return $taskSpecificKey; } $this->redisClient->set($taskSpecificKey, $serialized, 'EX', self::TASK_TTL); $this->redisClient->rpush($this->announceListKey, $taskKey); $this->setStatus($task, TaskQueue::STATE_INITIAL); return true; }
/** * @param string $queueName * @param mixed $workload */ public function put($queueName, $workload) { $this->predis->rpush($queueName, serialize($workload)); }
/** * @param array $record */ protected function write(array $record) { $this->redisClient->rpush($this->redisKey, sprintf('%s|%s', $record['level'], $record['message'])); }
/** * @param string $commandIdentifier * @param mixed $reply */ public function writeCommandReply($commandIdentifier, $reply) { $this->client->rpush(sprintf(self::COMMAND_RESPONSE_KEY, $commandIdentifier), [$reply]); $this->client->expire(sprintf(self::COMMAND_RESPONSE_KEY, $commandIdentifier), $this->timeout); }
/** * 设置列表数据 * @param string $key * @param array $elements * @return int */ public function setList($key, $elements) { // list:magazine:100:images 12 13 14 return $this->redis->rpush($key, (array) $elements); }
public function recordGauge($name, $value, $measureTime = null) { $counter = new \Stats\Gauge($name, $value, $this->sourceName, $measureTime); $serialized = serialize($counter); $this->redisClient->rpush($this->getGaugeKey(), [$serialized]); }
public function sendMessage($messageBody, $queueId = null) { $queueId = $this->normaliseQueueId($queueId); $this->predis->rpush($queueId, [$messageBody]); }
/** * {@inheritdoc} */ public function queueMessage(\Swift_Mime_Message $message) { $this->redis->rpush($this->key, serialize($message)); return true; }
/** * {@inheritdoc} */ public function pushRaw(string $payload, string $queue = null, array $options = []) { $this->redis->rpush($this->getQueue($queue), $payload); return Arr::get(json_decode($payload, true), 'id'); }