Skip to content

Commit 87a84fb

Browse files
authored
Merge pull request #21 from ppavlovic/master
Added ErrorLogger instead of trigger_error
2 parents 45dbbd7 + 4f11b89 commit 87a84fb

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"ext-pdo" : "*",
3131
"php" : ">=5.6",
3232
"g4/cron" : "*",
33-
"g4/log" : "1.*"
33+
"g4/log" : "1.13.*"
3434
},
3535
"suggest": {
3636
"php-amqplib/php-amqplib": "Use RabbitMQ for task processing"

src/TaskAbstract.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
abstract class TaskAbstract
55
{
6+
const RELOCATED = 'RABBITMQ_NOT_AVAILABLE';
7+
68
private $createdTs;
79

810
private $data;
@@ -25,6 +27,12 @@ public function addDelay($value)
2527
return $this;
2628
}
2729

30+
public function relocateToPersistence()
31+
{
32+
$this->data[self::RELOCATED] = true;
33+
return $this;
34+
}
35+
2836
/**
2937
* @return bool
3038
*/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace G4\Tasker\Tasker2\Exception;
4+
5+
class RabbitmqNotAvailableException extends \RuntimeException
6+
{
7+
public function __construct($message = null)
8+
{
9+
if (!$message) {
10+
$message = 'RabbitMQ connection is not available for Tasker Manager';
11+
}
12+
parent::__construct($message);
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace G4\Tasker\Tasker2\Exception;
4+
5+
class TasksRelocatedToPersistenceException extends \RuntimeException
6+
{
7+
public function __construct($countTasks)
8+
{
9+
$message = sprintf(
10+
'RabbitMQ connection is not available for Tasker TaskQueue, relocated %d tasks to persistence',
11+
$countTasks
12+
);
13+
14+
parent::__construct($message);
15+
}
16+
}

src/Tasker2/Manager.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace G4\Tasker\Tasker2;
44

5+
use G4\Tasker\Tasker2\Exception\RabbitmqNotAvailableException;
56
use G4\Tasker\Tasker2\Queue\BatchPublisher;
67
use PhpAmqpLib\Connection\AMQPStreamConnection;
78

@@ -31,6 +32,11 @@ class Manager
3132
*/
3233
private $messageOptions;
3334

35+
/**
36+
* @var \G4\Log\ErrorLogger
37+
*/
38+
private $errorLogger;
39+
3440
public function __construct(
3541
\G4\Tasker\Model\Repository\TaskRepositoryInterface $taskRepository,
3642
AMQPStreamConnection $rabbitMqConnection = null,
@@ -42,11 +48,17 @@ public function __construct(
4248
$this->messageOptions = $messageOptions;
4349
}
4450

51+
public function setErrorLogger(\G4\Log\ErrorLogger $logger)
52+
{
53+
$this->errorLogger = $logger;
54+
return $this;
55+
}
56+
4557
public function run()
4658
{
4759
if ($this->rabbitMqConnection === null) {
4860
// no rabbitmq connection is available
49-
trigger_error('RabbitMQ connection is not available for Tasker Manager', E_USER_NOTICE);
61+
$this->errorLogger !== null && $this->errorLogger->log(new RabbitmqNotAvailableException());
5062
return;
5163
}
5264
$this

src/Tasker2/TaskQueue.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace G4\Tasker\Tasker2;
44

55
use G4\Tasker\TaskAbstract;
6+
use G4\Tasker\Tasker2\Exception\TasksRelocatedToPersistenceException;
67
use G4\Tasker\Tasker2\Queue\BatchPublisher;
78
use PhpAmqpLib\Connection\AMQPStreamConnection;
89
use G4\ValueObject\Uuid;
@@ -34,6 +35,11 @@ class TaskQueue
3435
*/
3536
private $requestUuid;
3637

38+
/**
39+
* @var \G4\Log\ErrorLogger
40+
*/
41+
private $errorLogger;
42+
3743
public function __construct(
3844
\G4\Tasker\Queue $queue,
3945
AMQPStreamConnection $AMQPConnection = null,
@@ -48,6 +54,12 @@ public function __construct(
4854
$this->requestUuid = $requestUuid;
4955
}
5056

57+
public function setErrorLogger(\G4\Log\ErrorLogger $logger)
58+
{
59+
$this->errorLogger = $logger;
60+
return $this;
61+
}
62+
5163
public function add(\G4\Tasker\TaskAbstract $task)
5264
{
5365
$this->tasks[] = clone $task;
@@ -99,8 +111,7 @@ private function saveCurrentTasks($tasks)
99111

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

@@ -118,6 +129,18 @@ private function saveCurrentTasks($tasks)
118129
return $this;
119130
}
120131

132+
private function delayCurrentTasks($tasks)
133+
{
134+
$tasksModified = array_map(function(TaskAbstract $task) {
135+
return $task->relocateToPersistence();
136+
}, $tasks);
137+
138+
$this->saveDelayedTasks($tasksModified);
139+
$this->errorLogger !== null
140+
&& $this->errorLogger->log(new TasksRelocatedToPersistenceException(count($tasksModified))
141+
);
142+
}
143+
121144
private function getRequestUuid()
122145
{
123146
return $this->requestUuid !== null

0 commit comments

Comments
 (0)