-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathStreamingTransactionHandler.php
184 lines (158 loc) · 5 KB
/
StreamingTransactionHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* ArangoDB PHP client: streaming transaction handler
*
* @package ArangoDBClient
* @author Jan Steemann
* @copyright Copyright 2019, ArangoDB GmbH, Cologne, Germany
*/
namespace ArangoDBClient;
/**
* Provides management of streaming transactions
*
* @package ArangoDBClient
* @since 3.5
*/
class StreamingTransactionHandler extends Handler
{
private $_pendingTransactions = [];
/**
* Construct a new streaming transaction handler
*
* @param Connection $connection - connection to be used
*
*/
public function __construct(Connection $connection)
{
parent::__construct($connection);
register_shutdown_function(array($this, 'closePendingTransactions'));
}
/**
* Creates a streaming transaction from scratch (no collections) or from an
* existing transaction object (necessary when collections need to be passed
* into the transaction or when an existing transaction is resumed)
*
* @throws ServerException
*
* @param StreamingTransaction $trx - existing transaction
*/
public function create(StreamingTransaction $trx = null)
{
if ($trx === null) {
$trx = new StreamingTransaction($this->getConnection());
}
$response = $this->getConnection()->post(
Urls::URL_TRANSACTION . '/begin',
$this->getConnection()->json_encode_wrapper($trx->attributes)
);
$jsonResponse = $response->getJson();
$id = $jsonResponse['result']['id'];
$trx->setId($id);
$this->_pendingTransactions[$id] = true;
return $trx;
}
/**
* Closes all pending transactions created by the handler
*/
public function closePendingTransactions()
{
// automatically abort all unintentionally kept-open transactions, so we don't
// leak server resources by not closing transactions
foreach ($this->_pendingTransactions as $id => $done) {
try {
$this->abort($id);
} catch (\Exception $e) {
// ignore all errors here
}
}
$this->_pendingTransactions = [];
}
/**
* Steal the transaction from the handler, so that it is not responsible anymore
* for auto-aborting it on shutdown
*
* @param string $id - transaction id
*/
public function stealTransaction($id)
{
unset($this->_pendingTransactions[$id]);
}
/**
* Retrieves the status of a transaction
*
* @throws ServerException
*
* @param mixed $trx - streaming transaction object or transaction id as string
*
* @return array - returns an array with attributes 'id' and 'status'
*/
public function getStatus($trx)
{
if ($trx instanceof StreamingTransaction) {
$id = $trx->getId();
} else {
$id = (string) $trx;
}
$response = $this->getConnection()->get(UrlHelper::buildUrl(Urls::URL_TRANSACTION, [$id]));
$jsonResponse = $response->getJson();
$status = $jsonResponse['result']['status'];
if ($status === 'aborted' || $status === 'committed') {
$this->stealTransaction($id);
}
return $jsonResponse['result'];
}
/**
* Commits a transaction
*
* @throws ServerException
*
* @param mixed $trx - streaming transaction object or transaction id as string
*
* @return bool - true if commit succeeds, throws an exception otherwise
*/
public function commit($trx)
{
if ($trx instanceof StreamingTransaction) {
$id = $trx->getId();
} else {
$id = (string) $trx;
}
$this->stealTransaction($id);
$this->getConnection()->put(UrlHelper::buildUrl(Urls::URL_TRANSACTION, [$id]), '');
return true;
}
/**
* Aborts a transaction
*
* @throws ServerException
*
* @param mixed $trx - streaming transaction object or transaction id as string
*
* @return bool - true if abort succeeds, throws an exception otherwise
*/
public function abort($trx)
{
if ($trx instanceof StreamingTransaction) {
$id = $trx->getId();
} else {
$id = (string) $trx;
}
$this->stealTransaction($id);
$this->getConnection()->delete(UrlHelper::buildUrl(Urls::URL_TRANSACTION, [$id]));
return true;
}
/**
* Return all currently running transactions
*
* @throws ServerException
*
* @return array - array of currently running transactions, each transaction is an array with attributes 'id' and 'status'
*/
public function getRunning()
{
$response = $this->getConnection()->get(Urls::URL_TRANSACTION);
$jsonResponse = $response->getJson();
return $jsonResponse['transactions'];
}
}
class_alias(CollectionHandler::class, '\triagens\ArangoDb\StreamingTransactionHandler');