Skip to content

Commit

Permalink
Merge pull request #16 from trandangtri/feature/logging-worker
Browse files Browse the repository at this point in the history
Add more logging message to the base worker
  • Loading branch information
trandangtri authored Oct 20, 2017
2 parents 3d0737c + d5a0644 commit 4409b10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
<argument type="service" id="aws.sqs" />
</service>

<service id="tritran.sqs_queue.queue_worker" class="TriTran\SqsQueueBundle\Service\BaseWorker" />
<service id="tritran.sqs_queue.queue_worker" class="TriTran\SqsQueueBundle\Service\BaseWorker">
<call method="setLogger">
<argument type="service" id="logger" on-invalid="ignore"/>
</call>
</service>

<service id="tritran.sqs_queue.queue_manager" class="TriTran\SqsQueueBundle\Service\QueueManager">
<argument type="service" id="aws.sqs" />
Expand Down
13 changes: 13 additions & 0 deletions Service/BaseWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace TriTran\SqsQueueBundle\Service;

use Psr\Log\LoggerAwareTrait;

/**
* Class BaseWorker
* @package TriTran\SqsQueueBundle\Service
*/
class BaseWorker
{
use LoggerAwareTrait;

/**
* @param BaseQueue $queue
* @param int $limit Zero is all
Expand Down Expand Up @@ -43,10 +47,19 @@ private function fetchMessage(BaseQueue $queue, int $limit = 1)
while ($messages->valid()) {
/** @var Message $message */
$message = $messages->current();

$this->logger && $this->logger->info(sprintf('Processing message ID: %s', $message->getId()));
$result = $consumer->process($message);

if ($result !== false) {
$this->logger && $this->logger->info(
sprintf('Successfully processed message ID: %s', $message->getId())
);
$queue->deleteMessage($message);
} else {
$this->logger && $this->logger->warning(
sprintf('Cannot process message ID: %s, will release it back to queue', $message->getId())
);
$queue->releaseMessage($message);
}

Expand Down

0 comments on commit 4409b10

Please sign in to comment.