Skip to content

Commit

Permalink
hive-service: Publish service status via hive-messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
gbenson committed Sep 24, 2024
1 parent a324f82 commit b57885f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libs/service/hive/service/restart_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,8 @@ def touch(filename: str):
os.utime(filename)
except FileNotFoundError:
open(filename, "wb").close()

def report_via_channel(self, channel, **kwargs):
"""Report this startup via a :class:`hive.messaging.Channel`.
"""
return self.status.report_via_channel(channel, **kwargs)
29 changes: 29 additions & 0 deletions libs/service/hive/service/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,32 @@ class ServiceStatus:
messages: list[str] = field(default_factory=list)
timestamp: datetime = field(default_factory=datetime.now)
_uuid: UUID = field(default_factory=uuid4)

def _as_dict(self) -> dict[str]:
report = {
"meta": {
"timestamp": str(self.timestamp),
"uuid": str(self._uuid),
"type": "service_status_report",
},
"service": self.service,
"condition": self.condition.name,
}
if self.messages:
report["messages"] = self.messages[:]
return report

def report_via_channel(
self,
channel,
*,
routing_key: str = "service.status",
mandatory: bool = False,
):
"""Publish this report via a :class:`hive.messaging.Channel`.
"""
return channel.publish_event(
message=self._as_dict(),
routing_key=routing_key,
mandatory=mandatory,
)

0 comments on commit b57885f

Please sign in to comment.