Starting and stopping Google App Engine backends

2019-02-10 14:56发布

I read the Google App Engine backend docs, but I still can't understand how to start/stop backends (dynamic backends) from Python (using URLFetch, I guess).

Could someone give me a code example? The backend will not be on the application's default version.

2条回答
beautiful°
2楼-- · 2019-02-10 15:16

Use appcfg to start and stop backends. From the documentation:

appcfg backends <dir> start <backend>

Sets the backend state to START, allowing it to receive HTTP requests. Resident backends start immediately. Dynamic backends do not start until the first user request arrives. Has no effect if the backend was already started.

appcfg backends <dir> stop <backend>

Sets the backend state to STOP and shuts down any running instances. The stopped backend cannot receive HTTP requests; if it recevies a request, it returns a 404 response. This command has no effect if the backend was already stopped.

查看更多
Rolldiameter
3楼-- · 2019-02-10 15:22

It depends on what type of backend you are using, "Resident Backends" can't be shutdown from the production environment only via the Admin Console or command-line while "Dynamic Backends" are shutdown after sitting idle for a few minutes.

So if you use Dynamic Backends you can just send a request telling it to stop what it is doing and it will be shutdown automatically.

http://code.google.com/intl/iw/appengine/docs/python/config/backends.html#Types_of_Backends

Edit

Example of how this might work:

from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext.webapp.util import run_wsgi_app
import time

class ShutdownHandler(webapp.RequestHandler):
    def get(self):
        memcache.put('backendShutdown', True, 60)

class StartHandler(webapp.RequestHandler):
    def get(self):
        lastCheck = time.time()
        while True:
            if time.time() - 60 > lastCheck:
                stopBackend = memcache.get('backendShutdown')
                if stopBackend:
                    memcache.delete('backendShutdown')
                    break
                lastCheck = time.time()


if __name__ == '__main__':
    _handlers = [(r'/_ah/start', StartHandler),
                 (r'/backend/worker/shutdown', ShutdownHandler)] # somekind of handler for shutdown
    run_wsgi_app(webapp.WSGIApplication(_handlers))

And to stop it you would use:

from google.appengine.api import backends, urlfetch
url = backends.get_url('worker') + '/backend/worker/shutdown'
urlfetch.fetch(url)
查看更多
登录 后发表回答