googleapis / python-scheduler

This library has moved to https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-scheduler
Apache License 2.0
30 stars 11 forks source link

Creating jobs for non-default App Engine services is misleading #24

Closed aubreyyan closed 4 years ago

aubreyyan commented 4 years ago

Environment details

Name: google-cloud-scheduler
Version: 1.3.0
Summary: Cloud Scheduler API API client library
Home-page: https://github.com/googleapis/python-scheduler
Author: Google LLC
Author-email: googleapis-packages@google.com
License: Apache 2.0
Location: /mnt/c/Users/aubre/Projects/course_collect/venv/lib/python3.8/site-packages
Requires: google-api-core

Steps to reproduce

  1. Provision Cloud Scheduler API and deploy a non-default app to App Engine with an endpoint that can be accessed by Cloud Scheduler.

my app.yaml (note project id is not course-collect)

service: course-collect

runtime: python38

handlers:
  # This configures Google App Engine to serve the files in the app's static
  # directory.
- url: /static
  static_dir: static

  # This handler routes all requests not caught above to your main app. It is
  # required when static routes are defined, but can be omitted (along with
  # the entire handlers section) when there are no static files defined.
- url: /.*
  script: auto

Using python-scheduler wrapper of the Cloud Scheduler API, run something like this locally (authenticated of course):

app.py

import config
from google.cloud.scheduler_v1 import CloudSchedulerClient

scheduler_client = CloudSchedulerClient()
scheduler_path = scheduler_client.location_path(config.PROJECT_ID, config.REGION_ID)

scheduler_client.create_job(scheduler_path, {
    "name": f"{scheduler_path}/jobs/{config.CRON_NAME}",
    "app_engine_http_target": {"relative_uri": "/init", "app_engine_routing": {"service": "course-collect"}},
    "time_zone": config.TIME_ZONE,
    "schedule": '38 0 20 7 *'
})
  1. The job will be created and look something like this

image

  1. Note the Target uri, which as the user, I am not responsible for putting together App Engine : course-collect.course-gen.ue.r.appspot.com/init

  2. Now deploy the app with the non-default service name (it does not matter if you do this step before/after invoking your library)

$ gcloud app deploy app.yaml
Services to deploy:

descriptor:      [/mnt/c/Users/aubre/Projects/course_collect/app.yaml]
source:          [/mnt/c/Users/aubre/Projects/course_collect]
target project:  [course-gen]
target service:  [course-collect]
target version:  [20200720t002354]
target url:      [https://course-collect-dot-course-gen.ue.r.appspot.com]

Do you want to continue (Y/n)?  y

Beginning deployment of service [course-collect]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 3 files to Google Cloud Storage                ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
Updating service [course-collect]...done.
Setting traffic split for service [course-collect]...done.
Deployed service [course-collect] to [https://course-collect-dot-course-gen.ue.r.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s course-collect

To view your application in the web browser run:
  $ gcloud app browse -s course-collect

Google App Engine, by default, provisions non-default services with uri's in the syntax <custom_name>-dot-<default_name>..... and this is something I think is much harder to change and probably shouldn't. See Deploy multiple applications from same project

Therefore, the blame for trying to target App Engine : course-collect.course-gen.ue.r.appspot.com/init falls with this library, and it should take into account the "dot" syntax.

Code example

already shown

Notes

I recognized, on further inspection, that my flask deployment had some issues, and the request is actually going through, despite the discrepancies in target uri's which makes this no longer a bug, but still a nice-to-have for the uri's to match, For anyone not familiar, it would seem like this library is incompatible with non-default services

averikitsch commented 4 years ago

Thank you for the updated note. We will add a note to the official docs.