2

I'm new-ish to Python and Pyramid so apologies if I'm trying to do the wrong thing here.

I'm currently running a Pyramid application inside of a Docker container, with the following entrypoint:

pipenv run pserve development.ini --reload

This serves my application correctly and I can then edit code directly inside the container. This all works fine. I then attempted to register this service to an instance of Netflix's Eureka Service Registry so I could then proxy to this service with a gateway (such as Netflix Zuul). I used Eureka's REST API to achieve this and again, this all worked fine.

However, when I go to shutdown the Pyramid service, I would like to send an additional HTTP request to Eureka to DELETE the registered service - This is ideal so I don't have to wait for expiry on Eureka and there will never be a window where Zuul might be proxying requests to a downed service.

The problem is I cannot reliably find a way to run a shutdown event in Pyramid. Basically, when i stop the Docker container, the service receives exit code 137 (which I believe is the result of a kill -9) and nothing ever happens. I've attempted using atexit as well as signal event such as SIGKILL, SIGTERM, SIGINT, etc and nothing ever happens. I've also tried running pserve without a --reload flag but that still doesn't work.

Is there anyway for me to reliably get this DELETE event to send right before the server and docker container shuts down?

This is the development.ini file I'm using:

[app:main]
use = egg:my-app
pyramid.reload_templates = true
pyramid.includes =
    pyramid_debugtoolbar
    pyramid_redis_sessions
    pyramid_tm

debugtoolbar.hosts = 0.0.0.0/0

sqlalchemy.url = mysql://root:root@mysql/keyblade

my-app.secret = secretkey

redis.sessions.secret = secretkey
redis.sessions.host = redis
redis.sessions.port = 6379

[server:main]
use = egg:waitress#main
listen = 0.0.0.0:8000

# Logging Configuration

[loggers]
keys = root, debug, sqlalchemy.engine.base.Engine

[logger_debug]
level = DEBUG
handlers =
qualname = debug

[handlers]
keys = console

[formatters] 
keys = generic

[logger_root]
level = INFO
handlers = console

[logger_sqlalchemy.engine.base.Engine]
level = INFO
handlers =
qualname = sqlalchemy.engine.base.Engine

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

2 Answers2

3

There is no shutdown protocol/api for a WSGI application (there technically isn't one for startup either despite people using/hoping that application-creation is close to the same time the server starts handling requests). You may be able to find a WSGI server that provides some hooks (for example gunicorn provides http://docs.gunicorn.org/en/stable/settings.html#worker-exit), but the better approach is to have your upstream handle your servers disappearing via health checks. Expecting that you'll be able to send a DELETE reliably when things go wrong is very unlikely to be a robust solution.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70
0

However, when I go to shutdown the Pyramid service, I would like to send an additional HTTP request to Eureka to DELETE the registered service - This is ideal so I don't have to wait for expiry on Eureka and there will never be a window where Zuul might be proxying requests to a downed service.

This is a web server specific and Pyramid cannot provide abstractions for it, as "your mileage may vary". Web server workers itself cannot know when they killed, as it is externally forced.

I would take an approach where you have an external process to monitor to web server and then perform clean up actions when it detects the web server is no longer running. The definition of no longer running could be "no a single process alive". Then you just have a background scheduled job (cron) to check for this condition. Or even better, have it on another monitoring instance that sits on a different server and can act in the situation in the server itself goes down.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435