Cloud SQL 实例未使用 Cloud Scheduler 停止

Cloud SQL instance not being stopped using Cloud Scheduler

Cloud SQL 实例在执行这些步骤后未使用 Cloud Schedule 停止:

  1. 创建一个应该触发云功能的pub/sub主题。
  2. 使用步骤 1 中已创建的主题部署云功能,并具有以下 python (3.8) 代码文件和要求。 (入口点:start_stop
  3. 创建一个云调度程序作业,以定期触发云功能,并使用在步骤 1 中创建的主题。 payload设置为start [CloudSQL instance name]stop [CloudSQL instance name]启动或停止指定实例

Main.py:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
import base64
from pprint import pprint

credentials = GoogleCredentials.get_application_default()
service = discovery.build('sqladmin', 'v1beta4', credentials=credentials, cache_discovery=False)
project = 'projectID'

def start_stop(event, context):
  print(event)
  pubsub_message = base64.b64decode(event['data']).decode('utf-8')
  print(pubsub_message)
  command, instance_name = pubsub_message.split(' ', 1)

  if command == 'start':
    start(instance_name)
  elif command == 'stop':
    stop(instance_name)
  else:
    print("unknown command " + command)

def start(instance_name):
  print("starting " + instance_name)
  patch(instance_name, "ALWAYS")

def stop(instance_name):
  print("stopping " + instance_name)
  patch(instance_name, "NEVER")

def patch(instance, activation_policy):
  request = service.instances().get(project=project, instance=instance)
  response = request.execute()
  j = response["settings"]
  settingsVersion = int(j["settingsVersion"])
  
  dbinstancebody = {
    "settings": {
      "settingsVersion": settingsVersion,
      "activationPolicy": activation_policy
    }
  }

  dbinstancebody = {
    "settings": {
      "settingsVersion": response["settings"]["settingsVersion"],
      "activationPolicy": activation_policy
    }
  }

  request = service.instances().update(
    project=project,
    instance=instance,
    body=dbinstancebody)
    
  response = request.execute()
  pprint(response)

Requirements.txt

google-api-python-client==1.10.0
google-auth-httplib2==0.0.4
google-auth==1.21.1
oauth2client==4.1.3

当我在停止调度程序中单击RUN NOW 按钮时,它已成功执行,但是当我导航到SQL 实例时,它并没有停止。 有人可以发现我缺少的东西吗?如果您需要更多详细信息,请告诉我,我刚刚开始使用 GCP。 :)

发送到 GCP 的正文中缺少层配置 api:

dbinstancebody = {
    "settings": {
      "settingsVersion": settingsVersion,
      "tier": "db-custom-2-13312"
      "activationPolicy": activation_policy
    }
}

如果单击已部署的函数,您将看到所有详细信息(以及图表),但最后还会显示 错误 。 (我的电脑不适合所有屏幕,这就是为什么我后来注意到这个部分)