首页 > 解决方案 > 如何确保 CodeDeploy 在清除缓存时保持所有自动扩展组实例处于维护状态?

问题描述

我正在研究涉及以下内容的部署过程:

问题在于 CodeDeploy 事件不一定在同一秒发生,这可能会导致我们重新加载应用程序缓存的方式出现问题。

我们的应用程序应该仅在所有活动实例都处于维护状态时才清除缓存以避免收到新的 http 请求(否则它可能会抛出“前端控制器达到 100 次路由器匹配迭代”异常)。

我们曾考虑在所有实例的共享文件夹上使用锁定文件,但这听起来很老套。

任何关于如何确保所有实例都在维护中以进行清除缓存的想法将不胜感激!

标签: amazon-web-servicesmagentoautoscalingaws-code-deploy

解决方案


我对 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. 等待清除完成并退出维护模式

使用不同的生命周期挂钩,可能是ApplicationStartValidateService,等待您的清除完成并退出维护模式。只要清除时间少于 1 小时,您的实例就不应使生命周期挂钩超时。

如果您想通过 CodeDeploy 执行此操作,我会执行上述操作。当然,您可以在部署之外进行管理,并在您的实例上运行某种代码来管理所有这些。


推荐阅读