From 8d30359f1f32a53a08d5f4931e3d3dc95cb883a6 Mon Sep 17 00:00:00 2001 From: 0xBasically <0xBasic@yearn.finance> Date: Mon, 11 Sep 2023 13:34:46 -0400 Subject: [PATCH] wip --- scripts/s3.py | 35 +------------ yearn/helpers/telegram_monitoring.py | 77 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 yearn/helpers/telegram_monitoring.py diff --git a/scripts/s3.py b/scripts/s3.py index d9bd92570..f3cc6f40e 100644 --- a/scripts/s3.py +++ b/scripts/s3.py @@ -34,6 +34,7 @@ from yearn.v1.vaults import VaultV1 from yearn.v2.registry import Registry as RegistryV2 from yearn.v2.vaults import Vault as VaultV2 +from yearn.helpers import monitoring sentry_sdk.set_tag('script','s3') @@ -302,37 +303,5 @@ def _get_export_paths(suffix): return file_name, s3_path - - def with_monitoring(): - if os.getenv("DEBUG", None): - main() - return - from telegram.ext import Updater - - export_mode = _get_export_mode() - private_group = os.environ.get('TG_YFIREBOT_GROUP_INTERNAL') - public_group = os.environ.get('TG_YFIREBOT_GROUP_EXTERNAL') - updater = Updater(os.environ.get('TG_YFIREBOT')) - now = datetime.now() - message = f"`[{now}]`\n⚙️ {export_mode} Vaults API for {Network.name()} is updating..." - ping = updater.bot.send_message(chat_id=private_group, text=message, parse_mode="Markdown") - ping = ping.message_id - try: - main() - except Exception as error: - tb = traceback.format_exc() - now = datetime.now() - message = f"`[{now}]`\n🔥 {export_mode} Vaults API update for {Network.name()} failed!\n" - try: - detail_message = (message + f"```\n{tb}\n```")[:4000] - updater.bot.send_message(chat_id=private_group, text=detail_message, parse_mode="Markdown", reply_to_message_id=ping) - updater.bot.send_message(chat_id=public_group, text=detail_message, parse_mode="Markdown") - except BadRequest: - pass - #detail_message = message + f"{error.__class__.__name__}({error})" - #updater.bot.send_message(chat_id=private_group, text=detail_message, parse_mode="Markdown", reply_to_message_id=ping) - #updater.bot.send_message(chat_id=public_group, text=detail_message, parse_mode="Markdown") - raise error - message = f"✅ {export_mode} Vaults API update for {Network.name()} successful!" - updater.bot.send_message(chat_id=private_group, text=message, reply_to_message_id=ping) + monitoring("s3", _get_export_mode()) diff --git a/yearn/helpers/telegram_monitoring.py b/yearn/helpers/telegram_monitoring.py new file mode 100644 index 000000000..5f412ee93 --- /dev/null +++ b/yearn/helpers/telegram_monitoring.py @@ -0,0 +1,77 @@ +import os +import traceback +from datetime import datetime +from brownie import chain +from telegram.error import BadRequest +from y import Network + +def monitoring(script, export_mode_import): + if os.getenv("DEBUG", None): + main() + return + from telegram.ext import Updater + + export_mode = export_mode_import + private_group = os.environ.get('TG_YFIREBOT_GROUP_INTERNAL') + public_group = os.environ.get('TG_YFIREBOT_GROUP_EXTERNAL') + updater = Updater(os.environ.get('TG_YFIREBOT')) + now = datetime.now() + + if script == "s3": + message = f"`[{now}]`\n⚙️ #{export_mode} Vaults API for #{Network.name()} is updating..." + ping = updater.bot.send_message(chat_id=private_group, text=message, parse_mode="Markdown") + ping = ping.message_id + try: + main() + except Exception as error: + tb = traceback.format_exc() + now = datetime.now() + message = f"`[{now}]`\n🔥 #{export_mode} Vaults API update for #{Network.name()} failed!\n" + if chain.id == Network.Mainnet: + return + else: + try: + detail_message = (message + f"```\n{tb}\n```")[:4000] + updater.bot.send_message(chat_id=private_group, text=detail_message, parse_mode="Markdown", reply_to_message_id=ping) + updater.bot.send_message(chat_id=public_group, text=detail_message, parse_mode="Markdown") + except BadRequest: + detail_message = message + f"{error.__class__.__name__}({error})" + updater.bot.send_message(chat_id=private_group, text=detail_message, parse_mode="Markdown", reply_to_message_id=ping) + updater.bot.send_message(chat_id=public_group, text=detail_message, parse_mode="Markdown") + raise error + message = f"✅ #{export_mode} Vaults API update for #{Network.name()} successful!" + updater.bot.send_message(chat_id=private_group, text=message, reply_to_message_id=ping) + elif script == "revenues": + message = f"`[{now}]`\n⚙️ Revenues script for ZooTroop is collecting to send..." + ping = updater.bot.send_message(chat_id=private_group, text=message, parse_mode="Markdown") + ping = ping.message_id + try: + main() + except Exception as error: + tb = traceback.format_exc() + now = datetime.now() + message = f"`[{now}]`\n🔥 Revenues script for ZooTroop has failed!\n```\n{tb}\n```"[:4000] + updater.bot.send_message(chat_id=private_group, text=message, parse_mode="Markdown", reply_to_message_id=ping) + updater.bot.send_message(chat_id=public_group, text=message, parse_mode="Markdown") + raise error + message = f"✅ Revenues script for ZooTroop has sent!" + updater.bot.send_message(chat_id=private_group, text=message, reply_to_message_id=ping) + elif script == "curve_apy_previews": + message = f"`[{now}]`\n⚙️ Curve Previews API for {Network.name()} is updating..." + ping = updater.bot.send_message(chat_id=private_group, text=message, parse_mode="Markdown") + ping = ping.message_id + try: + main() + except Exception as error: + tb = traceback.format_exc() + now = datetime.now() + message = f"`[{now}]`\n🔥 Curve Previews API update for {Network.name()} failed!\n```\n{tb}\n```"[:4000] + updater.bot.send_message(chat_id=private_group, text=message, parse_mode="Markdown", reply_to_message_id=ping) + updater.bot.send_message(chat_id=public_group, text=message, parse_mode="Markdown") + raise error + message = f"✅ Curve Previews API update for {Network.name()} successful!" + updater.bot.send_message(chat_id=private_group, text=message, reply_to_message_id=ping) + else: + message = f"`[{now}]`\n⚙️ Error what script was run?" + ping = updater.bot.send_message(chat_id=private_group, text=message, parse_mode="Markdown") + ping = ping.message_id \ No newline at end of file