Adding graceful shutdown code from https://github.com/LOKE/kue