ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

Amazon EventBridge Scheduler 定时调度场景技术规范与 SDK 实战指南

Amazon EventBridge Scheduler 定时调度场景技术规范与 SDK 实战指南 示例工程教程后端【免费下载链接】aws-doc-sdk-examplesWelcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.项目地址https://gitcode.com/gh_mirrors/aw/aws-doc-sdk-examples点击查看免费下载本指南围绕 aws-doc-sdk-examples 仓库中 EventBridge Scheduler 场景技术规范 展开完整讲解一个准备应用 → 创建一次性/周期性调度 → 清理资源的端到端功能场景如何用 CloudFormation 预置 SNS 主题与 IAM 角色作为调度目标如何通过CreateSchedule、CreateScheduleGroup、DeleteSchedule、DeleteScheduleGroup四个核心 API 完成调度编排。读完本文你将掌握该场景的架构设计、交互流程、错误处理约定与元数据规范并能结合仓库中 Python、Java、.NET 三套实现快速落地为可运行的演示程序。场景概述与前置准备场景在项目中的定位该规范对应的示例代码位于 scenarios/features/eventbridge_scheduler/README.md是一个Feature Scenario功能场景用于向 AWS 代码示例团队展示如何在多语言中一致地实现 EventBridge Scheduler 能力。场景目标用户为开发者与云架构师阅读对象包括各语言 SDK 的实现者与维护者。前置条件与 AWS 服务清单运行该场景前需要一个具备 Amazon EventBridge Scheduler 与 Amazon EventBridge 使用权限的 AWS 账户按规范要求可交互执行的终端环境需要用户多次输入名称与确认选项。场景依赖的 AWS 服务包括服务用途Amazon EventBridge Scheduler创建并管理一次性与周期性调度Amazon EventBridge调度事件路由的基础服务Amazon Simple Notification Service (SNS)作为调度事件的目标向订阅邮箱推送通知AWS CloudFormation一次性部署并销毁 SNS 主题与 IAM 角色等前置资源场景覆盖的 EventBridge Scheduler API场景直接覆盖四个调度 API 动作CreateScheduleCreateScheduleGroupDeleteScheduleDeleteScheduleGroup此外还会调用ListSchedules规范在错误处理表中提到Hello Service 场景无需处理其错误。架构设计与资源清单整体流程场景按四个阶段线性推进Python 实现 的run()方法清晰对应了这些阶段Preparing the application收集邮箱与栈名 → 部署 CloudFormation 栈 → 读取输出 ARN → 创建调度组Creating a one-time schedule创建带灵活时间窗口、执行后自动删除的一次性调度Creating a recurring schedule以指定速率创建持续运行 1 小时的周期性调度Clean up确认后删除调度、调度组并销毁 CloudFormation 栈。CloudFormation 栈资源场景通过 CloudFormation 一次性创建以下资源模板见 cfn_template.yamlSNS Topic用于接收调度事件的发布目标Email Subscription将订阅者邮箱绑定到主题用户必须点击确认邮件后才能收到事件通知IAM Role信任主体为scheduler.amazonaws.com允许调度服务 AssumeRole 并执行sns:Publish。模板还对外暴露两个输出Outputs供场景读取后作为后续 API 调用的参数Outputs: SNStopicARN: Description: SNS topic ARN Value: !Ref SchedulerSnsTopic RoleARN: Description: Scheduler role ARN Value: !GetAtt SchedulerRole.Arn其中 IAM 角色名称固定为example_scheduler_role其策略文档精确限定了sns:Publish到本主题 ARN最小权限原则贯穿整个模板设计。调度资源Schedule Group 与两类 Schedule场景创建三类 EventBridge Scheduler 资源资源说明Schedule Group组织调度的逻辑分组默认命名为scenario-schedules-group名称需唯一One-Time Schedule一次性调度带灵活时间窗口10 分钟触发后自动删除Recurring Schedule周期性调度从当前时间起持续运行 1 小时交互流程与参考输出阶段一准备应用Prepare the Application规范要求场景依次完成提示用户输入订阅邮箱用于 SNS 订阅提示用户输入CloudFormation 栈名并校验栈名是否已存在——若已存在需重新输入等待栈达到CREATE_COMPLETE状态若任一失败状态则提示用户并结束场景部署成功后读取并打印RoleARN与SNStopicARN两个输出值。参考输出节选Preparing the application... This example creates resources in a CloudFormation stack, including an SNS topic that will be subscribed to the EventBridge Scheduler events. You will need to confirm the subscription in order to receive event emails. Enter an email address to use for event subscriptions: testexample.com Enter a name for the AWS Cloud Formation Stack: teststack10 Deploying CloudFormation stack: teststack10 warn: CloudFormation stack teststack10 already exists. Please provide a unique name. stackAB ... Stack output RoleARN: arn:aws:iam::123456789123:role/example_scheduler_role Stack output SNStopicARN: arn:aws:sns:us-east-1:123456789123:stackAB-SchedulerSnsTopic-UORtMkZypo3x在 Python 实现 中栈部署通过 CloudFormation 的create_stack完成并显式传入Capabilities[CAPABILITY_NAMED_IAM]因为模板创建了命名 IAM 角色随后用stack_create_completewaiter 同步等待部署完成并从stack.outputs中按OutputKey提取两个 ARN若任一 ARN 缺失则记录错误并抛出ValueError。阶段二创建一次性调度One-Time Schedule规范约定提示用户输入调度名称该名称会出现在通知邮件中使用灵活时间窗口如 10 分钟并设置为执行完成后自动删除ActionAfterCompletion: DELETE调度表达式形如at(ISO-8601 时间)即触发一次后结束。参考输出Enter a name for the one-time schedule: one-time-test Creating a one-time schedule named one-time-test to send an initial event in 1 minute... Successfully created schedule one-time-test in schedule group scenario-schedules-group.Python 实现 的create_schedule方法展示了底层调用细节StartDate设为当前 UTC 时间EndDate为当前时间加 1 小时当use_flexible_time_windowTrue时设置FlexibleTimeWindow {Mode: FLEXIBLE, MaximumWindowInMinutes: 10}当delete_after_completionTrue时设置ActionAfterCompletion DELETE。调度输入即推送到 SNS 的消息体为One time scheduled event test from schedule 名称.。阶段三创建周期性调度Recurring Schedule规范约定提示用户输入调度名称与调度速率分钟调度从当前时间开始以rate(N minutes)表达式按指定速率持续运行 1 小时程序等待一段时间让调度真正触发然后提示用户确认删除。参考输出Creating a recurring schedule to send events for one hour... Enter a name for the recurring schedule: recurring-test Enter the desired schedule rate (in minutes): 2 Successfully created schedule recurring-test in schedule group scenario-schedules-group. ... Are you ready to delete the recurring-test schedule? (y/n) y Successfully deleted schedule with name recurring-test.对应地Java 实现 中也构造了at(...)/rate(...)形式的调度表达式并在 EventbridgeSchedulerActions.java 中通过FlexibleTimeWindowMode.FLEXIBLE/OFF与ActionAfterCompletion.DELETE/NONE实现同样的灵活时间窗口与完成策略。阶段四清理资源Clean Up规范要求提示用户确认后删除调度与调度组删除 CloudFormation 栈若删除失败则强制删除force-delete无论场景是否中途出错清理操作都应尽量执行若删除无法进行必须通知用户。参考输出Do you want to delete all resources created by this workflow? (y/n) y Successfully deleted schedule group scenario-schedules-group. CloudFormation stack teststack10 is being deleted. This may take a few minutes. ... CloudFormation stack teststack10 has been deleted. Amazon EventBridge Scheduler workflow completed.Python 的cleanup()按先删调度组、再销毁栈的顺序执行栈销毁同样使用stack_delete_completewaiter 同步等待而run()外层try/except中一旦发现异常就会调用demo.cleanup()这正对应规范中清理操作应在任何错误时尝试执行的要求。错误处理约定规范给出了各动作的异常处理矩阵actionErrorHandlingListSchedulesnone不处理Hello Service 场景无需CreateScheduleConflictException提示用户使用唯一名称CreateScheduleGroupConflictException提示用户使用唯一名称DeleteScheduleResourceNotFoundException提示用户该调度已删除DeleteScheduleGroupResourceNotFoundException提示用户该调度组已删除Python 的 SchedulerWrapper 严格按此约定实现四个方法均捕获ClientError对ConflictException记录名称冲突日志并重新抛出对ResourceNotFoundException记录资源已删除/未找到日志删除类方法不再抛出避免清理中断其余错误则记录后重新抛出。这一记录 选择性重抛的设计保证了场景出错后清理逻辑仍可继续。元数据Metadata规范场景为每个动作及整体场景在统一元数据文件scheduler_metadata.yaml中登记条目供 AWS 代码示例团队的自动检测工具使用action / scenariometadata keyListSchedulesscheduler_helloCreateSchedulescheduler_CreateScheduleCreateScheduleGroupscheduler_CreateScheduleGroupDeleteSchedulescheduler_DeleteScheduleDeleteScheduleGroupscheduler_DeleteScheduleGroupEventBridge Scheduler Scenarioscheduler_ScheduledEventsScenario代码中的snippet-start:[python.example_code.scheduler.CreateSchedule]等标记正是为这些元数据条目提供对应关系便于文档生成与代码检索。多语言实现与可运行性该场景已在三种语言中落地实现README 的 Implementations 部分 给出了入口Java 实现说明含HelloScheduler.java入门示例与EventbridgeSchedulerScenario场景类Python 实现说明含 scheduler_wrapper.py 动作封装、scheduler_scenario.py 场景编排与独立的 cfn_template.yaml.NET 实现说明SchedulerWrapper.cs/SchedulerWorkflow.cs对应同一架构。Python 实现还配套了完整测试集test_scenario_*.py覆盖准备应用、创建一次性调度、创建周期性调度与清理四个阶段可从侧面验证规范描述的各阶段行为与清理顺序。运行方式从仓库对应语言目录入手按 Python 运行说明 或 Java 运行说明 安装 SDK 依赖与配置 AWS 凭证运行场景主类按提示依次输入订阅邮箱、CloudFormation 栈名需唯一、一次性调度名、周期性调度名与速率分钟到邮箱中点击 SNS 订阅确认链接即可收到一次性与周期性两类事件邮件邮件内容包含自定义调度名结束时按提示确认删除调度、调度组与 CloudFormation 栈。注意一次性调度的灵活时间窗口为 10 分钟、周期性调度的持续窗口为 1 小时且场景强依赖 SNS 邮件订阅确认请预留足够的时间完成确认步骤部署与销毁均通过 waiter 同步等待耗时较长属正常现象。本文以 EventBridge Scheduler 场景技术规范 为骨架结合仓库内 Python / Java / .NET 三套实现与 CloudFormation 模板完整还原了从资源预置、一次性/周期性调度创建到清理销毁的全过程。开发者既可将其作为多语言移植的对照基准也可直接运行对应语言示例快速验证 EventBridge Scheduler 的调度能力与 SNS 事件通知链路。赞分享示例工程教程后端【免费下载链接】aws-doc-sdk-examplesWelcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.项目地址https://gitcode.com/gh_mirrors/aw/aws-doc-sdk-examples点击查看免费下载相关推荐使用 AWS SDK for Python (Boto3) 操作 Amazon EventBridge Scheduler从 Hello World 到完整定时任务场景使用 AWS SDK for Python Boto3 操作 Amazon EventBridge Scheduler从 Hello World 到完整定时任示例工程教程后端使用 AWS SDK for .NET 操作 Amazon EventBridge Scheduler计划组、一次性/重复计划与定时事件场景实战使用 AWS SDK for .NET 操作 Amazon EventBridge Scheduler计划组、一次性/重复计划与定时事件场景实战 本指南以 d示例工程教程后端使用 AWS SDK 构建 EventBridge Scheduler 定时事件场景一次性与周期性调度完整实践使用 AWS SDK 构建 EventBridge Scheduler 定时事件场景一次性与周期性调度完整实践 本指南以 aws doc sdk exampl示例工程教程后端上一篇Eigent聊天历史模型如何存储和管理用户的对话记录和上下文信息下一篇Kubernetes DaemonSetexample-voting-app节点级服务部署创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表