如何在 Google Cloud 平台中安排标准 App Engine 的启停?

huangapple go评论81阅读模式
英文:

How can schedule stop-start of standard app engine in gcp

问题

我正在尝试找出在Google Cloud中调度停止/启动最新版本的App Engine的方法,但未找到任何方法。我只找到了用于停止/启动特定版本的gcloud命令,但不知道如何调度gcloud命令。

我们使用手动扩展标准的App Engine,并希望每晚在特定时间停止它,然后在早上再次启动它。
如何最佳方式实现这一目标?

我的解决方案是为停止和启动App Engine创建单独的Python云函数,然后在特定时间调度这些函数。

https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables

谢谢。

英文:

I was trying to find out a way to schedule stop/start the latest version of app engine in google cloud, but couldn't find any way for that. I just found gcloud command for stopping/starting a specific version, but don't know how can I schedule gcloud command.

We have manual scaling standard app engine , and I want to stop it every night at specific time, and re-start it again in morning.
What is the best way to do that?

My implemented solution is separate python cloud function for stopping and starting the app engine, then scheduling those functions at the specific time

https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables

Thanks,

答案1

得分: 1

Here is the translated content:

最终,我可以在云函数中禁用/启用应用引擎,而不是停止特定版本。

1- 创建一个由 Pub/Sub 触发的云函数

import os
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

def disableEnable(event, context):
   credentials = GoogleCredentials.get_application_default()
   appengine = discovery.build('appengine', 'v1', credentials=credentials)
   apps = appengine.apps()
   APP_NAME = os.getenv("APP_NAME")
   messageServingStatus = event['message']

   # 获取目标应用的服务状态
   target_app = apps.get(appsId=APP_NAME).execute()
   current_status = target_app['servingStatus']

   # 如果需要,禁用目标应用
   if current_status == 'SERVING' and messageServingStatus == 'STOPPED':
     print(f'Attempting to disable app {APP_NAME}...')
     body = {'servingStatus': 'USER_DISABLED'}
   if current_status == 'USER_DISABLED' and messageServingStatus == 'SERVING':
     print(f'Attempting to enable app {APP_NAME}...')
     body = {'servingStatus': 'SERVING'}   
   apps.patch(appsId=APP_NAME, updateMask='serving_status', body=body).execute()

2- 创建两个具有特定频率的调度程序(用于启用/禁用应用引擎)
目标类型= Pub/Sub
选择已创建的 Pub/Sub 主题名称

英文:

Finally I could disable/serving the app engine in cloud function, instead of stopping a specific version.

1- Creating a cloud function which is triggered by pub/sub

import os
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials


def disableEnable(event, context):
   credentials = GoogleCredentials.get_application_default()
   appengine = discovery.build('appengine', 'v1', 
     credentials=credentials)
   apps = appengine.apps()
   APP_NAME = os.getenv("APP_NAME")
   messageServingStatus = event['message']

   # Get the target app's serving status
   target_app = apps.get(appsId=APP_NAME).execute()
   current_status = target_app['servingStatus']

   # Disable target app, if necessary
   if current_status == 'SERVING' and messageServingStatus == 'STOPPED':
     print(f'Attempting to disable app {APP_NAME}...')
     body = {'servingStatus': 'USER_DISABLED'}
   if current_status == 'USER_DISABLED' and messageServingStatus == 
     'SERVING':
     print(f'Attempting to enable app {APP_NAME}...')
     body = {'servingStatus': 'SERVING'}   
   apps.patch(appsId=APP_NAME, updateMask='serving_status', body=body).execute()

2-create two scheduler with specific frequency (FOR ENABLING/DISABLING appengine)
Target Type= Pub/Sub
Select created Pub/sub topic name

答案2

得分: 0

App Engine Standard是由Google管理的无服务器产品。它可以自动缩放(高达0)(在自动和基本模式下)。在手动模式下,缩放性不那么弹性,并且您可以设置所需的实例数量,它是静态的。

App Engine的强大之处在于其弹性。我只在自动模式下使用它。存在一些权衡,如从0扩展时的启动时间(也称为冷启动),以及实例的存活时间(您无法管理,一段时间没有请求处理后,实例会被卸载)。

但这也意味着您无需管理扩展和收缩。如果有流量,您将拥有实例。如果没有任何请求(夜晚),所有实例都会自动关闭,您不需要支付任何费用。


还有一些其他考虑,例如请求处理的最大持续时间,无法执行后台处理等等。

让我们更多地了解您的应用程序约束,以便与您一起审查App Engine是否最适合您。

英文:

App Engine Standard is a serverless product managed by Google. It scales up and down (up to 0) automatically (in automatic and basic mode). In manual mode, the scaling is not so elastic, and you set the number of instances that you want, it's flat.

The power of App Engine is that elasticity. I use it only in automatic mode. There are some trade off like the start up time when you scale from 0 (also called cold start), and the instance live duration (that you don't manage, after a while with no request handling, the instance is offloaded).

But it also means you have nothing to manage to scale up and down. If there is traffic, you have instances. If you have nothing (the night) all the instance are shut down automatically and you pay nothing.


There are some counterpart, like the max duration of request processing, the incapacity to perform background processing and stuff like that.

Let us now more about your app constraint to review with you if app engine is the best fit for you.

huangapple
  • 本文由 发表于 2023年4月20日 08:43:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定