LogoLogo
Official SiteAPI Reference
stable/3.0
stable/3.0
  • Cement Developer Guide
  • Release Information
    • What's New!
    • Upgrading
    • ChangeLog
    • Deprecations
  • Getting Started
    • Installation
    • Developer Tools
    • Framework Overview
    • Beginner Tutorial
      • Part 1: Creating Your First Project
      • Part 2: Adding Features
      • Part 3: Extending a Project
      • Part 4: Making Everything Legit
  • Core Foundation
    • Interfaces and Handlers
    • Hooks
    • Configuration Settings
    • Arguments
    • Logging
    • Controllers
    • Output Rendering
    • Caching
    • Mail Messaging
    • Framework Extensions
    • Application Plugins
    • Templating
  • Utilities
    • Filesystem
    • Shell
    • Miscellaneous
  • Extensions
    • Alarm
    • Argparse
    • Colorlog
    • ConfigParser
    • Daemon
    • Dummy
    • Generate
    • Jinja2
    • Json
    • Logging
    • Memcached
    • Mustache
    • Plugin
    • Print
    • Redis
    • Scrub
    • SMTP
    • Tabulate
    • Yaml
    • Watchdog
  • Additional Topics
    • Extending The App Object
    • Unit Testing
    • Cleanup
    • Signal Handling
    • Pipenv
    • Autocomplete
    • Profiling with cProfile
    • Debugging with VSCode
  • Environment Variables
  • Terminology
  • Contributing
  • Privacy Policy
Powered by GitBook
On this page
  • Introduction to Application Cleanup
  • Exit Status and Error Codes
  • Running Cleanup Code
  1. Additional Topics

Cleanup

PreviousUnit TestingNextSignal Handling

Last updated 6 years ago

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 method that must be called after 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 ensures that the and 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 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)
app.close()
app.run()
app.close()
App.Meta.exit_on_close
pre_close
post_close