Skip to content

Commit

Permalink
Work toward graceful shutdown
Browse files Browse the repository at this point in the history
But not really working because of #24
  • Loading branch information
genericmoniker committed Mar 13, 2021
1 parent 568d56d commit d513086
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions backend/src/mirror/event_bus.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json
import logging
from asyncio import Queue
from dataclasses import dataclass
from typing import * # pylint: disable=wildcard-import,unused-wildcard-import

_TERMINATE_SENTINEL = "__exit__"

_logger = logging.getLogger(__name__)


@dataclass
class Event:
Expand Down Expand Up @@ -39,6 +42,7 @@ def __init__(self) -> None:

async def shutdown(self) -> None:
for queue in self._listener_queues:
_logger.info("queuing terminate sentinel")
await queue.put(_TERMINATE_SENTINEL)

async def post(self, event: Event) -> None:
Expand Down Expand Up @@ -75,6 +79,7 @@ async def listen_for_events(self):
while True:
event = await queue.get()
if event == _TERMINATE_SENTINEL:
_logger.info("terminating event generator")
return
yield event.as_sse_dict()
finally:
Expand Down
6 changes: 6 additions & 0 deletions backend/src/mirror/plugins/activity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def start_plugin(context):
_state["task"] = task


def stop_plugin(context): # pylint: disable=unused-argument
task = _state.get("task")
if task:
task.cancel()


async def _refresh(context):
"""Get the step count data."""
data = {"stepsGoal": None, "steps": None}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/mirror/plugins/calendars/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .calendars import start_plugin
from .calendars import start_plugin, stop_plugin
from .configure import configure_plugin
6 changes: 6 additions & 0 deletions backend/src/mirror/plugins/calendars/calendars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ def start_plugin(context):
tasks.append(create_task(agenda.refresh(context), name="agenda.refresh"))
tasks.append(create_task(coming_up.refresh(context), name="coming_up.refresh"))
tasks.append(create_task(countdown.refresh(context), name="countdown.refresh"))


def stop_plugin(context): # pylint: disable=unused-argument
for task in _state.get("tasks"):
if task:
task.cancel()
6 changes: 6 additions & 0 deletions backend/src/mirror/plugins/weather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def start_plugin(context):
_state["task"] = task


def stop_plugin(context): # pylint: disable=unused-argument
task = _state.get("task")
if task:
task.cancel()


async def _refresh(context):
"""Get the weather data for the configured coordinates.
Expand Down
6 changes: 6 additions & 0 deletions backend/src/mirror/plugins/worth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def start_plugin(context):
_state["task"] = task


def stop_plugin(context): # pylint: disable=unused-argument
task = _state.get("task")
if task:
task.cancel()


async def _refresh(context):
"""Get the net worth data."""
while True:
Expand Down

0 comments on commit d513086

Please sign in to comment.