我正在尝试使用 Azure SDK for javascript(@azure/arm-sql 版本 8.0.0)来复制 SQL 数据库,但我不想等到操作完成。相反,我想在创建请求后退出,稍后(假设每分钟)检查操作是否完成。SDK似乎通过功能支持我的用例:
getPollState()
获取一个 LROPollState 对象,该对象可用于在不同的上下文中轮询此 LRO(例如在不同的进程或不同的机器上)。如果 LRO 无法生成 LRO 轮询策略,那么这将返回 undefined。
和restoreLROPoller()
从提供的 LROPollState 中恢复 LROPoller。此方法可用于在不同的进程或机器上重新创建 LROPoller。
但是,文档没有指定如何通过网络对状态进行序列化/传输。我天真地尝试将其序列化为 JSON,但是当我运行下面的代码段时,出现以下错误:
TypeError: operationSpec.serializer.deserialize is not a function occurred in deserializing the responseBody - {"name":"b9952e45-85ff-41f8-b01c-83050c9d9a2c","status":"InProgress","startTime":"2021-10-14T15:38:01.59Z"}
这是一个简化的代码片段:
import { SqlManagementClient } from "@azure/arm-sql";
import { DefaultAzureCredential } from "@azure/identity";
import { LROPoller } from "@azure/ms-rest-azure-js";
const subscription = "<subscription ID>";
const rg = "myResourceGroup";
const server = "mySqlServer";
const dbName = "myDb";
const credentials = new DefaultAzureCredential();
const sqlClient = new SqlManagementClient(credentials, subscription);
const originalDb = await sqlClient.databases.get(rg, server, dbName);
const operation: LROPoller = await sqlClient.databases.beginCreateOrUpdate(rg, server, dbName + "_copy", {
location: "westeurope",
createMode: "Copy",
sourceDatabaseId: originalDb.id
});
const operationState = operation.getPollState()!;
const serializedState = JSON.stringify(operationState);
// The program would save the state somewhere and exit now, but let's make it simple.
const deserializedState = JSON.parse(serializedState);
const restoredOperation: LROPoller = sqlClient.restoreLROPoller(deserializedState);
// Following line throws the exception
// TypeError: operationSpec.serializer.deserialize is not a function occurred in deserializing the responseBody…
await restoredOperation.poll();
所以我的问题是如何以一种以后可以重用的方式保存操作状态。