/** * Put a job in the queue. * * @param string $queueName Queue name * @param JobInterface $job The job * @param \DateTime $availableAt Time when the job is available for executing */ public function queue($queueName, JobInterface $job, \DateTime $availableAt = null) { $queue = $this->queueManager->getQueue($queueName); $payload = serialize($job); $message = new Message($payload); $message->setAvailableAt($availableAt); $queue->publish($message); }
/** * @test */ public function publishMessageWithDelayAndWaitAndReserveUsingTimeoutAndFinishMessage() { // Make sure the queue is empty. $this->assertSame(null, $this->queue->waitAndReserve(), 'Queue should be empty!'); // Publish new message. $newMessage = new Message('TYPO3'); $newMessage->setAvailableAt(new \DateTime('now + 2sec')); $this->assertSame(2, $newMessage->getDelay(), 'Delay does not match!'); $this->queue->publish($newMessage); // Do the tests. $this->assertSame(null, $this->queue->waitAndReserve(), 'There should be no job available at this moment!'); $message = $this->queue->waitAndReserve(3); $this->assertInstanceOf(Message::class, $message); $this->assertSame(Message::STATE_RESERVED, $message->getState(), 'Message has not the state reserved!'); $this->assertNotEmpty($message->getIdentifier(), 'Message identifier should be set!'); $this->assertTrue($this->queue->finish($message), 'Message could not be finished!'); $this->assertSame(Message::STATE_DONE, $message->getState(), 'Message has not the state done!'); }
/** * Decode a message from a string representation * * @param string $value * @return R3H6\Jobqueue\Queue\Message */ protected function decodeMessage($value) { $decodedMessage = json_decode($value, true); $message = new Message($decodedMessage['payload'], $decodedMessage['identifier']); $message->setState($decodedMessage['state']); $message->setAttemps($decodedMessage['attemps']); return $message; }
/** * @test */ public function setAvailableAtAndGetDelay() { $dateTimeFixture = new \DateTime('now + 7sec'); $this->subject->setAvailableAt($dateTimeFixture); $this->assertSame(7, $this->subject->getDelay()); }
/** * Converts a data model into a message. * * @param R3H6\JobqueueDatabase\Domain\Model\Job $job * @return Message */ private function decodeJob(DatabaseJob $job) { $message = new Message($job->getPayload(), $job->getUid()); $message->setState($job->getState()); $message->setAttemps($job->getAttemps()); return $message; }