I'm building an application where I want to intialize a few things and I am registering the while_serving and before_serving hooks in my create_app, i.e. the main prodedure that generates the application. Some of these hooks have dependencies on others, e.g. settings must be loaded & validated first, then the connection to the database is established and then I do some initialization based on the database. before_serving hooks are executed in order, so that is good.
However, because I want to register the database setup in a while_serving so that I can also easily and precisely release the database resources (registered in the same generator function) once the application shuts down, the FIFO registering of hooks isn't working anymore because all before_serving hooks are called first and only then are the while_serving hooks executed/iterated. For me, it would be ideal if they were instead executed in the order of their registration.
I haven't mentioned after_serving yet and that's because I don't actually use it currently, but if I were to I believe that the the same ordering should apply, i.e. "unload" while_serving hooks and after_serving hooks in the order they were registered.
The abstract example code that illustrates this problem is as follows:
app.before_serving(check_settings)
# Uses settings
app.while_serving(init_db)
# Uses database
app.before_serving(exporter.init_metrics) # fails because `init_db` is executed later
My current workaround, after splitting the init_db generator into two functions:
app.before_serving(check_settings)
# Uses settings
app.before_serving(init_db)
app.after_serving(uninit_db)
# Uses database
app.before_serving(exporter.init_metrics)
I'm building an application where I want to intialize a few things and I am registering the
while_servingandbefore_servinghooks in mycreate_app, i.e. the main prodedure that generates the application. Some of these hooks have dependencies on others, e.g. settings must be loaded & validated first, then the connection to the database is established and then I do some initialization based on the database.before_servinghooks are executed in order, so that is good.However, because I want to register the database setup in a
while_servingso that I can also easily and precisely release the database resources (registered in the same generator function) once the application shuts down, the FIFO registering of hooks isn't working anymore because allbefore_servinghooks are called first and only then are thewhile_servinghooks executed/iterated. For me, it would be ideal if they were instead executed in the order of their registration.I haven't mentioned
after_servingyet and that's because I don't actually use it currently, but if I were to I believe that the the same ordering should apply, i.e. "unload"while_servinghooks andafter_servinghooks in the order they were registered.The abstract example code that illustrates this problem is as follows:
My current workaround, after splitting the
init_dbgenerator into two functions: