Not sure whether I should ask this here or open a Github Issue…
Some sanic plugins/extensions rely on the “before_server_start” and/or “after_server_start” events to trigger initialization routines.
My Sanic-Plugins-Framwork and several of the SPF-built plugins used listeners on “before_server_start” for some start-up tasks.
After release of Sanic 19.6 and the introduction of ASGS mode, it came to my attention that some plugins broke, because ASGS-mode is designed to trigger the “after_server_start” event only. It didn’t seem like a big difference, so I changed SPF and Sanic-CORS to now run their startup routines on “after_server_start” to maintain compatibility going forward.
This has how opened the gates to some other problems. Namely, for those who use app.create_server()
with return_asyncio_server=True
rather than app.run()
.
See issues here: https://github.com/ashleysommer/sanicpluginsframework/issues/12
and here: https://github.com/ashleysommer/sanic-cors/issues/35
When you use app.create_server()
with return_asyncio_server=True
, it triggers the “before_server_start” but it never triggers “after_server_start”.
I believe the intention is that the sanic user is supposed to trigger the “after_server_start” event manually after running asyncio.ensure_future(server)
but before running loop.run_forever()
.
However I can’t see an easy way for a user to do that. The list of listener functions to call on the event is created by the _helper()
function, and stored in the server_settings
object. But when the server coroutine is handed back to the caller, there is no way of getting that server_settings
object.
Ideally we’d want to do something like this:
loop = asyncio.get_event_loop()
server = app.create_server(host=host, port=hostport, return_asyncio_server=True)
server_settings = #somehow get access to the server_settings created in create_server
task = asyncio.ensure_future(server, loop=loop)
trig = app.trigger_events(server_settings.get("after_start", []), loop)
trig_task = asyncio.ensure_future(trig, loop=loop)
loop.run_forever()
Does anyone have any comments or suggestions around how to correctly do this?