Skip to content

Commit

Permalink
Merge pull request #2 from EthDevOps/1-make-bot-more-resilient-agains…
Browse files Browse the repository at this point in the history
…t-server-errors

add retry method for channel backups
  • Loading branch information
elasticroentgen authored Mar 11, 2024
2 parents c6f086b + dc174b4 commit 21de82f
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
from datetime import datetime
import logging
import time
import requests
import boto3
import nextcord
Expand All @@ -19,6 +20,7 @@
S3_CLIENT = None
SIGNING_KEY = None


class ConfigurationError(Exception):
def __init__(self, message):
super().__init__(message)
Expand Down Expand Up @@ -102,13 +104,18 @@ def extract_message(message):
# process any attachments/files
for attach in message.attachments:
# Download file
resp = requests.get(attach.url, timeout=30)
resp.raise_for_status()
content = bytearray()
try:
resp = requests.get(attach.url, timeout=30)
resp.raise_for_status()
content = resp.content
except requests.exceptions.HTTPError:
print('Unable to download attachment')

attachments.append({
'type': attach.content_type,
'origin_name': attach.filename,
'content': base64.b64encode(resp.content).decode()
'content': base64.b64encode(content).decode()
})

backup_msg = {
Expand Down Expand Up @@ -559,27 +566,35 @@ async def on_ready():
generate_directory_file(client.get_all_channels(), datetime.now())

# Backup channels
max_attempts = 3
for channel in target_channels:
# Only interested in text channels
if not isinstance(channel, nextcord.TextChannel):
continue

print(f'Backing up Channel {channel.name} on {channel.guild.name}')

# Backup channels
last_msg_id = await get_last_message_id(channel)
new_last_msg_id = await backup_channel(channel, last_msg_id)
if new_last_msg_id is not None:
await set_last_message_id(channel, new_last_msg_id)

# Backup threads in channel
for thread in channel.threads:
print(f'Backing up Thread {thread.id} in Channel {channel.name} on {channel.guild.name}')

last_msg_id = await get_last_message_id(thread)
new_last_msg_id = await backup_channel(thread, last_msg_id)
if new_last_msg_id is not None:
await set_last_message_id(thread, new_last_msg_id)
for attempt in range(1, max_attempts + 1):
try:
# Only interested in text channels
if not isinstance(channel, nextcord.TextChannel):
continue

print(f'Backing up Channel {channel.name} on {channel.guild.name}')

# Backup channels
last_msg_id = await get_last_message_id(channel)
new_last_msg_id = await backup_channel(channel, last_msg_id)
if new_last_msg_id is not None:
await set_last_message_id(channel, new_last_msg_id)

# Backup threads in channel
for thread in channel.threads:
print(f'Backing up Thread {thread.id} in Channel {channel.name} on {channel.guild.name}')

last_msg_id = await get_last_message_id(thread)
new_last_msg_id = await backup_channel(thread, last_msg_id)
if new_last_msg_id is not None:
await set_last_message_id(thread, new_last_msg_id)
except Exception as e: # pylint: disable=W0718
wait_time = attempt * 5
print(f'\tAttempt {attempt} failed ({e}), retrying in {wait_time} seconds...')
time.sleep(wait_time)
print("\tMax retries reached. Function execution failed.")

# Quit when done
print('Notifying the heartbeat check...')
Expand Down

0 comments on commit 21de82f

Please sign in to comment.