/** * @expectedException \Phloppy\Exception\CommandException * @expectedExceptionMessage MAXLEN Queue is already longer than the specified MAXLEN count */ public function testAddJobMaxlen() { $queue = 'test-' . substr(sha1(mt_rand()), 0, 6); $producer = new Producer($this->stream); $producer->addJob($queue, Job::create(['body' => 'job-maxlen-1'])); $producer->addJob($queue, Job::create(['body' => 'job-maxlen-2'])); $producer->addJob($queue, Job::create(['body' => 'job-maxlen-3']), 1); }
/** * @expectedException \Phloppy\Exception\CommandException * @expectedExceptionMessage ERR Foo */ public function testAddJobWithInvalidReplicate() { $mock = $this->getMock('\\Phloppy\\Stream\\StreamInterface'); $mock->expects($this->any())->method('write')->willReturn($mock); $mock->expects($this->any())->method('readLine')->willReturn("-ERR Foo"); $p = new Producer($mock); $p->addJob('test', Job::create(['body' => 42])); }
public function testWorking() { $retry = rand(30, 60); $queue = 'test-' . substr(sha1(mt_rand()), 0, 6); $consumer = new Consumer($this->stream); $producer = new Producer($this->stream); $producer->addJob($queue, Job::create(['body' => __METHOD__, 'retry' => $retry, 'ttl' => 600])); $job = $consumer->getJob($queue); $this->assertEquals($retry, $consumer->working($job)); }
/** * Enqueue the given job. * * @param string $queue * @param Job $job * @param int $maxlen specifies that if there are already count messages queued for the specified queue name, * the message is refused and an error reported to the client. * @oaram bool $async asks the server to let the command return ASAP and replicate the job to other nodes in the * background. The job gets queued ASAP, while normally the job is put into the queue only when * the client gets a positive reply. * * @return Job The updated job (e.g. ID set). */ public function addJob($queue, Job $job, $maxlen = 0, $async = false) { $command = ['ADDJOB', $queue, $job->getBody(), $this->getReplicationTimeout(), 'REPLICATE', $this->getReplicationFactor(), 'DELAY', $job->getDelay(), 'RETRY', $job->getRetry(), 'TTL', $job->getTtL()]; if ($maxlen) { $command[] = 'MAXLEN'; $command[] = (int) $maxlen; } if ($async) { $command[] = 'ASYNC'; } $id = $this->send($command); $job->setId($id); $job->setQueue($queue); return $job; }
public function testJScan() { $p = new Producer($this->stream); $queue = uniqid('jscan_'); $n = rand(0, 100); for ($i = 0; $i < $n; $i++) { $p->addJob($queue, Job::create(['body' => '23'])); } $node = new Node($this->stream, $this->log); $it = $node->jscan(5, [$queue], [Job::STATE_QUEUED]); $busy = $node->jscan(0, [$queue], [Job::STATE_QUEUED]); $iterCount = $busyCount = 0; $jobs = []; foreach ($it as $k => $job) { $iterCount++; $jobs[] = $job; } foreach ($busy as $k => $job) { $busyCount++; } // cleanup $this->assertEquals($n, $node->del($jobs)); $this->assertEquals($n, $iterCount); }
public function testEnqueueDequeue() { $queueName = 'test-' . substr(sha1(mt_rand()), 0, 6); $queue = new Queue($this->stream); $producer = new Producer($this->stream); $node = new Node($this->stream); $this->assertEquals(0, $queue->len($queueName)); $job1 = $producer->addJob($queueName, Job::create(['body' => 'test-enq-deq-1'])); $job2 = $producer->addJob($queueName, Job::create(['body' => 'test-enq-deq-2'])); $this->assertEquals(2, $queue->len($queueName)); // dequeue one of the jobs $this->assertEquals(1, $queue->dequeue([$job1->getId()])); // now only one job left in queue $this->assertEquals(1, $queue->len($queueName)); // job2 still enqueued, so none should be enqueued $this->assertEquals(0, $queue->enqueue([$job2->getId()])); // job1 is enqueued again, so 1 should be returned $this->assertEquals(1, $queue->enqueue([$job1->getId()])); // and the queue has len 2 again $this->assertEquals(2, $queue->len($queueName)); // cleanup $this->assertEquals(2, $node->del([$job1->getId(), $job2->getId()])); }
/** * Map Disque's job responses to Job objects. * * @param array $list Job response array from the disque server. * @return Job[] */ protected function mapJobs(array $list) { return array_map(function ($element) { return Job::create(['queue' => $element[0], 'id' => $element[1], 'body' => isset($element[2]) ? $element[2] : '']); }, $list); }
/** * Send a notice to the server that this client is still processing the job. * * @param Job $job * * @return int The current retry time for the job * * @see https://github.com/antirez/disque#working-jobid */ public function working(Job $job) { return (int) $this->send(['WORKING', $job->getId()]); }