amazon-web-services - 如何确保 CodeDeploy 在清除缓存时保持所有自动扩展组实例处于维护状态?
问题描述
我正在研究涉及以下内容的部署过程:
- Gitlab 运行程序将 Magento 1.9 应用程序推送到 S3 存储桶
- Gitlab 运行程序使用 CodeDeploy 部署应用程序
- CodeDeploy 将应用程序部署在自动扩展组中的所有实例上
问题在于 CodeDeploy 事件不一定在同一秒发生,这可能会导致我们重新加载应用程序缓存的方式出现问题。
我们的应用程序应该仅在所有活动实例都处于维护状态时才清除缓存以避免收到新的 http 请求(否则它可能会抛出“前端控制器达到 100 次路由器匹配迭代”异常)。
我们曾考虑在所有实例的共享文件夹上使用锁定文件,但这听起来很老套。
任何关于如何确保所有实例都在维护中以进行清除缓存的想法将不胜感激!
解决方案
我对 Magento 了解不多,但听起来有几件事需要发生:
1. 将实例置于维护模式
在您的内部appspec
,您可能使用ApplicationStop
生命周期挂钩或其他挂钩将单个实例置于维护模式。
2. 等待所有实例进入维护模式并清除缓存
假设您在 中将主机置于维护模式ApplicationStop
,您可以使用另一个生命周期挂钩来等待所有实例进入维护模式。例如,您可以有一个脚本BeforeInstall
来检查是否所有实例都通过了ApplicationStop
,如果它们通过了,则开始清除(即,它是最后一个)。
这是一些伪代码:
# BeforeInstall or something other hook script
# 1. Get the instance details from CodeDeploy
instances = listDeploymentInstances().map(instanceId -> getDeploymentInstance(instanceId))
# 2. Check if all of the instances have completed ApplicationStop
for instance in instances {
applicationStopStatus = instance.instanceSummary.lifecycleEvents
.findFirst(event -> event.lifecycleEventName == "ApplicationStop")
.status
# If it's succeeded, we're good to go
if status == "Succeeded
purgeCache()
# If it's failed, you'll have to decide what should be done
else if status == "Failed"
# Abort the deployment or handle some other way
# If it's not completed, ignore it and let another instance kick off the purge
else
return
}
3. 等待清除完成并退出维护模式
使用不同的生命周期挂钩,可能是ApplicationStart
或ValidateService
,等待您的清除完成并退出维护模式。只要清除时间少于 1 小时,您的实例就不应使生命周期挂钩超时。
如果您想通过 CodeDeploy 执行此操作,我会执行上述操作。当然,您可以在部署之外进行管理,并在您的实例上运行某种代码来管理所有这些。
推荐阅读
- rest - 谁应该管理电子邮件号召性用语?前端还是后端?
- wordpress - 运行 git push -u origin master 命令后出现错误
- javascript - 如何使用 Nodejs 使用 findById 编辑和更新 MySQL 中的特定字段数据?
- javascript - twilio 是否为其 voip 通话提供音量控制?
- amazon-cognito - 如何将 Cognito(联合)身份 ID 映射到 Cognito 用户池 ID?
- python - 从 JSONS 列表中提取数据的问题
- c# - c#,winforms中如何将手机摄像头或其他摄像头连接到图片框?
- c++11 - libxl 库在 C++ 中的使用
- jquery - 为什么鼠标悬停和鼠标悬停时动画不会重复
- node.js - AWS lambda 调用 Facebook SDK 失败