3

I am using Google App Engine Standard Environment for my NodeJs App. Things were working fine until I register a route '/*' in my express app to catch all routes after my initial routes like '/', '/login' etc. . After deploying my app on GAE I got :

Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.

In my App Engine build logs :

enter image description here My app.yaml looks like: app.yaml

runtime: nodejs10
env: standard
service: default
health_check:
  enable_health_check: False
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

Quoting from official Docs of GAE: https://cloud.google.com/appengine/docs/standard/nodejs/how-instances-are-managed

Startup Each service instance is created in response to a start request, which is an empty HTTP GET request to /_ah/start. App Engine sends this request to bring an instance into existence; users cannot send a request to /_ah/start. Manual and basic scaling instances must respond to the start request before they can handle another request. The start request can be used for two purposes:

To start a program that runs indefinitely, without accepting further requests. To initialize an instance before it receives additional traffic. Manual, basic, and automatically scaling instances startup differently. When you start a manual scaling instance, App Engine immediately sends a /_ah/start request to each instance. When you start an instance of a basic scaling service, App Engine allows it to accept traffic, but the /_ah/start request is not sent to an instance until it receives its first user request. Multiple basic scaling instances are only started as necessary, in order to handle increased traffic. Automatically scaling instances do not receive any /_ah/start request.

When an instance responds to the /_ah/start request with an HTTP status code of 200–299 or 404, it is considered to have successfully started and can handle additional requests. Otherwise, App Engine terminates the instance. Manual scaling instances are restarted immediately, while basic scaling instances are restarted only when needed for serving traffic.

Am I missing something? Please help.

Thanks in Advance

nikhil024
  • 435
  • 5
  • 19

2 Answers2

10

I solved this problem by registering my last route as this:

app.get(/^(?!.*_ah).*$/,(req,res,next)=>{
})

as compared to my previous configuration.

app.get('/*',(req,res,next)=>{
    })

Here I used negate regex. Credit to this SO answer

Quoting from official GAE Docs

To initialize an instance before it receives additional traffic. Manual, basic, and automatically scaling instances startup differently. When you start a manual scaling instance, App Engine immediately sends a /_ah/start request to each instance.

What above route handler does is to catch all routes except the one starting with _ah that means _ah/start and _ah/stop will not be listened by my app as they are used by Google App engine for registering the app.

I hope this helps someone in the future.

Thanks

nikhil024
  • 435
  • 5
  • 19
1

You may want to go through the app.yaml Configuration File, you have several configs in there which aren't supported (some are only applicable to the flexible environment) or are simply redundant in your environment and have no effect on your app:

env: standard
service: default
health_check:
  enable_health_check: False
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

Out of them the resources ones might be relevant, you seem to desire 0.5G of memory for your app. For that you'd need an instance_class configuration, selecting at least a B4 class. Otherwise your app gets only a B2 (with 256M of memory). From Runtime and app elements:

Default: B2 is assigned if you do not specify an instance class along with the basic_scaling element or the manual_scaling element.

But that's just a suspicion. You may want to dig out the actual error log matching the 500 err for the /_ah/start request, hopefully it can pinpoint the root cause of the failure (plenty of possibilities, insufficient memory is just one of them). Without it it's just guesswork.

Side note - potentially of interest: How to tell if a Google App Engine documentation page applies to the standard or the flexible environment

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • I used the hack for negating regex in my route handler for _ah routes.Although i have made recommended changes to the configuration file app.yaml . – nikhil024 Dec 04 '18 at 05:55
  • CPU and Memory are valid parameters: https://cloud.google.com/appengine/docs/flexible/python/reference/app-yaml – Paul Kenjora Feb 19 '20 at 23:31