如何响应作业内设备上执行直接方法的失败?

How to response a fail on execute directmethod on device inside a job?

我的工作是在多个设备上调用直接方法。我在一台设备上强制失败。问题是:当我查询作业的统计信息时,所有设备都成功响应。

下一个文本是设备在按 jobid 过滤时的响应:

Query device job response
{
  "jobId": "DHCMD520c3696-6bbd-4973-b50e-b7db9695e004",
  "createdTime": "May 16, 2018 2:57:39 PM",
  "startTime": "May 16, 2018 2:57:38 PM",
  "lastUpdatedDateTime": "May 16, 2018 2:59:27 PM",
  "endTime": "May 16, 2018 3:02:38 PM",
  "jobType": "scheduleDeviceMethod",
  "jobStatus": "completed",
  "deviceId": "dev05162018125626383",
  "outcome": "{\"status\":404,\"payload\":\"Failed\"}",
  "outcomeResult": {
    "status": 404,
    "payload": "Failed"
  }
}
Query device job response
{
  "jobId": "DHCMD520c3696-6bbd-4973-b50e-b7db9695e004",
  "createdTime": "May 16, 2018 2:57:39 PM",
  "startTime": "May 16, 2018 2:57:38 PM",
  "lastUpdatedDateTime": "May 16, 2018 2:59:26 PM",
  "endTime": "May 16, 2018 3:02:38 PM",
  "jobType": "scheduleDeviceMethod",
  "jobStatus": "completed",
  "deviceId": "dev05162018125626386",
  "outcome": "{\"status\":200,\"payload\":\"Started firmware update, download file: https://firmwareservice.blob.core.windows.net/firmwares/F_V03.04.005_MONITOR800.zip\"}",
  "outcomeResult": {
    "status": 200,
    "payload": "Firmware update"
  }

第一个设备响应“400 失败”,但作业状态 "completed" 未失败。

下一个文本是按作业 ID 过滤时作业的响应:

{
  "jobId": "DHCMD520c3696-6bbd-4973-b50e-b7db9695e004",
  "queryCondition": "connectionState = 'Connected'",
  "createdTime": "2018-05-16T12:57:38.899+0000",
  "startTime": "2018-05-16T12:57:38.562+0000",
  "lastUpdatedDateTime": null,
  "endTime": "2018-05-16T12:59:50.764+0000",
  "maxExecutionTimeInSeconds": 300,
  "jobType": "scheduleDeviceMethod",
  "jobStatus": "completed",
  "cloudToDeviceMethod": "{\"methodName\":\"runcommand\",\"responseTimeoutInSeconds\":200,\"connectTimeoutInSeconds\":5,\"payload\":{\"method\":\"firmwareUpdate\",\"URL\":\"https://firmwareservice.blob.core.windows.net/firmwares/F_V03.04.005_MONITOR800.zip\"}}",
  "updateTwin": null,
  "failureReason": null,
  "statusMessage": null,
  "jobStatistics": {
    "deviceCount": 2,
    "failedCount": 0,
    "succeededCount": 2,
    "runningCount": 0,
    "pendingCount": 0
  },
  "deviceId": null,
  "parentJobId": null,
  "outcome": null,
  "outcomeResult": null,
  "error": null
}

当一个设备响应失败时,您可以观察jobStatistics响应2个成功的设备。

指示设备失败的作业的直接方法的响应应该是什么?

I force a fail on one device.

您将状态设置为“404”并将其与作业响应一起发送,对吗? (代码来自here。)如果我错了请纠正我。

string result = "'Failed'";
return Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 404));

虽然你给了一个404错误代码,但是设备收到了工作请求并成功给出了响应。设备和服务后端之间的通信无错误地完成。如果您需要发送更多信息,您可以将您在响应有效负载中设置的错误作为自定义部分进一步处理。不过是另外一个话题了。

如果要检查作业失败的情况,可以在收到作业请求后停止设备,然后再发送响应。这样它就无法发送响应。然后你可以看到 "GatewayTimeout" 错误代码,作业状态失败,如下所示:

   {
    "deviceId": "device1",
    "jobId": "41c4727f-e723-4a9e-a5f3-a390a915610d",
    "jobType": "scheduleDeviceMethod",
    "status": "failed",
    "startTimeUtc": "2018-05-17T06:49:35.2555449Z",
    "endTimeUtc": "2018-05-17T06:51:35.2555449Z",
    "createdDateTimeUtc": "2018-05-17T06:49:38.2561206Z",
    "lastUpdatedDateTimeUtc": "2018-05-17T06:49:43.2943762Z",
    "outcome": {
      "deviceMethodResponse": null
    },
    "error": {
      "code": "GatewayTimeout",
      "description": "Timed out waiting for the response from device."
    }
  }