Cleanup

Introduction to Application Cleanup

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.

To allow for application cleanup not only within your program, but also external plugins and extensions, there is the app.close() method that must be called after app.run() regardless of any exceptions or runtime errors.

When using the Python with operator, the App.close() method is automatically called when exiting the block.

Calling app.close() ensures that the pre_close and post_close framework hooks are run, allowing extensions/plugins/etc to cleanup after the program runs.

Exit Status and Error Codes

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 App.exit_code:

from cement import App

class MyApp(App):
    class Meta:
        label = 'myapp'
        exit_on_close = True

with MyApp() as app:
    try:
        app.run()
    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_close meta 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_close in production, but not for testing as in this example:

# create a separate class for unit tests

class MyTestApp(MyApp):
    class Meta:
        exit_on_close = False

The default exit code is 0. However, any uncaught exceptions will cause the application to exit with a code of 1 (error).

Running Cleanup Code

Any extension, or plugin, or even the application itself that has cleanup code should do so within the pre_close or post_close framework hooks to ensure that it gets run.

def my_cleanup_code(app):
    # do something to cleanup
    if os.path.exists('/path/to/some/dir'):
        os.remove('/path/to/some/dir')

def load(app):
    app.hook.register('pre_close', my_cleanup_code)

Last updated