The concept of cleanup after application run time is nothing new, but often ignored or forgotten by developers. What happens during cleanup all depends on the application. This might mean closing and deleting temporary files, removing session data, or deleting a PID (Process ID) file for example.
When using the Python
App.close()method is automatically called when exiting the block.
You can optionally configure your application to automatically call
sys.exit()as well as set the status code that your application exists with via the meta option App.Meta.exit_on_close as well as setting
Example: Exit Status and Error Codes
from cement import App
label = 'myapp'
exit_on_close = True
with MyApp() as app:
except SomeException as e:
# do something with e
app.log.fatal('Caught Exception: %s' % e)
app.exit_code = 100
Note the use of the
App.exit_on_closemeta option. Cement will not call
sys.exit()unless this is set to
True. You will find that calling
sys.exit()in testing is very problematic, therefore you will likely want to enable
exit_on_closein production, but not for testing as in this example:
Example: Disable Exit on Close in Testing
# create a separate class for unit tests
exit_on_close = False
The default exit code is
0. However, any uncaught exceptions will cause the application to exit with a code of
Any extension, or plugin, or even the application itself that has cleanup code should do so within the
post_closeframework hooks to ensure that it gets run.
Example: Running Cleanup Code
# do something to cleanup