Skip to content

Commit

Permalink
Merge pull request #564 from pomeloy/heartbeat
Browse files Browse the repository at this point in the history
Calculate heartbeat interval using sleeping_time
  • Loading branch information
codders authored Apr 13, 2024
2 parents f5de67a + a9f9b07 commit 298e201
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions flathunter/heartbeat.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
"""Providing heartbeat messages"""
from typing import Optional

from flathunter.abstract_notifier import Notifier
from flathunter.config import YamlConfig
from flathunter.logging import logger
from flathunter.notifiers import SenderApprise, SenderMattermost, SenderTelegram, SenderSlack
from flathunter.exceptions import HeartbeatException


def interval2counter(interval: str) -> Optional[int]:
def interval2counter(interval: str) -> int:
"""Transform the string interval to sleeper counter frequencies"""
if interval is None:
return None
return 0
if interval.lower() == 'hour':
return 6
return 3600
if interval.lower() == 'day':
return 144
return 86400
if interval.lower() == 'week':
return 1008
return 604800
raise HeartbeatException(
"No valid heartbeat instruction received - no heartbeat messages will be sent.")


class Heartbeat:
"""Will inform the user on regular intervals whether the bot is still alive"""
notifier: Notifier
interval: Optional[int]
interval: int

def __init__(self, config: YamlConfig, interval: str):
notifiers = config.notifiers()
Expand All @@ -41,7 +39,7 @@ def __init__(self, config: YamlConfig, interval: str):
else:
raise HeartbeatException("No notifier configured - check 'notifiers' config section!")

self.interval = interval2counter(interval)
self.interval = int(interval2counter(interval)/int(config.loop_period_seconds()))

def send_heartbeat(self, counter) -> int:
"""Send a new heartbeat message"""
Expand All @@ -52,7 +50,7 @@ def send_heartbeat(self, counter) -> int:
logger.info('Sending heartbeat message.')
self.notifier.notify(
'Beep Boop. This is a heartbeat message. '
'Your bot is searching actively for flats.'
'Your bot is actively searching for flats.'
)
counter = 0
return counter

0 comments on commit 298e201

Please sign in to comment.