Skip to content

Commit

Permalink
Added startup and shutdown event notifications
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Costanzo <[email protected]>
  • Loading branch information
ianco committed Oct 27, 2021
1 parent cb4d07b commit 47d8a81
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions aries_cloudagent/core/conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from ..vc.ld_proofs.document_loader import DocumentLoader
from ..wallet.did_info import DIDInfo
from .dispatcher import Dispatcher
from .util import STARTUP_EVENT_TOPIC, SHUTDOWN_EVENT_TOPIC

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -363,8 +364,14 @@ async def start(self) -> None:
except Exception:
LOGGER.exception("Error accepting mediation invitation")

# notify protcols of startup status
await self.root_profile.notify(STARTUP_EVENT_TOPIC, {})

async def stop(self, timeout=1.0):
"""Stop the agent."""
# notify protcols that we are shutting down
await self.root_profile.notify(SHUTDOWN_EVENT_TOPIC, {})

shutdown = TaskQueue()
if self.dispatcher:
shutdown.run(self.dispatcher.complete())
Expand Down
10 changes: 10 additions & 0 deletions aries_cloudagent/core/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Core utilities and constants."""

import re


CORE_EVENT_PREFIX = "acapy::CORE::"
STARTUP_EVENT_TOPIC = CORE_EVENT_PREFIX + "STARTUP"
STARTUP_EVENT_PATTERN = re.compile(f"^{STARTUP_EVENT_TOPIC}?$")
SHUTDOWN_EVENT_TOPIC = CORE_EVENT_PREFIX + "SHUTDOWN"
SHUTDOWN_EVENT_PATTERN = re.compile(f"^{SHUTDOWN_EVENT_TOPIC}?$")
19 changes: 19 additions & 0 deletions aries_cloudagent/protocols/endorse_transaction/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

from ....admin.request_context import AdminRequestContext
from ....connections.models.conn_record import ConnRecord
from ....core.event_bus import Event, EventBus
from ....core.profile import Profile
from ....core.util import STARTUP_EVENT_PATTERN, SHUTDOWN_EVENT_PATTERN
from ....indy.issuer import IndyIssuerError
from ....ledger.error import LedgerError
from ....messaging.models.base import BaseModelError
Expand Down Expand Up @@ -694,6 +697,22 @@ async def transaction_write(request: web.BaseRequest):
return web.json_response(tx_completed.serialize())


def register_events(event_bus: EventBus):
"""Subscribe to any events we need to support."""
event_bus.subscribe(STARTUP_EVENT_PATTERN, on_startup_event)
event_bus.subscribe(SHUTDOWN_EVENT_PATTERN, on_shutdown_event)


async def on_startup_event(profile: Profile, event: Event):
"""Handle any events we need to support."""
print(">>> Received STARTUP event")


async def on_shutdown_event(profile: Profile, event: Event):
"""Handle any events we need to support."""
print(">>> Received SHUTDOWN event")


async def register(app: web.Application):
"""Register routes."""

Expand Down

0 comments on commit 47d8a81

Please sign in to comment.