Starting and stopping Google App Engine backends

后端 未结 2 747
庸人自扰
庸人自扰 2021-01-03 05:51

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 s

2条回答
  •  被撕碎了的回忆
    2021-01-03 06:25

    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)
    

提交回复
热议问题