Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add retry method for channel backups #2

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading