Skip to content

Commit

Permalink
add uptime pinging
Browse files Browse the repository at this point in the history
  • Loading branch information
Chlorophytus committed Aug 10, 2024
1 parent 86173d8 commit f424b71
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.25)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Project instantiation
project(chlorobot VERSION 1.1.8.52)
project(chlorobot VERSION 1.2.0.53)

# Fetch gRPC
option(USE_SYSTEM_GRPC "Use system installed gRPC" ON)
Expand Down
2 changes: 1 addition & 1 deletion chloresolve/chloresolve/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.8+rev52"
__version__ = "1.2.0+rev53"
30 changes: 30 additions & 0 deletions chloresolve/chloresolve/uptime_pinging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import asyncio
import logging
import requests


class UptimeTimer:
def __init__(self, interval_seconds: int, uri: str):
"""
Initializes a heartbeat timer per the specified seconds interval
"""
self.interval_seconds = interval_seconds
self.uri = uri
self.task = asyncio.ensure_future(self.heartbeat())
self.logger = logging.getLogger(__class__.__name__)

async def ping(self):
"""
Heartbeats to an UptimeRobot/etc. URL
"""
requests.get(self.uri)
self.logger.info("Sending heartbeat")
await asyncio.sleep(self.interval_seconds)
await self.ping()

def cancel(self):
"""
Cancels the heartbeat timer task.
"""
self.logger.info("Cancelling heartbeat task")
self.task.cancel()
7 changes: 6 additions & 1 deletion chloresolve/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import asyncio
import os
import chloresolve
from chloresolve import command, dispatch, strip
from chloresolve import command, dispatch, strip, uptime_pinging


class Chloresolver:
Expand Down Expand Up @@ -319,7 +319,12 @@ async def main() -> None:

logging.info("Connected to gRPC socket")

heartbeat = chloresolve.uptime_pinging.UptimeTimer(
int(os.environ["CHLOROBOT_HEALTHCHECK_INTERVAL"]),
os.environ["CHLOROBOT_HEALTHCHECK_URL"],
)
await resolver.listen()
heartbeat.cancel()


if __name__ == "__main__":
Expand Down

0 comments on commit f424b71

Please sign in to comment.