Skip to content

Commit 56fe2b4

Browse files
committed
use Core LongRunningOperations instead of GAX
1 parent 5f05477 commit 56fe2b4

18 files changed

Lines changed: 381 additions & 248 deletions
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Core\LongRunning;
19+
20+
use Google\ApiCore\OperationResponse;
21+
use Google\ApiCore\Serializer;
22+
use Google\LongRunning\Operation;
23+
use Google\LongRunning\ListOperationsRequest;
24+
use Google\LongRunning\GetOperationRequest;
25+
use Google\LongRunning\CancelOperationRequest;
26+
use Google\LongRunning\DeleteOperationRequest;
27+
use Google\Cloud\Core\RequestProcessorTrait;
28+
29+
/**
30+
* Defines the calls required to manage Long Running Operations using a GAPIC
31+
* generated client.
32+
*
33+
* @internal
34+
*/
35+
class LongRunningGapicConnection implements LongRunningConnectionInterface
36+
{
37+
use RequestProcessorTrait;
38+
39+
public function __construct(
40+
private $gapicClient,
41+
private Serializer $serializer
42+
) {
43+
}
44+
45+
/**
46+
* @param array $args
47+
*/
48+
public function get(array $args)
49+
{
50+
$operationResponse = $this->gapicClient->resumeOperation($args['name']);
51+
52+
return $this->operationResponseToArray($operationResponse);
53+
}
54+
55+
/**
56+
* @param array $args
57+
*/
58+
public function cancel(array $args)
59+
{
60+
$operationResponse = $this->gapicClient->resumeOperation(
61+
$args['name'],
62+
$args['method'] ?? null
63+
);
64+
$operationResponse->cancel();
65+
66+
return $this->operationResponseToArray($operationResponse);
67+
}
68+
69+
/**
70+
* @param array $args
71+
*/
72+
public function delete(array $args)
73+
{
74+
$operationResponse = $this->gapicClient->resumeOperation(
75+
$args['name'],
76+
$args['method'] ?? null
77+
);
78+
$operationResponse->cancel();
79+
80+
return $this->operationResponseToArray($operationResponse);
81+
}
82+
83+
/**
84+
* @param array $args
85+
*/
86+
public function operations(array $args)
87+
{
88+
$request = ListOperationsRequest::build($args['name'], $args['filter'] ?? null);
89+
$response = $this->gapicClient->getOperationsClient()->listOperations($request);
90+
91+
return $this->handleResponse($response);
92+
}
93+
94+
private function operationResponseToArray(OperationResponse $operationResponse)
95+
{
96+
$response = $this->handleResponse($operationResponse->getLastProtoResponse());
97+
$metaType = $response['metadata']['typeUrl'];
98+
99+
// unpack result Any type
100+
$response['response'] = $this->handleResponse($operationResponse->getResult());
101+
102+
// unpack error Any type
103+
$response['error'] = $this->handleResponse($operationResponse->getError());
104+
105+
$metadata = $operationResponse->getMetadata();
106+
if ($metadata instanceof Any) {
107+
// For some reason we aren't doing this in GAX OperationResponse (but we should)
108+
$metadata = $metadata->unpack();
109+
}
110+
$response['metadata'] = $this->handleResponse($metadata);
111+
112+
// Used in LongRunningOperation to invoke callables
113+
$response['metadata'] += ['typeUrl' => $metaType];
114+
115+
return $response;
116+
}
117+
}

Core/src/LongRunning/LongRunningOperation.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,11 @@ public function reload(array $options = [])
252252

253253
$this->result = null;
254254
$this->error = null;
255-
if (isset($res['done']) && $res['done']) {
255+
256+
if (isset($res['done']) && $res['done'] && isset($res['metadata']['typeUrl'])) {
256257
$type = $res['metadata']['typeUrl'];
257258
$this->result = $this->executeDoneCallback($type, $res['response']);
258-
$this->error = (isset($res['error']))
259-
? $res['error']
260-
: null;
259+
$this->error = $res['error'] ?? null;
261260
}
262261

263262
return $this->info = $res;

Spanner/src/Backup.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
use Closure;
2121
use DateTimeInterface;
22-
use Google\ApiCore\OperationResponse;
22+
use Google\Cloud\Core\LongRunning\LongRunningGapicConnection;
23+
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2324
use Google\ApiCore\ValidationException;
2425
use Google\Cloud\Core\Exception\NotFoundException;
2526
use Google\Cloud\Core\Iterator\ItemIterator;
@@ -95,14 +96,14 @@ public function __construct(
9596
* consistent copy of the database. If not present, it will be the same
9697
* as the create time of the backup.
9798
* }
98-
* @return OperationResponse
99+
* @return LongRunningOperation
99100
* @throws \InvalidArgumentException
100101
*/
101102
public function create(
102103
$database,
103104
DateTimeInterface $expireTime,
104105
array $options = []
105-
): OperationResponse {
106+
): LongRunningOperation {
106107
[$data, $callOptions] = $this->splitOptionalArgs($options);
107108

108109
$data += [
@@ -124,10 +125,10 @@ public function create(
124125
}
125126

126127
$request = $this->serializer->decodeMessage(new CreateBackupRequest(), $data);
127-
return $this->databaseAdminClient->createBackup($request, $callOptions + [
128+
$operation = $this->databaseAdminClient->createBackup($request, $callOptions + [
128129
'resource-prefix' => $this->instance->name(),
129-
])
130-
->withResultFunction($this->backupResultFunction());
130+
]);
131+
return $this->operationFromOperationResponse($operation);
131132
}
132133

133134
/**
@@ -151,14 +152,14 @@ public function create(
151152
* @param array $options [optional] {
152153
* Configuration Options.
153154
* }
154-
* @return OperationResponse
155+
* @return LongRunningOperation
155156
* @throws \InvalidArgumentException
156157
*/
157158
public function createCopy(
158159
Backup $newBackup,
159160
DateTimeInterface $expireTime,
160161
array $options = []
161-
): OperationResponse {
162+
): LongRunningOperation {
162163
[$data, $callOptions] = $this->splitOptionalArgs($options);
163164
$data += [
164165
'parent' => $newBackup->instance->name(),
@@ -169,10 +170,10 @@ public function createCopy(
169170

170171
$request = $this->serializer->decodeMessage(new CopyBackupRequest(), $data);
171172

172-
return $this->databaseAdminClient->copyBackup($request, $callOptions + [
173+
$operation = $this->databaseAdminClient->copyBackup($request, $callOptions + [
173174
'resource-prefix' => $this->instance->name(),
174-
])
175-
->withResultFunction($this->backupResultFunction());
175+
]);
176+
return $this->operationFromOperationResponse($operation);
176177
}
177178

178179
/**
@@ -355,19 +356,25 @@ public function updateExpireTime(DateTimeInterface $newTimestamp, array $options
355356
*
356357
* Example:
357358
* ```
358-
* $operation = $spanner->resumeOperation($operationName);
359+
* $operation = $backup->resumeOperation($operationName);
359360
* ```
360361
*
361362
* @param string $operationName The Long Running Operation name.
362-
* @return OperationResponse
363+
* @return LongRunningOperation
363364
*/
364-
public function resumeOperation($operationName, array $options = []): OperationResponse
365+
public function resumeOperation($operationName, array $options = []): LongRunningOperation
365366
{
366-
return (new OperationResponse(
367+
return new LongRunningOperation(
368+
new LongRunningGapicConnection($this->databaseAdminClient, $this->serializer),
367369
$operationName,
368-
$this->databaseAdminClient->getOperationsClient(),
370+
[
371+
[
372+
'typeUrl' => 'type.googleapis.com/google.spanner.admin.database.v1.CreateBackupMetadata',
373+
'callable' => $this->backupResultFunction(),
374+
]
375+
],
369376
$options
370-
))->withResultFunction($this->backupResultFunction());
377+
);
371378
}
372379

373380
/**
@@ -390,7 +397,7 @@ public function resumeOperation($operationName, array $options = []): OperationR
390397
* @type string $pageToken A previously-returned page token used to
391398
* resume the loading of results from a specific point.
392399
* }
393-
* @return ItemIterator<OperationResponse>
400+
* @return ItemIterator<LongRunningOperation>
394401
*/
395402
public function longRunningOperations(array $options = []): ItemIterator
396403
{
@@ -429,10 +436,9 @@ private function fullyQualifiedBackupName($name): string
429436

430437
private function backupResultFunction(): Closure
431438
{
432-
return function (BackupProto $backup) {
433-
$name = DatabaseAdminClient::parseName($backup->getName());
434-
$info = $this->serializer->decodeMessage($backup);
435-
return $this->instance->backup($name['name'], $info);
439+
return function (array $backup) {
440+
$name = DatabaseAdminClient::parseName($backup['name']);
441+
return $this->instance->backup($name['name'], $backup);
436442
};
437443
}
438444
}

0 commit comments

Comments
 (0)