Use {@see \Google\Cloud\PubSub\Subscription::acknowledge()} to acknowledge
a single message.
Example:
$messages = $subscription->pull();
$messagesArray = iterator_to_array($messages);
$subscription->acknowledgeBatch($messagesArray);
public function __construct(Subscription $subscription, $job, LoggerInterface $logger) { // [START callback] $callback = function ($response) use($job, $subscription, $logger) { $ackIds = []; $messages = json_decode($response->getBody(), true); if (isset($messages['receivedMessages'])) { foreach ($messages['receivedMessages'] as $message) { $attributes = $message['message']['attributes']; $logger->info(sprintf('Message received for book ID "%s" ', $attributes['id'])); // Do the actual work in the LookupBookDetailsJob class $job->work($attributes['id']); $ackIds[] = $message['ackId']; } } // Acknowledge the messsages have been handled if (!empty($ackIds)) { $subscription->acknowledgeBatch($ackIds); } }; // [END callback] $this->callback = $callback; $this->subscription = $subscription; $this->connection = new AsyncConnection(); }
public function testAcknowledgeBatch() { $ackIds = ['foobar', 'otherAckId']; $this->connection->acknowledge(Argument::that(function ($args) use($ackIds) { if ($args['foo'] !== 'bar') { return false; } if ($args['ackIds'] !== $ackIds) { return false; } return true; }))->shouldBeCalledTimes(1); $subscription = new Subscription($this->connection->reveal(), 'subscription-name', 'topic-name', 'project-id'); $subscription->acknowledgeBatch($ackIds, ['foo' => 'bar']); }