gauravdott
gauravdott

Reputation: 420

Catching Exit signal from Cherrypy server

I have an application which is served via Cherrypy. I want to catch the exit signal from Cherrypy server in case of server restarts/failures so I can perform some db cleanups.

I am aware of a subscribe/unsubscribe methods to achieve this but am not sure about it. Can anybody help?

Upvotes: 2

Views: 410

Answers (1)

MookiBar
MookiBar

Reputation: 183

Sad no one properly answered it in two years. And the cherrypy documentation is terrible. Here it is, for posterity, so others can avoid the trouble...

Assuming your custom quit function (to clean DBs, etc) is my_quit_func....

import cherrypy
from cherrypy.process.plugins import SignalHandler

signalhandler = SignalHandler(cherrypy.engine)
signalhandler.handlers['SIGTERM'] = my_quit_func
signalhandler.handlers['SIGHUP'] = my_quit_func
signalhandler.handlers['SIGQUIT'] = my_quit_func
signalhandler.handlers['SIGINT'] = my_quit_func
signalhandler.subscribe()

....then, after this block, start the main cherrypy thread using cherrypy.quickstart or whatever's your preference.

Upvotes: 3

Related Questions