Skip to content

Commit

Permalink
Improved error catching for loops
Browse files Browse the repository at this point in the history
clos #584
  • Loading branch information
StephanAkkerman committed Aug 3, 2024
1 parent 48f73bf commit 53b074a
Show file tree
Hide file tree
Showing 26 changed files with 94 additions and 106 deletions.
3 changes: 1 addition & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,8 @@ LISTENERS:
LOGGING_LEVEL: DEBUG

# This is only for debugging
DEBUG_MODE: False
DEBUG_MODE: True

# Add file names of the cogs that should be enabled
DEBUG_COGS:
- rsi_heatmap
- yield
7 changes: 2 additions & 5 deletions src/cogs/loops/assets.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
## > Imports
# > Standard libraries
from __future__ import annotations

import asyncio
import datetime

# > 3rd Party Dependencies
import discord
import numpy as np
import pandas as pd
from discord.ext import commands
from discord.ext.tasks import loop

# > Local dependencies
import util.vars
from util.cg_data import get_coin_info
from util.db import update_db
from util.disc_util import get_channel, get_guild, get_user
from util.disc_util import get_channel, get_guild, get_user, loop_error_catcher
from util.exchange_data import get_data
from util.formatting import format_change, format_embed_length
from util.vars import config, logger
Expand Down Expand Up @@ -66,6 +62,7 @@ async def usd_value(self, asset: str, exchange: str) -> tuple[float, float]:
return usd_val, change

@loop(hours=1)
@loop_error_catcher
async def assets(self) -> None:
"""
Only do this function at startup and if a new portfolio has been added.
Expand Down
8 changes: 2 additions & 6 deletions src/cogs/loops/earnings_overview.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import datetime

# > Discord dependencies
import discord

# > 3rd party dependencies
import pandas as pd

# Local dependencies
from discord.ext import commands
from discord.ext.tasks import loop

from util.disc_util import get_channel, get_tagged_users
from util.disc_util import get_channel, get_tagged_users, loop_error_catcher
from util.vars import config, data_sources, get_json_data, logger


Expand Down Expand Up @@ -111,6 +106,7 @@ def date_check(self) -> bool:
return False

@loop(hours=1)
@loop_error_catcher
async def earnings(self) -> None:
"""
Checks every hour if today is a sunday and if the market is closed.
Expand Down
9 changes: 3 additions & 6 deletions src/cogs/loops/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
import re
from io import StringIO

# > Discord dependencies
import discord

# 3rd party imports
import pandas as pd
import pytz
from bs4 import BeautifulSoup
from discord.ext import commands
from discord.ext.tasks import loop
from lxml.html import fromstring

from util.disc_util import get_channel

# Local dependencies
from util.disc_util import get_channel, loop_error_catcher
from util.vars import config, data_sources, get_json_data, post_json_data


Expand Down Expand Up @@ -117,6 +112,7 @@ async def get_events(self):
return pd.DataFrame(results)

@loop(hours=1)
@loop_error_catcher
async def post_events(self):
"""
Checks every hour if today is a friday and if the market is closed.
Expand Down Expand Up @@ -302,6 +298,7 @@ async def get_crypto_calendar(self) -> pd.DataFrame:
return df

@loop(hours=24)
@loop_error_catcher
async def post_crypto_events(self):
if self.crypto_channel is None:
self.crypto_channel = await get_channel(
Expand Down
3 changes: 2 additions & 1 deletion src/cogs/loops/funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from discord.ext import commands
from discord.ext.tasks import loop

from util.disc_util import get_channel
from util.disc_util import get_channel, loop_error_catcher

# Local dependencies
from util.vars import config, data_sources, get_json_data, logger
Expand All @@ -26,6 +26,7 @@ def __init__(self, bot: commands.Bot) -> None:
self.funding.start()

@loop(hours=4)
@loop_error_catcher
async def funding(self) -> None:
"""
This function gets the data from the funding API and posts it in the funding channel.
Expand Down
7 changes: 3 additions & 4 deletions src/cogs/loops/funding_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from tqdm import tqdm

from util.cg_data import get_top_vol_coins
from util.disc_util import get_channel
from util.disc_util import get_channel, loop_error_catcher
from util.vars import config, data_sources, logger

FIGURE_SIZE = (20, 10)
Expand All @@ -35,11 +35,10 @@ class Funding_heatmap(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
self.channel = None

if config["LOOPS"]["FUNDING_HEATMAP"]["ENABLED"]:
self.post_heatmap.start()
self.post_heatmap.start()

@loop(hours=24)
@loop_error_catcher
async def post_heatmap(self):
if self.channel is None:
self.channel = await get_channel(
Expand Down
10 changes: 3 additions & 7 deletions src/cogs/loops/gainers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import pandas as pd

# > 3rd party dependencies
import yahoo_fin.stock_info as si

# > Discord dependencies
from discord.ext import commands
from discord.ext.tasks import loop

from util.afterhours import afterHours
from util.disc_util import get_channel
from util.disc_util import get_channel, loop_error_catcher
from util.formatting import format_embed

# Local dependencies
from util.vars import config, get_json_data, logger


Expand Down Expand Up @@ -41,6 +35,7 @@ def __init__(self, bot: commands.Bot) -> None:
self.crypto.start()

@loop(hours=1)
@loop_error_catcher
async def crypto(self) -> None:
"""
This function will check the gainers and losers on Binance, using USDT as the base currency.
Expand Down Expand Up @@ -129,6 +124,7 @@ async def crypto(self) -> None:
await self.crypto_losers_channel.send(embed=e_losers)

@loop(hours=1)
@loop_error_catcher
async def stocks(self) -> None:
"""
This function uses the yahoo_fin.stock_info module to get the gainers for todays stocks.
Expand Down
5 changes: 4 additions & 1 deletion src/cogs/loops/ideas.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import util.vars
from util.db import update_db
from util.disc_util import get_channel, get_tagged_users
from util.disc_util import get_channel, get_tagged_users, loop_error_catcher
from util.vars import config, data_sources, get_json_data, logger


Expand Down Expand Up @@ -145,6 +145,7 @@ async def send_embed(self, df: pd.DataFrame, type: str) -> None:
update_db(util.vars.ideas_ids, "ideas_ids")

@loop(hours=24)
@loop_error_catcher
async def crypto_ideas(self) -> None:
"""
This function posts the crypto Trading View ideas.
Expand All @@ -164,6 +165,7 @@ async def crypto_ideas(self) -> None:
await self.send_embed(df, "crypto")

@loop(hours=24)
@loop_error_catcher
async def stock_ideas(self) -> None:
"""
This function posts the stocks Trading View ideas.
Expand All @@ -182,6 +184,7 @@ async def stock_ideas(self) -> None:
await self.send_embed(df, "stocks")

@loop(hours=24)
@loop_error_catcher
async def forex_ideas(self) -> None:
"""
This function posts the forex Trading View ideas.
Expand Down
9 changes: 4 additions & 5 deletions src/cogs/loops/index.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# Standard libraries
from __future__ import annotations

import datetime

# > Discord dependencies
import discord
from discord.ext import commands
from discord.ext.tasks import loop

from util.afterhours import afterHours
from util.disc_util import get_channel
from util.disc_util import get_channel, loop_error_catcher
from util.formatting import human_format
from util.tv_data import tv
from util.tv_symbols import crypto_indices, forex_indices, stock_indices

# Local dependencies
from util.vars import config, data_sources, get_json_data


Expand Down Expand Up @@ -66,6 +62,7 @@ async def get_feargread(self) -> tuple[int, str] | None:
return today, change

@loop(hours=1)
@loop_error_catcher
async def crypto(self) -> None:
"""
This function will get the current prices of crypto indices on TradingView and the Fear and Greed index.
Expand Down Expand Up @@ -152,6 +149,7 @@ async def crypto(self) -> None:
await self.crypto_channel.send(embed=e)

@loop(hours=1)
@loop_error_catcher
async def stocks(self) -> None:
"""
Posts the stock indices in the configured channel, only posts if the market is open.
Expand Down Expand Up @@ -231,6 +229,7 @@ async def stocks(self) -> None:
await self.stocks_channel.send(embed=e)

@loop(hours=1)
@loop_error_catcher
async def forex(self) -> None:
"""
Posts the forex indices in the configured channel, only posts if the market is open.
Expand Down
9 changes: 4 additions & 5 deletions src/cogs/loops/liquidations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from matplotlib import ticker
from tqdm import tqdm

from util.disc_util import get_channel
from util.disc_util import get_channel, loop_error_catcher
from util.formatting import human_format
from util.vars import config, data_sources, logger

Expand All @@ -33,12 +33,11 @@ class Liquidations(commands.Cog):

def __init__(self, bot: commands.Bot) -> None:
self.bot = bot

if config["LOOPS"]["LIQUIDATIONS"]["ENABLED"]:
self.channel = None
self.post_liquidations.start()
self.channel = None
self.post_liquidations.start()

@loop(hours=24)
@loop_error_catcher
async def post_liquidations(self):
"""
Copy chart like https://www.coinglass.com/LiquidationData
Expand Down
6 changes: 2 additions & 4 deletions src/cogs/loops/listings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import asyncio
import datetime

# > Discord dependencies
import discord
from discord.ext import commands
from discord.ext.tasks import loop

from util.disc_util import get_channel

# Local dependencies
from util.disc_util import get_channel, loop_error_catcher
from util.vars import config, data_sources, get_json_data


Expand Down Expand Up @@ -122,6 +119,7 @@ async def set_old_symbols(self) -> None:
self.new_listings.start()

@loop(hours=6)
@loop_error_catcher
async def new_listings(self) -> None:
"""
This function will be called every 6 hours to check for new listings.
Expand Down
12 changes: 4 additions & 8 deletions src/cogs/loops/losers.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
# Standard libraries
# > 3rd party dependencies
import yahoo_fin.stock_info as si

# > Discord dependencies
from discord.ext import commands
from discord.ext.tasks import loop

from util.afterhours import afterHours
from util.disc_util import get_channel
from util.disc_util import get_channel, loop_error_catcher
from util.formatting import format_embed

# Local dependencies
from util.vars import config, logger


class Losers(commands.Cog):
"""
This class contains the cog for posting the top crypto and stocks losers.
This class contains the cog for posting the top stocks losers.
The crypto losers can be found in gainers.py (because they are both done at the same time).
It can be enabled / disabled in the config under ["LOOPS"]["LOSERS"].
"""

Expand All @@ -28,6 +23,7 @@ def __init__(self, bot: commands.Bot) -> None:
self.losers.start()

@loop(hours=2)
@loop_error_catcher
async def losers(self) -> None:
"""
If the market is open, this function posts the top 50 losers for todays stocks.
Expand Down
11 changes: 5 additions & 6 deletions src/cogs/loops/nfts.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
## > Imports
# > Standard library
import datetime
import re

# > Discord dependencies
import discord
import numpy as np

# > Third party
import pandas as pd
from bs4 import BeautifulSoup
from discord.ext import commands
from discord.ext.tasks import loop

from util.cg_data import cg
from util.disc_util import get_channel
from util.disc_util import get_channel, loop_error_catcher
from util.formatting import format_change

# > Local
Expand Down Expand Up @@ -48,6 +43,7 @@ def __init__(self, bot: commands.Bot) -> None:
self.trending_nfts.start()

@loop(hours=1)
@loop_error_catcher
async def top_nfts(self):
if self.top_channel is None:
self.top_channel = await get_channel(
Expand Down Expand Up @@ -109,6 +105,7 @@ async def top_nfts(self):
await self.top_channel.send(embed=e)

@loop(hours=1)
@loop_error_catcher
async def trending_nfts(self):
if self.trending_channel is None:
self.trending_channel = await get_channel(
Expand Down Expand Up @@ -208,6 +205,7 @@ async def gc_trending(self):
await self.trending_channel.send(embed=e)

@loop(hours=1)
@loop_error_catcher
async def upcoming_nfts(self):
if self.upcoming_channel is None:
self.upcoming_channel = await get_channel(
Expand Down Expand Up @@ -257,6 +255,7 @@ async def upcoming_nfts(self):
await self.upcoming_channel.send(embed=e)

@loop(hours=1)
@loop_error_catcher
async def top_p2e(self):
if self.p2e_channel is None:
self.p2e_channel = await get_channel(
Expand Down
Loading

0 comments on commit 53b074a

Please sign in to comment.