6. Defining long-running resource operations
If your service uses any long-running operations (LROs; see our guidelines for specifics), you will need to define a “status monitor” operation which can report the status of the operation.
Let’s say that we want to make our createOrUpdateWidget
and deleteWidget
operations long-running. Here’s how we can update our Widgets
interface to accomplish that:
import "@azure-tools/typespec-azure-core";
using Azure.Core;using Azure.Core.Traits;
alias ServiceTraits = SupportsRepeatableRequests & SupportsConditionalRequests & SupportsClientRequestId;
alias Operations = Azure.Core.ResourceOperations<ServiceTraits>;
interface Widgets { /** Get status of a Widget operation. This operation return status in status code. No response body is returned. */ getWidgetOperationStatus is Operations.GetResourceOperationStatus<Widget, never>;
/** Fetch a Widget by name. */ getWidget is Operations.ResourceRead<Widget>;
/** Create or replace a Widget asynchronously. */ @pollingOperation(Widgets.getWidgetOperationStatus) createOrUpdateWidget is Operations.LongRunningResourceCreateOrReplace<Widget>;
/** Delete a Widget asynchronously. */ @pollingOperation(Widgets.getWidgetOperationStatus) deleteWidget is Operations.LongRunningResourceDelete<Widget>;
/** List Widget resources. */ listWidgets is Operations.ResourceList<Widget>;}
- We change
createOrUpdateWidget
to useLongRunningResourceCreateOrReplace<Widget>
anddeleteWidget
to useLongRunningResourceDelete
. - We define the
getWidgetOperationStatus
operation based on theGetResourceOperationStatus
signature. This defines the operation status monitor as a child resource of theWidget
type so that it shows up under that resource in the route hierarchy. - We must add the
pollingOperation
decorator to both of the long-running operations and reference theWidgets.getWidgetOperationStatus
operation. This connects the long-running operations to their associated status monitor operation to make it easier for service clients to be generated.
NOTE: The status monitor operation must be defined earlier in the interface than the long-running operations that reference it otherwise TypeSpec will not be able to resolve the reference!
See considerations for service design for more information about LROs.