全部产品
云市场

消息通知

更新时间:2019-11-12 11:22:53

1. 背景

批量计算使用 MNS 来实现消息通知。用户负责主题 (Topic) 的创建、管理和订阅,并在创建集群或提交作业时指定相关配置。批量计算会依据配置向指定的主题推送消息,用户可在 MNS 控制台配置 URL、队列、邮件和短信四种方式获取消息通知。

2. 说明

2.1. 支持的事件

目前批量计算支持两类事件,您可以按需配置:

  • 集群事件:
    • 集群已删除:OnClusterDeleted
    • 实例已创建:OnInstanceCreated
    • 实例已运行:OnInstanceActive
  • 作业事件:
    • 作业等待中:OnJobWaiting
    • 作业运行中:OnJobRunning
    • 作业已停止:OnJobStopped
    • 作业已结束:OnJobFinished
    • 作业已失败:OnJobFailed
    • 任务等待中:OnTaskWaiting
    • 任务运行中:OnTaskRunning
    • 任务停止中:OnTaskStopped
    • 任务已结束:OnTaskFinished
    • 任务已失败:OnTaskFailed
    • 实例等待中:OnInstanceWaiting
    • 实例运行中:OnInstanceRunning
    • 实例已停止:OnInstanceStopped
    • 实例已结束:OnInstanceFinished
    • 实例已失败:OnInstanceFailed
    • 优先级改变:OnPriorityChange

2.2. 消息格式

适用于 OnClusterDeleted

  1. {
  2. "Category": "Cluster",
  3. "ClusterId": "cls-hr2rbl6qt5gki7392b8001",
  4. "ClusterName": "test-cluster",
  5. "CreationTime": "2016-11-01T15:25:02.837728Z",
  6. "State": "Deleted",
  7. "Event": "OnClusterDeleted"
  8. }

适用于 OnInstanceCreatedOnInstanceActive

  1. {
  2. "Category": "Cluster",
  3. "ClusterId": "cls-hr2rbl6qt5gki7392b8001",
  4. "Group": "group1",
  5. "InstanceId": "i-wz9c51g2s6zsrtnqi4fa",
  6. "InnerIpAddress": "10.45.168.26",
  7. "Hints": "",
  8. "State": "Starting",
  9. "CreationTime": "2016-11-01T15:25:02.837728Z",
  10. "Event": "OnInstanceCreated"
  11. }

适用于 OnJobWaitingOnJobRunningOnJobStoppedOnJobFinishedOnJobFailed

  1. {
  2. "Category": "Job",
  3. "JobId": "job-0000000058524720000077E900007257",
  4. "JobName": "test-job",
  5. "Event": "OnJobWaiting",
  6. "State": "Waiting",
  7. "CreationTime": "2016-11-01T15:25:02.837728Z",
  8. "StartTime": "2016-11-01T15:35:02.837728Z",
  9. "EndTime": "2016-11-01T15:45:02.837728Z",
  10. "Message": ""
  11. }

适用于 OnTaskWaitingOnTaskRunningOnTaskStoppedOnTaskFinishedOnTaskFailed

  1. {
  2. "Category": "Job",
  3. "JobId": "job-0000000058524720000077E900007257",
  4. "Task": "Echo",
  5. "Event": "OnTaskWaiting",
  6. "State": "Waiting",
  7. "StartTime": "2016-11-01T15:35:02.837728Z",
  8. "EndTime": "2016-11-01T15:45:02.837728Z"
  9. }

适用于 OnInstanceWaitingOnInstanceRunningOnInstanceStoppedOnInstanceFinishedOnInstanceFailed

  1. {
  2. "Category": "Job",
  3. "JobId": "job-0000000058524720000077E900007257",
  4. "Task": "Echo",
  5. "InstanceId": "0",
  6. "Event": "OnInstanceWaiting",
  7. "State": "Waiting",
  8. "StartTime": "2016-11-01T15:35:02.837728Z",
  9. "EndTime": "2016-11-01T15:45:02.837728Z",
  10. "RetryCount": "0",
  11. "Progress": "0",
  12. "StdoutRedirectPath": "oss://bucket/tests/a44c0ad8-a003-11e6-8f8e-fefec0a80e06/logs/stderr.job-0000000058184218000008150000000D.task.0",
  13. "StderrRedirectPath": "oss://bucket/tests/a44c0ad8-a003-11e6-8f8e-fefec0a80e06/logs/stdout.job-0000000058184218000008150000000D.task.0",
  14. "ExitCode": "0",
  15. "ErrorCode": "",
  16. "ErrorMessage": "",
  17. "Detail": ""
  18. }

适用于 OnPriorityChange

  1. {
  2. "Category": "Job",
  3. "JobId": "job-0000000058524720000077E900007257",
  4. "JobName": "test-job",
  5. "Event": "OnPriorityChange",
  6. "State": "Waiting",
  7. "CreationTime": "2016-11-01T15:45:02.837728Z",
  8. "StartTime": "2016-11-01T15:55:02.837728Z",
  9. "EndTime": "2016-11-01T15:57:02.837728Z",
  10. "Message": "",
  11. "From": "10",
  12. "To": "20"
  13. }

3. 使用

使用消息队列必须同时指定 MNS 主题名称 (Name)、MNS 私网 Endpoint (Endpoint) 和关注的事件 (Events)。

以下我们将展示通过 SDK 展示如何使用 MNS 消息队列,其中 MNS 主题名称为test,MNS私网 Endpoint 为http://xxx.mns.cn-beijing.aliyuncs.com/,关注的事件为OnClusterDeletedOnInstanceCreatedOnInstanceActive

3.1. SDK

  1. from batchcompute.resources import (
  2. ClusterDescription, Notification, Topic
  3. )
  4. cluster_desc = ClusterDescription()
  5. notification = Notification()
  6. topic = Topic()
  7. topic.Name = 'test'
  8. topic.Endpoint = 'http://xxx.mns.cn-beijing.aliyuncs.com/'
  9. topic.Events = ["OnClusterDeleted", "OnInstanceCreated", "OnInstanceActive"]
  10. notification.Topic = topic
  11. cluster_desc.Notification = notification