|
12 | 12 | use Cbws\Sdk\Common\Longrunning\V1alpha1\DeleteOperationRequest; |
13 | 13 | use Cbws\Sdk\Common\Longrunning\V1alpha1\GetOperationRequest; |
14 | 14 | use Cbws\Sdk\Common\Longrunning\V1alpha1\WaitOperationRequest; |
| 15 | +use Fiber; |
15 | 16 | use Generator; |
16 | 17 | use Google\Protobuf\GPBEmpty; |
17 | 18 |
|
@@ -181,4 +182,38 @@ public function awaitOperation(string $name, string $metadataType, string $respo |
181 | 182 | /** @phpstan-var Operation<TMetadata, TResponse> */ |
182 | 183 | return $operation; |
183 | 184 | } |
| 185 | + |
| 186 | + /** |
| 187 | + * Provider Fiber for waiting until the operation has finished, either successfully or with an error. |
| 188 | + * |
| 189 | + * @template TMetadata of MetadataInterface |
| 190 | + * @template TResponse of ResponseInterface |
| 191 | + * |
| 192 | + * @param class-string<TMetadata> $metadataType |
| 193 | + * @param class-string<TResponse> $responseType |
| 194 | + * |
| 195 | + * @return Fiber<?int, ?int, Operation<TMetadata, TResponse>, Operation<TMetadata, TResponse>> |
| 196 | + */ |
| 197 | + public function fiberOperation(string $name, string $metadataType, string $responseType): Fiber |
| 198 | + { |
| 199 | + /** @phpstan-var Fiber<?int, ?int, Operation<TMetadata, TResponse>, Operation<TMetadata, TResponse>> */ |
| 200 | + return new Fiber(function (?int $timeout = 5) use ($name, $metadataType, $responseType) { |
| 201 | + do { |
| 202 | + $operation = $this->waitOperation($name, $metadataType, $responseType, $timeout ?: 5); |
| 203 | + |
| 204 | + // We should see if we can have a way of providing a non-blocking way to implement fibers, doing get over |
| 205 | + // and over again would put a massive load on our service. |
| 206 | + // if ($timeout !== null) { |
| 207 | + // $operation = $this->waitOperation($name, $metadataType, $responseType, $timeout); |
| 208 | + // } else { |
| 209 | + // $operation = $this->getOperation($name, $metadataType, $responseType); |
| 210 | + // } |
| 211 | + |
| 212 | + /** @phpstan-var ?int $timeout */ |
| 213 | + $timeout = Fiber::suspend($operation); |
| 214 | + } while (!$operation->getDone()); |
| 215 | + |
| 216 | + return $operation; |
| 217 | + }); |
| 218 | + } |
184 | 219 | } |
0 commit comments