简体   繁体   English

在Google App Engine上以编程方式更改后端实例类

[英]Change backend instance class programmatically on Google App Engine

I am using backend instances for an Google App Engine project. 我正在使用Google App Engine项目的后端实例。 (Frontend instances can't handle requests longer than 60 seconds — I need longer time.) (前端实例无法处理超过60秒的请求 - 我需要更长的时间。)

I chose B4 instance type because sometimes the load is high. 我选择了B4实例类型,因为有时负载很高。 However, on certain times (let's say 2am to 7am) the load is so low that having an B4 instance is overkill. 但是,在某些时候(假设凌晨2点到早上7点),负载太低,以至于B4实例过度。

I want to make a cron job that changes the type of that instance to B2 on certain times and back to B4 on other times to save cost. 我想创建一个cron作业,在某些时候将该实例的类型更改为B2,并在其他时间返回到B4以节省成本。

However, looking at the Modules API , I could not find a way to do so. 但是,看看Modules API ,我找不到这样做的方法。

So how can I do so? 那我该怎么办呢?

Edit after getting an answer by Ramiel 在得到Ramiel的回答后编辑

In the end I used the Admin API as follows: 最后我使用了Admin API,如下所示:

# Construct the api client
cred = GoogleCredentials.get_application_default()
svc = discovery.build('appengine', 'v1', credentials=cred)
vapi = svc.apps().services().versions()

# get list of versions
o = vapi.list(appsId=app_identity.get_application_id(), servicesId=modules.get_current_module_name()).execute()

# PATCH all SERVING versions with the new instanceClass
for v in o['versions']:
    if v['servingStatus'] == 'SERVING':
        result = vapi.patch(
            appsId=app_identity.get_application_id(),
            servicesId=modules.get_current_module_name(),
            versionsId=v['id'],
            updateMask='instanceClass',
            body={
                'instanceClass': instanceClass
            }
        ).execute()

Checkout admin-api endpoints 结帐admin-api端点

https://cloud.google.com/appengine/docs/admin-api/reference/rest/v1/apps.services.versions/patch https://cloud.google.com/appengine/docs/admin-api/reference/rest/v1/apps.services.versions/patch

If this does not work for some reason, you can also deploy multiple versions of app with various instance/scaling settings and programmatically switch them with start_version from Modules API 如果由于某种原因这不起作用,您还可以使用各种实例/扩展设置部署多个版本的应用程序,并使用Modules API中的 start_version以编程方式切换它们


btw, if you switch to manual scaling you don't have 60s limit 顺便说一句,如果你切换到手动缩放,你没有60s的限制

This might not be what you're looking for, but it's a possible way of achieving what you want. 这可能不是您想要的,但它是实现您想要的可能方式。

Set up a system on container engine or something like that which would automatically pull the latest code from your repo, automatically adjust the instance type and automatically do a redeploy. 在容器引擎上设置一个系统或类似的系统,它会自动从您的仓库中提取最新代码,自动调整实例类型并自动进行重新部署。 You could make it deploy different instance types on different times. 您可以使它在不同的时间部署不同的实例类型。 A new redeploy would be needed for every change in instance class, but these could be fully automated in theory so this would be possible. 实例类中的每个更改都需要新的重新部署,但这些可以在理论上完全自动化,因此这是可能的。

Thought? 思想? Is this a possible solution for you? 这可能是您的解决方案吗?

任务队列可以运行10分钟,检查文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM