Add more reliable way to recover from crashes

This commit is contained in:
Deko
2024-03-10 15:01:32 +01:00
parent 231b83c8a7
commit 046c101f1c
3 changed files with 47 additions and 33 deletions

View File

@@ -1,6 +1,6 @@
import json
import os
import time
from datetime import datetime
from loguru import logger
from requests import HTTPError
@@ -11,14 +11,18 @@ from app.twitch_client import StreamInformation, TwitchClient
class Main:
is_live: bool = False
last_live_at = None
_previous_stream: StreamInformation = None
current_stream_id: str = ""
streams: dict[str, StreamInformation]
def __init__(self):
self.twitch_client = TwitchClient(streamer=os.environ["STREAMER_NAME"])
self.twitch_client.update_access_token()
self.profile_image = self.twitch_client.get_streamer_profile_picture()
self.discord_client = DiscordClient()
with open("streams.json", "r") as file:
saved_streams = json.load(file)
for stream_id, saved_stream in saved_streams:
self.streams[stream_id] = StreamInformation(**saved_stream)
def update_status(self):
try:
@@ -28,41 +32,46 @@ class Main:
if self.is_live:
logger.info("Streamer went offline.")
self.is_live = False
self.discord_client.finalize_information_on_discord(
streamer_name=self._previous_stream.user_name,
vod_url=self.twitch_client.get_vod(
user_id=self._previous_stream.user_id
),
)
stream = self.streams.get(self.current_stream_id)
if stream:
self.discord_client.finalize_information_on_discord(
streamer_name=stream.user_name,
vod_url=self.twitch_client.get_vod(
user_id=stream.user_id
),
)
return
if not self.is_live:
logger.info("Streamer went live.")
if (
self.last_live_at
and (datetime.now() - self.last_live_at).total_seconds()
< 300
):
existing_stream = self.streams.get(stream.id)
if existing_stream:
logger.info(
"Streamer was already live once in the past 5 minutes. "
"This is probably due to a crash, not sending the Discord ping."
"Recovering from crash, updating discord if possible."
)
self.last_live_at = datetime.now()
self.is_live = True
if existing_stream.discord_message_id:
self.discord_client.notification_msg_id = (
existing_stream.discord_message_id
)
self.discord_client.update_information_on_discord(
stream=stream, profile_image=self.profile_image
)
return
self.is_live = True
self.current_stream_id = stream.id
self.streams[stream.id] = stream
self.discord_client.send_information_to_discord(
stream=stream, profile_image=self.profile_image
)
self.last_live_at = datetime.now()
self.is_live = True
self.streams[
stream.id
].discord_message_id = self.discord_client.notification_msg_id
else:
logger.info("Streamer still live.")
self.discord_client.update_information_on_discord(
stream=stream, profile_image=self.profile_image
)
self._previous_stream = stream
except HTTPError as e:
logger.exception(e)
@@ -71,12 +80,12 @@ class Main:
return
self.is_live = False
self.discord_client.finalize_information_on_discord(
streamer_name=self._previous_stream.user_login,
vod_url=self.twitch_client.get_vod(
user_id=self._previous_stream.user_id
),
)
stream = self.streams.get(self.current_stream_id)
if stream:
self.discord_client.finalize_information_on_discord(
streamer_name=stream.user_name,
vod_url=self.twitch_client.get_vod(user_id=stream.user_id),
)
DELAY_SECONDS = 30.0
@@ -91,6 +100,8 @@ def entry() -> None:
try:
while True:
main.update_status()
with open("streams.json", "w") as file:
json.dump(main.streams, file)
time.sleep(
DELAY_SECONDS - ((time.time() - start_time) % DELAY_SECONDS)
)