Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions coco/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ def __init__(
self.redis_async = None

def _call_endpoints_on_start(self):
"""This is a short-lived Sanic worker.

It takes care of initialising redis for the endpoints
and handles "call_on_start" endpoints."""

logger.debug("init-endpoints worker start-up")

for endpoint in self.endpoints.values():
# Initialise request counter
self.redis_sync.incr(f"dropped_counter_{endpoint.name}", amount=0)
Expand Down Expand Up @@ -228,6 +235,8 @@ def _call_endpoints_on_start(self):
# TODO: raise log level in failure case?
logger.debug(f"Called /{endpoint.name} on start, result: {result}")

logger.debug("init-endpoints worker finished (exiting)")

def _start_server(self, sock: socket.socket | None = None):
"""Start a sanic server.

Expand All @@ -241,8 +250,10 @@ def _start_server(self, sock: socket.socket | None = None):
self.sanic_app.config.REQUEST_TIMEOUT = self.frontend_timeout
self.sanic_app.config.RESPONSE_TIMEOUT = self.frontend_timeout

def start_qworker(app):
"""Start the qworker."""
def start_workers(app):
"""Start the non-Sanic worker processes."""

# Start the qworker (the cocod back-end)
app.manager.manage(
"qworker",
worker.main_loop,
Expand All @@ -260,7 +271,17 @@ def start_qworker(app):
auto_start=True,
)

self._call_endpoints_on_start()
# Start a worker to initialise the endpoints and
# run the call-on-start actions
app.manager.manage(
"init-endpoints",
self._call_endpoints_on_start,
{},
# This is a short-lived process. Setting "tracked"
# to False tells Sanic that it's okay that it exits
# before the server exits.
tracked=False,
)

def signal_coco_shutdown(app):
"""Tell the qworker to shutdown via redis."""
Expand All @@ -273,7 +294,7 @@ def signal_coco_shutdown(app):
logger.error(f"queueing coco_shutdown in redis failed: {e}")

# Get Sanic to start/stop the qworker process when it starts/terminates
self.sanic_app.register_listener(start_qworker, "main_process_ready")
self.sanic_app.register_listener(start_workers, "main_process_ready")
self.sanic_app.register_listener(signal_coco_shutdown, "before_server_stop")

# Create the Redis connection pool, use sanic to start it so that it
Expand Down
Loading