在清除缓存时,如何确保CodeDeploy使所有自动扩展组实例保持在维护状态?

时间:2018-09-18 12:42:01

标签: amazon-web-services magento autoscaling aws-code-deploy

我正在从事涉及以下方面的部署过程:

  • Gitlab运行程序将Magento 1.9应用程序推入S3存储桶
  • Gitlab运行程序使用CodeDeploy部署应用程序
  • CodeDeploy将应用程序部署在自动扩展组中所有实例上

与此相关的问题是CodeDeploy事件不一定在同一秒发生,并且可能导致我们重新加载应用程序缓存的方式出现问题。

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

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

任何关于如何确保所有实例都处于维护状态以实现清除缓存的想法将不胜感激!

1 个答案:

答案 0 :(得分:0)

我对Magento不太了解,但是听起来有些事情需要发生:

1。将实例置于维护模式

大概在appspec内,您使用了ApplicationStop生命周期挂钩或另一个挂钩将单个实例置于维护模式。

2。等待所有实例进入维护模式并清除缓存

假设您将主机置于ApplicationStop的维护模式,则可以使用另一个lifecycle hook等待所有实例进入维护模式。例如,您可以在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进行此操作,则可以执行上述操作。当然,您可以在部署之外进行管理,并在管理所有这些的实例上运行某种代码。