google/longrunning
Idiomatic PHP client for Google’s LongRunning Operations API. Install via Composer, supports REST and optional gRPC for streaming, and integrates with Google Cloud PHP authentication and debugging guides. Beta but largely stable.
Install the Package:
composer require google/longrunning
Ensure ext-grpc is installed for gRPC support (recommended for high-throughput scenarios).
Set Up Authentication:
Use a service account key file (JSON) and configure credentials via the Google\Auth\Credentials class or environment variables:
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\LongRunning\LongRunningClient;
$credentials = ServiceAccountCredentials::fromStream(__DIR__.'/path/to/service-account.json');
$client = new LongRunningClient(['credentials' => $credentials]);
First Use Case: Poll an Operation
Trigger a GCP operation (e.g., via google/cloud-compute or another GCP SDK) and poll its status:
$operationName = 'projects/my-project/regions/us-central1/operations/op-123456789';
$operation = $client->getOperation($operationName);
// Poll until completion with exponential backoff
$result = $client->pollUntilDone($operation);
Check Documentation:
google/cloud-compute) to start an operation, then poll its status with LongRunningClient.// Start an operation (e.g., via Compute Engine SDK)
$instance = $computeClient->instances->insert('my-project', 'my-zone', $config);
$operationName = $instance->getName();
// Poll the operation
$operation = $client->getOperation($operationName);
$result = $client->pollUntilDone($operation);
$operationName = 'projects/my-project/regions/us-central1/operations/op-123456789';
$client->cancelOperation($operationName);
$filter = 'region=us-central1';
$operations = $client->listOperations('projects/my-project', ['filter' => $filter]);
foreach ($operations as $operation) {
if ($operation->getStatus() === 'FAILED') {
// Handle failure (e.g., log, notify user)
}
}
$client = new LongRunningClient([
'credentials' => $credentials,
'retrySettings' => [
'maxAttempts' => 10,
'initialBackoff' => 1.0, // seconds
'maxBackoff' => 60.0, // seconds
'backoffFactor' => 2.0,
],
]);
LongRunningClient in Laravel background jobs to poll GCP operations.use Google\LongRunning\LongRunningClient;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class PollGcpOperation implements ShouldQueue
{
use Queueable;
protected $operationName;
public function __construct(string $operationName)
{
$this->operationName = $operationName;
}
public function handle(LongRunningClient $client)
{
$operation = $client->getOperation($this->operationName);
$result = $client->pollUntilDone($operation);
// Process $result (e.g., save to DB, notify user)
}
}
Bind LongRunningClient in Laravel’s service container for easy access:
// config/app.php
'providers' => [
// ...
App\Providers\GoogleLongRunningServiceProvider::class,
];
// App\Providers\GoogleLongRunningServiceProvider.php
public function register()
{
$this->app->singleton(LongRunningClient::class, function ($app) {
$credentials = ServiceAccountCredentials::fromStream(storage_path('app/gcp-credentials.json'));
return new LongRunningClient(['credentials' => $credentials]);
});
}
Google\ApiCore\ApiException for API errors (e.g., invalid operation names, permission denied).Google\ApiCore\Operation\Operation methods like getError() to inspect failures:
try {
$result = $client->pollUntilDone($operation);
} catch (ApiException $e) {
$error = $operation->getError();
logger()->error("GCP Operation Failed: {$error['message']}");
}
ext-grpc is installed and configure the client:
$client = new LongRunningClient([
'credentials' => $credentials,
'grpcOptions' => [
'ssl_target_name_override' => 'your-service-account-id',
],
]);
$client = new LongRunningClient([
'credentials' => $credentials,
'logger' => new \Monolog\Logger('gcp', [
new \Monolog\Handler\StreamHandler(storage_path('logs/gcp.log'), \Monolog\Logger::DEBUG),
]),
]);
partial_success flag in listOperations to handle batch jobs where some resources succeed and others fail:
$operations = $client->listOperations('projects/my-project', [
'filter' => 'region=us-central1',
'partial_success' => true,
]);
roles/longrunning.operations.getter).roles/compute.admin for Compute Engine).Google\Auth\Credentials::fromStream() with a JSON key file and validate scopes:
$credentials = ServiceAccountCredentials::fromStream($keyFilePath)
->withQuotaProject('your-project-id')
->withScopes([Google_Service_ComputeEngine::CLOUD_PLATFORM]);
projects/ or regions/).projects/{PROJECT_ID}/regions/{REGION}/operations/{OPERATION_ID}
$instance->getName() in Compute Engine).PENDING or RUNNING due to misconfigured polling.pollUntilDone() with custom retry settings or implement a watchdog timeout:
$startTime = time();
while (true) {
$operation = $client->getOperation($operationName);
if ($operation->getDone()) {
break;
}
if (time() - $startTime > 3600) { // 1-hour timeout
throw new \RuntimeException("Operation timed out");
}
sleep(5);
}
ssl_target_name_override or incorrect channel settings.$client = new LongRunningClient([
'credentials' => $credentials,
'grpcOptions' => [
'ssl_target_name_override' => 'your-service-account-id@your-project-id.iam.gserviceaccount.com
How can I help you explore Laravel packages today?