Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ext-pdo" : "*",
"php" : ">=5.6",
"g4/cron" : "*",
"g4/log" : "1.*"
"g4/log" : "1.13.*"
},
"suggest": {
"php-amqplib/php-amqplib": "Use RabbitMQ for task processing"
Expand Down
8 changes: 8 additions & 0 deletions src/TaskAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

abstract class TaskAbstract
{
const RELOCATED = 'RABBITMQ_NOT_AVAILABLE';

private $createdTs;

private $data;
Expand All @@ -25,6 +27,12 @@ public function addDelay($value)
return $this;
}

public function relocateToPersistence()
{
$this->data[self::RELOCATED] = true;
return $this;
}

/**
* @return bool
*/
Expand Down
14 changes: 14 additions & 0 deletions src/Tasker2/Exception/RabbitmqNotAvailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace G4\Tasker\Tasker2\Exception;

class RabbitmqNotAvailableException extends \RuntimeException
{
public function __construct($message = null)
{
if (!$message) {
$message = 'RabbitMQ connection is not available for Tasker Manager';
}
parent::__construct($message);
}
}
16 changes: 16 additions & 0 deletions src/Tasker2/Exception/TasksRelocatedToPersistenceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace G4\Tasker\Tasker2\Exception;

class TasksRelocatedToPersistenceException extends \RuntimeException
{
public function __construct($countTasks)
{
$message = sprintf(
'RabbitMQ connection is not available for Tasker TaskQueue, relocated %d tasks to persistence',
$countTasks
);

parent::__construct($message);
}
}
14 changes: 13 additions & 1 deletion src/Tasker2/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace G4\Tasker\Tasker2;

use G4\Tasker\Tasker2\Exception\RabbitmqNotAvailableException;
use G4\Tasker\Tasker2\Queue\BatchPublisher;
use PhpAmqpLib\Connection\AMQPStreamConnection;

Expand Down Expand Up @@ -31,6 +32,11 @@ class Manager
*/
private $messageOptions;

/**
* @var \G4\Log\ErrorLogger
*/
private $errorLogger;

public function __construct(
\G4\Tasker\Model\Repository\TaskRepositoryInterface $taskRepository,
AMQPStreamConnection $rabbitMqConnection = null,
Expand All @@ -42,11 +48,17 @@ public function __construct(
$this->messageOptions = $messageOptions;
}

public function setErrorLogger(\G4\Log\ErrorLogger $logger)
{
$this->errorLogger = $logger;
return $this;
}

public function run()
{
if ($this->rabbitMqConnection === null) {
// no rabbitmq connection is available
trigger_error('RabbitMQ connection is not available for Tasker Manager', E_USER_NOTICE);
$this->errorLogger !== null && $this->errorLogger->log(new RabbitmqNotAvailableException());
return;
}
$this
Expand Down
27 changes: 25 additions & 2 deletions src/Tasker2/TaskQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace G4\Tasker\Tasker2;

use G4\Tasker\TaskAbstract;
use G4\Tasker\Tasker2\Exception\TasksRelocatedToPersistenceException;
use G4\Tasker\Tasker2\Queue\BatchPublisher;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use G4\ValueObject\Uuid;
Expand Down Expand Up @@ -34,6 +35,11 @@ class TaskQueue
*/
private $requestUuid;

/**
* @var \G4\Log\ErrorLogger
*/
private $errorLogger;

public function __construct(
\G4\Tasker\Queue $queue,
AMQPStreamConnection $AMQPConnection = null,
Expand All @@ -48,6 +54,12 @@ public function __construct(
$this->requestUuid = $requestUuid;
}

public function setErrorLogger(\G4\Log\ErrorLogger $logger)
{
$this->errorLogger = $logger;
return $this;
}

public function add(\G4\Tasker\TaskAbstract $task)
{
$this->tasks[] = clone $task;
Expand Down Expand Up @@ -99,8 +111,7 @@ private function saveCurrentTasks($tasks)

if ($this->AMQPConnection === null) {
// in case that rabbitmq is not available save tasks to database
$this->saveDelayedTasks($tasks);
trigger_error('RabbitMQ connection is not available for Tasker TaskQueue', E_USER_NOTICE);
$this->delayCurrentTasks($tasks);
return $this;
}

Expand All @@ -118,6 +129,18 @@ private function saveCurrentTasks($tasks)
return $this;
}

private function delayCurrentTasks($tasks)
{
$tasksModified = array_map(function(TaskAbstract $task) {
return $task->relocateToPersistence();
}, $tasks);

$this->saveDelayedTasks($tasksModified);
$this->errorLogger !== null
&& $this->errorLogger->log(new TasksRelocatedToPersistenceException(count($tasksModified))
);
}

private function getRequestUuid()
{
return $this->requestUuid !== null
Expand Down