|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 SURFnet bv |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline; |
|
20
|
|
|
|
|
21
|
|
|
use Doctrine\DBAL\Driver\Connection; |
|
22
|
|
|
use Psr\Log\LoggerInterface; |
|
23
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command; |
|
24
|
|
|
|
|
25
|
|
|
class TransactionAwarePipeline implements Pipeline |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var Pipeline |
|
29
|
|
|
*/ |
|
30
|
|
|
private $innerPipeline; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Connection |
|
34
|
|
|
*/ |
|
35
|
|
|
private $middlewareConnection; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var Connection |
|
39
|
|
|
*/ |
|
40
|
|
|
private $gatewayConnection; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var LoggerInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $logger; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param LoggerInterface $logger |
|
49
|
|
|
* @param Pipeline $innerPipeline |
|
50
|
|
|
* @param Connection $middlewareConnection |
|
51
|
|
|
* @param Connection $gatewayConnection |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( |
|
54
|
|
|
LoggerInterface $logger, |
|
55
|
|
|
Pipeline $innerPipeline, |
|
56
|
|
|
Connection $middlewareConnection, |
|
57
|
|
|
Connection $gatewayConnection |
|
58
|
|
|
) { |
|
59
|
|
|
$this->logger = $logger; |
|
60
|
|
|
$this->innerPipeline = $innerPipeline; |
|
61
|
|
|
$this->middlewareConnection = $middlewareConnection; |
|
62
|
|
|
$this->gatewayConnection = $gatewayConnection; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function process(Command $command) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->logger->debug(sprintf( |
|
68
|
|
|
'Starting Transaction in TransactionAwarePipeline for processing command "%s"', |
|
69
|
|
|
$command |
|
70
|
|
|
)); |
|
71
|
|
|
|
|
72
|
|
|
$this->middlewareConnection->beginTransaction(); |
|
73
|
|
|
$this->gatewayConnection->beginTransaction(); |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
$this->logger->debug(sprintf('Requesting inner pipeline to process command "%s"', $command)); |
|
77
|
|
|
|
|
78
|
|
|
$command = $this->innerPipeline->process($command); |
|
79
|
|
|
|
|
80
|
|
|
$this->logger->debug(sprintf('Inner pipeline processed command "%s", committing transaction', $command)); |
|
81
|
|
|
|
|
82
|
|
|
$this->middlewareConnection->commit(); |
|
83
|
|
|
$this->gatewayConnection->commit(); |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
// log at highest level if we may have a split head in the db-cluster... |
|
86
|
|
|
if (strpos($e->getMessage(), 'ER_UNKNOWN_COM_ERROR')) { |
|
87
|
|
|
$this->logger->emergency( |
|
88
|
|
|
sprintf( |
|
89
|
|
|
'[!!!] Critical Database Exception while processing command "%s": "%s"', |
|
90
|
|
|
$command, |
|
91
|
|
|
$e->getMessage() |
|
92
|
|
|
), |
|
93
|
|
|
['exception' => $e] |
|
94
|
|
|
); |
|
95
|
|
|
} else { |
|
96
|
|
|
$this->logger->error( |
|
97
|
|
|
sprintf( |
|
98
|
|
|
'Exception occurred while processing command "%s": "%s", rolling back transaction', |
|
99
|
|
|
$command, |
|
100
|
|
|
$e->getMessage() |
|
101
|
|
|
), |
|
102
|
|
|
['exception' => $e] |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->middlewareConnection->rollBack(); |
|
107
|
|
|
$this->gatewayConnection->rollBack(); |
|
108
|
|
|
|
|
109
|
|
|
$this->logger->debug(sprintf( |
|
110
|
|
|
'Transaction for command "%s" rolled back, re-throwing exception', |
|
111
|
|
|
$command |
|
112
|
|
|
)); |
|
113
|
|
|
|
|
114
|
|
|
throw $e; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->logger->debug(sprintf('Transaction committed, done processing command "%s"', $command)); |
|
118
|
|
|
|
|
119
|
|
|
return $command; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|