任务事件 - AWS IoT Core

任务事件

当任务为待处理、已完成或已取消状态时,或者设备在运行任务时报告成功或失败,AWS IoT Jobs 服务会发布到 MQTT 协议上的预留主题。设备或管理和监控应用程序可以通过订阅这些主题来跟踪任务的状态。

如何启用任务事件

来自 AWS IoT Jobs 服务的响应消息不会通过消息代理,也无法由其它客户端或规则订阅。要订阅与任务活动相关的消息,请使用 notifynotify-next 主题。有关任务主题的更多信息,请参阅 任务主题

要收到任务更新的通知,请使用 AWS Management Console 或 API 或 CLI启用这些任务。有关更多信息,请参阅 启用 AWS IoT 事件

任务事件的工作原理

由于取消或删除任务可能需要一些时间,所以发送两条消息来指示请求的开始和结束。例如,当取消请求开始时,系统会向 $aws/events/job/jobID/cancellation_in_progress 主题发送一条消息。当取消请求完成时,系统会向 $aws/events/job/jobID/canceled 主题发送一条消息。

任务删除请求的过程与之相似。管理和监控应用程序可以订阅这些主题来跟踪任务的状态。有关发布和订阅 MQTT 主题的更多信息,请参阅 设备通信协议

任务事件类型

不同的任务事件类型如下:

任务已完成/已取消/已删除

当任务已完成、已取消、已删除或正在取消或删除时,AWS IoT Jobs 服务会在 MQTT 主题上发布消息:

  • $aws/events/job/jobID/completed

  • $aws/events/job/jobID/canceled

  • $aws/events/job/jobID/deleted

  • $aws/events/job/jobID/cancellation_in_progress

  • $aws/events/job/jobID/deletion_in_progress

completed 消息包含以下示例负载:

{ "eventType": "JOB", "eventId": "7364ffd1-8b65-4824-85d5-6c14686c97c6", "timestamp": 1234567890, "operation": "completed", "jobId": "27450507-bf6f-4012-92af-bb8a1c8c4484", "status": "COMPLETED", "targetSelection": "SNAPSHOT|CONTINUOUS", "targets": [ "arn:aws:iot:us-east-1:123456789012:thing/a39f6f91-70cf-4bd2-a381-9c66df1a80d0", "arn:aws:iot:us-east-1:123456789012:thinggroup/2fc4c0a4-6e45-4525-a238-0fe8d3dd21bb" ], "description": "My Job Description", "completedAt": 1234567890123, "createdAt": 1234567890123, "lastUpdatedAt": 1234567890123, "jobProcessDetails": { "numberOfCanceledThings": 0, "numberOfRejectedThings": 0, "numberOfFailedThings": 0, "numberOfRemovedThings": 0, "numberOfSucceededThings": 3 } }

canceled 消息包含以下示例负载。

{ "eventType": "JOB", "eventId": "568d2ade-2e9c-46e6-a115-18afa1286b06", "timestamp": 1234567890, "operation": "canceled", "jobId": "4d2a531a-da2e-47bb-8b9e-ff5adcd53ef0", "status": "CANCELED", "targetSelection": "SNAPSHOT|CONTINUOUS", "targets": [ "arn:aws:iot:us-east-1:123456789012:thing/Thing0-947b9c0c-ff10-4a80-b4b3-cd33d0145a0f", "arn:aws:iot:us-east-1:123456789012:thinggroup/ThingGroup1-95c644d5-1621-41a6-9aa5-ad2de581d18f" ], "description": "My job description", "createdAt": 1234567890123, "lastUpdatedAt": 1234567890123 }

deleted 消息包含以下示例负载。

{ "eventType": "JOB", "eventId": "568d2ade-2e9c-46e6-a115-18afa1286b06", "timestamp": 1234567890, "operation": "deleted", "jobId": "4d2a531a-da2e-47bb-8b9e-ff5adcd53ef0", "status": "DELETED", "targetSelection": "SNAPSHOT|CONTINUOUS", "targets": [ "arn:aws:iot:us-east-1:123456789012:thing/Thing0-947b9c0c-ff10-4a80-b4b3-cd33d0145a0f", "arn:aws:iot:us-east-1:123456789012:thinggroup/ThingGroup1-95c644d5-1621-41a6-9aa5-ad2de581d18f" ], "description": "My job description", "createdAt": 1234567890123, "lastUpdatedAt": 1234567890123, "comment": "Comment for this operation" }

cancellation_in_progress 消息包含以下示例负载:

{ "eventType": "JOB", "eventId": "568d2ade-2e9c-46e6-a115-18afa1286b06", "timestamp": 1234567890, "operation": "cancellation_in_progress", "jobId": "4d2a531a-da2e-47bb-8b9e-ff5adcd53ef0", "status": "CANCELLATION_IN_PROGRESS", "targetSelection": "SNAPSHOT|CONTINUOUS", "targets": [ "arn:aws:iot:us-east-1:123456789012:thing/Thing0-947b9c0c-ff10-4a80-b4b3-cd33d0145a0f", "arn:aws:iot:us-east-1:123456789012:thinggroup/ThingGroup1-95c644d5-1621-41a6-9aa5-ad2de581d18f" ], "description": "My job description", "createdAt": 1234567890123, "lastUpdatedAt": 1234567890123, "comment": "Comment for this operation" }

deletion_in_progress 消息包含以下示例负载:

{ "eventType": "JOB", "eventId": "568d2ade-2e9c-46e6-a115-18afa1286b06", "timestamp": 1234567890, "operation": "deletion_in_progress", "jobId": "4d2a531a-da2e-47bb-8b9e-ff5adcd53ef0", "status": "DELETION_IN_PROGRESS", "targetSelection": "SNAPSHOT|CONTINUOUS", "targets": [ "arn:aws:iot:us-east-1:123456789012:thing/Thing0-947b9c0c-ff10-4a80-b4b3-cd33d0145a0f", "arn:aws:iot:us-east-1:123456789012:thinggroup/ThingGroup1-95c644d5-1621-41a6-9aa5-ad2de581d18f" ], "description": "My job description", "createdAt": 1234567890123, "lastUpdatedAt": 1234567890123, "comment": "Comment for this operation" }
任务执行最终状态

当设备将任务执行更新为最终状态时,AWS IoT Jobs 服务会发布消息:

  • $aws/events/jobExecution/jobID/succeeded

  • $aws/events/jobExecution/jobID/failed

  • $aws/events/jobExecution/jobID/rejected

  • $aws/events/jobExecution/jobID/canceled

  • $aws/events/jobExecution/jobID/timed_out

  • $aws/events/jobExecution/jobID/removed

  • $aws/events/jobExecution/jobID/deleted

消息包含以下示例负载:

{ "eventType": "JOB_EXECUTION", "eventId": "cca89fa5-8a7f-4ced-8c20-5e653afb3572", "timestamp": 1234567890, "operation": "succeeded|failed|rejected|canceled|removed|timed_out", "jobId": "154b39e5-60b0-48a4-9b73-f6f8dd032d27", "thingArn": "arn:aws:iot:us-east-1:123456789012:myThing/6d639fbc-8f85-4a90-924d-a2867f8366a7", "status": "SUCCEEDED|FAILED|REJECTED|CANCELED|REMOVED|TIMED_OUT", "statusDetails": { "key": "value" } }