1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace CircuitBreakerBundle\Traits; |
4
|
|
|
|
5
|
|
|
use CircuitBreakerBundle\Service\CircuitBreakerService; |
6
|
|
|
use CircuitBreakerBundle\Event\CircuitBreakerEvent; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
|
9
|
|
|
trait CircuitBreakerAwareTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var CircuitBreakerService |
13
|
|
|
*/ |
14
|
|
|
private $circuitBreaker; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var EventDispatcherInterface |
18
|
|
|
*/ |
19
|
|
|
private $eventDispatcher; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set the circuit breaker service |
23
|
|
|
* |
24
|
|
|
* @param CircuitBreakerService $circuitBreaker |
25
|
|
|
*/ |
26
|
1 |
|
public function setCircuitBreaker(CircuitBreakerService $circuitBreaker) |
27
|
|
|
{ |
28
|
1 |
|
$this->circuitBreaker = $circuitBreaker; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
33
|
|
|
*/ |
34
|
3 |
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
35
|
|
|
{ |
36
|
3 |
|
$this->eventDispatcher = $eventDispatcher; |
37
|
3 |
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Send an event to notify the circuit breaker that a given service is down |
42
|
|
|
* |
43
|
|
|
* @param string $serviceName The service which is down |
44
|
|
|
*/ |
45
|
1 |
|
public function notifyServiceIsDown(string $serviceName) |
46
|
|
|
{ |
47
|
1 |
|
$this->eventDispatcher->dispatch( |
48
|
1 |
|
'circuit.breaker', |
49
|
1 |
|
new CircuitBreakerEvent($serviceName, CircuitBreakerService::STATUS_DOWN) |
50
|
|
|
); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Send an event to notify the circuit breaker that a given service is up |
55
|
|
|
* |
56
|
|
|
* @param string $serviceName The service which is up |
57
|
|
|
*/ |
58
|
1 |
|
public function notifyServiceIsUp(string $serviceName) |
59
|
|
|
{ |
60
|
1 |
|
$this->eventDispatcher->dispatch( |
61
|
1 |
|
'circuit.breaker', |
62
|
1 |
|
new CircuitBreakerEvent($serviceName, CircuitBreakerService::STATUS_UP) |
63
|
|
|
); |
64
|
1 |
|
} |
65
|
|
|
} |
66
|
|
|
|