Lifecycle Hooks
If you need to have some code that runs BEFORE the Q2 Webserver starts up or AFTER it
stops to do some necessary setup/teardown for example then there are a convenient set of
lifecycle hooks that you can utilize. These hooks will run with the q2 run
entrypoint.
To get started, just run the command q2 create_lifecycle_scripts
which will create
the necessary files in a lifecycle directory at the root of your project.
Startup
This hook lives in the lifecycle/startup.py
file and has one required function
called handle()
. This function takes one argument, and it is of the type
StartupData
. You can define whatever other
functions or import any code in this file that you need. This hook is perfect for doing
some extra setup that you may need before the webserver starts up. One simple example is
if you are using the dotenv library to define some environment variables in a .env
file.
Shutdown
This hook lives in the lifecycle/shutdown.py
file and has one required function
called handle()
. This function takes one argument, and it is of the type
ShutdownData
. This hook behaves very similarly
to startup
except it runs AFTER the webserver has stopped. If you need anything to
be cleaned up at this point then you can do that here.