is there a way to register code to be run on server shutdown?

e.g. i want to flush and close a specific log file from within a WorkerTask.