Cron job targeting a specific service returns status 400

256 views
Skip to first unread message

David Pomeroy

unread,
Dec 15, 2020, 12:04:59 PM12/15/20
to Google App Engine
Hi have a Python 3.8 Django app deployed to GAE standard with a working cron job that
executes an endpoint. The endpoint returns a 200 when everything goes well.
The app.yaml file does not define the service name and is the default service.
there is only one job in the cron.yaml and does not specify a target service. 

When I specify the service name in the app.yaml and specify the cron job to
target that same service, it executes the endpoint but returns a status 400
and doesn't execute the code in the endpoint function.

What is causing the status 400?


Elliott (Cloud Platform Support)

unread,
Dec 15, 2020, 2:37:34 PM12/15/20
to Google App Engine
Hello,

I understand that you have a problem with your application. Although Google Groups is meant for general discussions, we may still be able to guide you, at least a little bit.

To move things forward, would you be able to share some redacted logs about the 400 error? I was able to find this article with basic information.

Client error responses:
400 Bad Request
The server could not understand the request due to invalid syntax.

David Pomeroy

unread,
Dec 16, 2020, 10:01:38 AM12/16/20
to google-a...@googlegroups.com
Hi Elliott,
I have been able to narrow down the problem. There is a mismatch between the app URL with a defined service name and the cron job "host" used.
I can explain it as follows:
if you have an app.yaml file such as this:

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
#END OF APP.YAML
-------------------------
and a cron.yaml file that looks like this:
cron:
  - description: "Example cron job"
    url: /myjob/
    schedule: every 2 minutes
#END OF CRON.YAML

The App Engine cron job will make a request to the "resource": "/myjob/" and "host": "projectid.ew.r.appspot.com"
as seen in the protoPayLoad in the log file for the app on the App Engine.
This works fine as the app url is "projectid.ew.r.appspot.com"

If you give the app a service name such as service1 in the app.yaml file by adding:
service: service1

and then target that service in the cron.yaml file which looks like this:
cron:
  - description: "Example cron job"
    url: /myjob/
    schedule: every 2 minutes
    target: service1

Then, the app is deployed to service1-dot-projectid.ew.r.appspot.com
But the cron job uses a "resource":/myjob/ and "host": "service1.projectid.ew.r.appspot.com"

There is a mismatch between the app url and the host the cron job uses. The app has -dot- between the service and the project name
while the cron looks for service1.projectid.ew.r.appspot.com with just "." instead of "-dot-"

What is the best way to ensure the cron job uses the right "host" URL? Preferably "-dot-".

Regards
David 



--
You received this message because you are subscribed to a topic in the Google Groups "Google App Engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-appengine/YShxDzvhd28/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-appengi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/e66be266-5356-45f6-971b-520cdca4b48an%40googlegroups.com.

Elliott (Cloud Platform Support)

unread,
Dec 16, 2020, 2:55:28 PM12/16/20
to Google App Engine
Hello,

I’m glad you were able to narrow down the problem to a mismatch. I was able to find a document indicating that the target option is optional.

target:
Optional. The name of a specific service in your app. When target is specified, the Cron service targets the job request to that service in your app. The job requests are routed to the versions in the specified service that are configured for traffic.

There is also additional information about redirecting requests here. You’ll find information about routing with a dispatch file.

At this point, this is more of a technical discussion rather than a general one. I would strongly suggest that you engage the programming community for further advice at Stackoverflow.

Elliott (Cloud Platform Support)

unread,
Dec 16, 2020, 2:57:06 PM12/16/20
to Google App Engine
Hello,

From redirecting requests, this information would be helpful.

You can create a dispatch file to override App Engine's URL-based routing rules and define your own custom routing rules. With a dispatch file, you can send incoming requests to a specific service based on the path or host name in the request URL.

Reply all
Reply to author
Forward
0 new messages