Reference documentation and code samples for the gapic-common class Gapic::Operation.
A class used to wrap Google::Longrunning::Operation objects. This class provides helper methods to check the
status of an Operation
Inherits
Object
Examples
Checking Operation status
# this example assumes both api_client and operations_client# already exist.require"gapic/operation"op=Gapic::Operation.new(api_client.method_that_returns_longrunning_operation(),operations_client,Google::Example::ResultType,Google::Example::MetadataType)op.done?# => falseop.reload!# => operation completedifop.done?results=op.resultshandle_error(results)ifop.error?# Handle results.end
Working with callbacks
# this example assumes both api_client and operations_client# already exist.require"gapic/operation"op=Gapic::Operation.new(api_client.method_that_returns_longrunning_operation(),operations_client,Google::Example::ResultType,Google::Example::MetadataType)# Register a callback to be run when an operation is done.op.on_donedo|operation|raiseoperation.results.messageifoperation.error?# process(operation.results)# process(operation.metadata)end# Reload the operation running callbacks if operation completed.op.reload!# Or block until the operation completes, passing a block to be called# on completion.op.wait_until_done!do|operation|raiseoperation.results.messageifoperation.error?# process(operation.results)# process(operation.rmetadata)end
Methods
#cancel
defcancel(options:nil)
Cancels the operation.
Parameter
options (Gapic::CallOptions, Hash) (defaults to: nil) — The options for making the RPC call. A Hash can be provided to customize
the options object, using keys that match the arguments for CallOptions.new.
#delete
defdelete(options:nil)
Deletes the operation.
Parameter
options (Gapic::CallOptions, Hash) (defaults to: nil) — The options for making the RPC call. A Hash can be provided to customize
the options object, using keys that match the arguments for CallOptions.new.
#done?
defdone?()->Boolean
Checks if the operation is done. This does not send a new api call, but checks the result of the previous api
call to see if done.
Returns
(Boolean) — Whether the operation is done.
#error
deferror()->Google::Rpc::Status,nil
If the operation response is an error, the error will be returned, otherwise returns nil.
Returns
(Google::Rpc::Status, nil) — The error object.
#error?
deferror?()->Boolean
Checks if the operation is done and the result is an error. If the operation is not finished then this will
return false.
Returns
(Boolean) — Whether an error has been returned.
#grpc_op
defgrpc_op()->Google::Longrunning::Operation
Returns
(Google::Longrunning::Operation) — The wrapped grpc
operation object.
grpc_op (Google::Longrunning::Operation) — The inital longrunning operation.
client (Google::Longrunning::OperationsClient) — The client that handles the grpc operations.
result_type (Class) (defaults to: nil) — The class type to be unpacked from the result. If not provided the class type will be
looked up. Optional.
metadata_type (Class) (defaults to: nil) — The class type to be unpacked from the metadata. If not provided the class type
will be looked up. Optional.
options (Gapic::CallOptions) (defaults to: {}) — call options for this operation
Returns the metadata of an operation. If a type is provided, the metadata will be unpacked using the type
provided; returning nil if the metadata is not of the type provided. If the type is not of provided, the
metadata will be unpacked using the metadata's type_url if the type_url is found in the
Google::Protobuf::DescriptorPool.generated_pool. If the type cannot be found the raw metadata is retuned.
Returns
(Object, nil) — The metadata of the operation. Can be nil.
#name
defname()->String
Returns the server-assigned name of the operation, which is only unique within the same service that originally
returns it. If you use the default HTTP mapping, the name should have the format of operations/some/unique/name.
Returns
(String) — The name of the operation.
#on_done
defon_done(&block){|operation|...}
Registers a callback to be run when a refreshed operation is marked as done. If the operation has completed
prior to a call to this function the callback will be called instead of registered.
options (Gapic::CallOptions, Hash) (defaults to: nil) — The options for making the RPC call. A Hash can be provided to customize
the options object, using keys that match the arguments for CallOptions.new.
Returns
(Gapic::Operation) — Since this method changes internal state, it returns itself.
options (Gapic::CallOptions, Hash) (defaults to: nil) — The options for making the RPC call. A Hash can be provided to customize
the options object, using keys that match the arguments for CallOptions.new.
Returns
(Gapic::Operation) — Since this method changes internal state, it returns itself.
#response
defresponse()->Object,nil
If the operation is done, returns the response, otherwise returns nil.
Returns
(Object, nil) — The response of the operation.
#response?
defresponse?()->Boolean
Checks if the operation is done and the result is a response. If the operation is not finished then this will
return false.
Returns
(Boolean) — Whether a response has been returned.
#results
defresults()->Object,Google::Rpc::Status,nil
If the operation is done, returns the response. If the operation response is an error, the error will be
returned. Otherwise returns nil.
Returns
(Object, Google::Rpc::Status, nil) — The result of the operation. If it is an error a
Google::Rpc::Status will be returned.
Blocking method to wait until the operation has completed or the maximum timeout has been reached. Upon
completion, registered callbacks will be called, then - if a block is given - the block will be called.
Parameter
retry_policy (RetryPolicy, Hash, Proc) (defaults to: nil) — The policy for retry. A custom proc that takes the error as an
argument and blocks can also be provided.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# gapic-common - Class Gapic::Operation (v1.1.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.1.0 (latest)](/ruby/docs/reference/gapic-common/latest/Gapic-Operation)\n- [1.0.1](/ruby/docs/reference/gapic-common/1.0.1/Gapic-Operation)\n- [0.26.0](/ruby/docs/reference/gapic-common/0.26.0/Gapic-Operation)\n- [0.25.0](/ruby/docs/reference/gapic-common/0.25.0/Gapic-Operation)\n- [0.24.0](/ruby/docs/reference/gapic-common/0.24.0/Gapic-Operation) \nReference documentation and code samples for the gapic-common class Gapic::Operation.\n\nA class used to wrap Google::Longrunning::Operation objects. This class provides helper methods to check the\nstatus of an Operation \n\nInherits\n--------\n\n- Object\n\nExamples\n--------\n\nChecking Operation status \n\n```ruby\n# this example assumes both api_client and operations_client\n# already exist.\nrequire \"gapic/operation\"\n\nop = Gapic::Operation.new(\n api_client.method_that_returns_longrunning_operation(),\n operations_client,\n Google::Example::ResultType,\n Google::Example::MetadataType\n)\n\nop.done? # =\u003e false\nop.reload! # =\u003e operation completed\n\nif op.done?\n results = op.results\n handle_error(results) if op.error?\n # Handle results.\nend\n```\n\nWorking with callbacks \n\n```ruby\n# this example assumes both api_client and operations_client\n# already exist.\nrequire \"gapic/operation\"\n\nop = Gapic::Operation.new(\n api_client.method_that_returns_longrunning_operation(),\n operations_client,\n Google::Example::ResultType,\n Google::Example::MetadataType\n)\n\n# Register a callback to be run when an operation is done.\nop.on_done do |operation|\n raise operation.results.message if operation.error?\n # process(operation.results)\n # process(operation.metadata)\nend\n\n# Reload the operation running callbacks if operation completed.\nop.reload!\n\n# Or block until the operation completes, passing a block to be called\n# on completion.\nop.wait_until_done! do |operation|\n raise operation.results.message if operation.error?\n # process(operation.results)\n # process(operation.rmetadata)\nend\n```\n\nMethods\n-------\n\n### #cancel\n\n def cancel(options: nil)\n\nCancels the operation. \n**Parameter**\n\n- **options** ([Gapic::CallOptions](./Gapic-CallOptions), Hash) *(defaults to: nil)* --- The options for making the RPC call. A Hash can be provided to customize the options object, using keys that match the arguments for CallOptions.new.\n\n### #delete\n\n def delete(options: nil)\n\nDeletes the operation. \n**Parameter**\n\n- **options** ([Gapic::CallOptions](./Gapic-CallOptions), Hash) *(defaults to: nil)* --- The options for making the RPC call. A Hash can be provided to customize the options object, using keys that match the arguments for CallOptions.new.\n\n### #done?\n\n def done?() -\u003e Boolean\n\nChecks if the operation is done. This does not send a new api call, but checks the result of the previous api\ncall to see if done. \n**Returns**\n\n- (Boolean) --- Whether the operation is done.\n\n### #error\n\n def error() -\u003e Google::Rpc::Status, nil\n\nIf the operation response is an error, the error will be returned, otherwise returns nil. \n**Returns**\n\n- (Google::Rpc::Status, nil) --- The error object.\n\n### #error?\n\n def error?() -\u003e Boolean\n\nChecks if the operation is done and the result is an error. If the operation is not finished then this will\nreturn false. \n**Returns**\n\n- (Boolean) --- Whether an error has been returned.\n\n### #grpc_op\n\n def grpc_op() -\u003e Google::Longrunning::Operation\n\n**Returns**\n\n- (Google::Longrunning::Operation) --- The wrapped grpc operation object.\n\n### #initialize\n\n def initialize(grpc_op, client, result_type: nil, metadata_type: nil, options: {}) -\u003e Operation\n\n**Parameters**\n\n- **grpc_op** (Google::Longrunning::Operation) --- The inital longrunning operation.\n- **client** (Google::Longrunning::OperationsClient) --- The client that handles the grpc operations.\n- **result_type** (Class) *(defaults to: nil)* --- The class type to be unpacked from the result. If not provided the class type will be looked up. Optional.\n- **metadata_type** (Class) *(defaults to: nil)* --- The class type to be unpacked from the metadata. If not provided the class type will be looked up. Optional.\n- **options** ([Gapic::CallOptions](./Gapic-CallOptions)) *(defaults to: {})* --- call options for this operation \n**Returns**\n\n- ([Operation](./Gapic-Operation)) --- a new instance of Operation\n\n### #metadata\n\n def metadata() -\u003e Object, nil\n\nReturns the metadata of an operation. If a type is provided, the metadata will be unpacked using the type\nprovided; returning nil if the metadata is not of the type provided. If the type is not of provided, the\nmetadata will be unpacked using the metadata's type_url if the type_url is found in the\n`Google::Protobuf::DescriptorPool.generated_pool`. If the type cannot be found the raw metadata is retuned. \n**Returns**\n\n- (Object, nil) --- The metadata of the operation. Can be nil.\n\n### #name\n\n def name() -\u003e String\n\nReturns the server-assigned name of the operation, which is only unique within the same service that originally\nreturns it. If you use the default HTTP mapping, the name should have the format of operations/some/unique/name. \n**Returns**\n\n- (String) --- The name of the operation.\n\n### #on_done\n\n def on_done(&block) { |operation| ... }\n\nRegisters a callback to be run when a refreshed operation is marked as done. If the operation has completed\nprior to a call to this function the callback will be called instead of registered. \n**Yield Parameter**\n\n- **operation** ([Gapic::Operation](./Gapic-Operation)) --- Yields the finished Operation.\n\n### #refresh!\n\n def refresh!(options: nil) -\u003e Gapic::Operation\n\n**Alias Of** : [#reload!](./Gapic-Operation#Gapic__Operation_reload!_instance_) \nReloads the operation object. \n**Parameter**\n\n- **options** ([Gapic::CallOptions](./Gapic-CallOptions), Hash) *(defaults to: nil)* --- The options for making the RPC call. A Hash can be provided to customize the options object, using keys that match the arguments for CallOptions.new. \n**Returns**\n\n- ([Gapic::Operation](./Gapic-Operation)) --- Since this method changes internal state, it returns itself.\n\n### #reload!\n\n def reload!(options: nil) -\u003e Gapic::Operation\n\n**Aliases**\n\n- [#refresh!](./Gapic-Operation#Gapic__Operation_refresh!_instance_) \nReloads the operation object. \n**Parameter**\n\n- **options** ([Gapic::CallOptions](./Gapic-CallOptions), Hash) *(defaults to: nil)* --- The options for making the RPC call. A Hash can be provided to customize the options object, using keys that match the arguments for CallOptions.new. \n**Returns**\n\n- ([Gapic::Operation](./Gapic-Operation)) --- Since this method changes internal state, it returns itself.\n\n### #response\n\n def response() -\u003e Object, nil\n\nIf the operation is done, returns the response, otherwise returns nil. \n**Returns**\n\n- (Object, nil) --- The response of the operation.\n\n### #response?\n\n def response?() -\u003e Boolean\n\nChecks if the operation is done and the result is a response. If the operation is not finished then this will\nreturn false. \n**Returns**\n\n- (Boolean) --- Whether a response has been returned.\n\n### #results\n\n def results() -\u003e Object, Google::Rpc::Status, nil\n\nIf the operation is done, returns the response. If the operation response is an error, the error will be\nreturned. Otherwise returns nil. \n**Returns**\n\n- (Object, Google::Rpc::Status, nil) --- The result of the operation. If it is an error a `Google::Rpc::Status` will be returned.\n\n### #wait_until_done!\n\n def wait_until_done!(retry_policy: nil) { |operation| ... }\n\nBlocking method to wait until the operation has completed or the maximum timeout has been reached. Upon\ncompletion, registered callbacks will be called, then - if a block is given - the block will be called. \n**Parameter**\n\n- **retry_policy** ([RetryPolicy](./Gapic-Operation-RetryPolicy), Hash, Proc) *(defaults to: nil)* --- The policy for retry. A custom proc that takes the error as an argument and blocks can also be provided. \n**Yield Parameter**\n\n- **operation** ([Gapic::Operation](./Gapic-Operation)) --- Yields the finished Operation."]]