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

fix community issues #2707

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions octobot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ async def _apply_db_bot_config(logger, config, community_auth) -> bool:
auto_update=False
)
config.load_profiles()
except octobot.community.errors.MissingBotConfigError:
except octobot.community.errors.BotNotFoundError:
raise errors.RemoteConfigError(
f"COMMUNITY_BOT_ID env variable is required to apply bot config. "
f"COMMUNITY_BOT_ID={constants.COMMUNITY_BOT_ID}"
)
except octobot.community.errors.MissingBotConfigError:
raise
except Exception as err:
raise errors.RemoteConfigError(
f"Error when fetching {constants.COMMUNITY_BOT_ID} bot configuration: {err} ({err.__class__.__name__})"
Expand Down Expand Up @@ -201,6 +203,8 @@ async def _get_authenticated_community_if_possible(config, logger):
# When no tentacles or in cloud, fetch private data. Otherwise fetch it later on in bot init
fetch_private_data = not has_tentacles or constants.IS_CLOUD_ENV
await community_auth.async_init_account(fetch_private_data=fetch_private_data)
if not community_auth.is_logged_in():
logger.info("No authenticated community account")
except authentication.FailedAuthentication as err:
logger.error(f"Failed authentication when initializing community authenticator: {err}")
except Exception as err:
Expand All @@ -210,6 +214,10 @@ async def _get_authenticated_community_if_possible(config, logger):

async def _async_load_community_data(community_auth, config, logger, is_first_startup):
if constants.IS_CLOUD_ENV and is_first_startup:
if not community_auth.is_logged_in():
raise authentication.FailedAuthentication(
"Impossible to load community data without an authenticated user account"
)
# auto config
if constants.USE_FETCHED_BOT_CONFIG:
await _apply_db_bot_config(logger, config, community_auth)
Expand Down Expand Up @@ -356,7 +364,7 @@ def start_octobot(args):
commands.run_bot(bot, logger)

except errors.RemoteConfigError as err:
logger.exception(err, False, "Error when fetchig bot configuration: " + str(err))
logger.exception(err)
os._exit(-1)

except errors.ConfigError as err:
Expand Down
10 changes: 9 additions & 1 deletion octobot/community/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ def _create_client(self):
self.configuration_storage
)

async def _re_create_client(self):
self.supabase_client = self._create_client()
self.logger.debug(f"Refreshing user session")
self.supabase_client.event_loop = asyncio.get_event_loop()
await self.supabase_client.refresh_session()

async def ensure_async_loop(self):
# elements should be bound to the current loop
if not self.is_using_the_current_loop():
Expand All @@ -228,7 +234,7 @@ async def ensure_async_loop(self):
# changed event loop: restart client
await self.supabase_client.aclose()
self.user_account.flush()
self.supabase_client = self._create_client()
await self._re_create_client()

def is_using_the_current_loop(self):
return self.supabase_client.event_loop is None \
Expand Down Expand Up @@ -493,6 +499,8 @@ async def _initialize_account(self, minimal=False, fetch_private_data=True):
await self._init_community_data(fetch_private_data)
if self._community_feed and self._community_feed.has_registered_feed():
await self._ensure_init_community_feed()
except authentication.AuthenticationError as err:
self.logger.info(f"Login aborted: no authenticated session: {err}")
except authentication.UnavailableError as e:
self.logger.exception(e, True, f"Error when fetching community data, "
f"please check your internet connection.")
Expand Down
32 changes: 20 additions & 12 deletions octobot/community/supabase_backend/community_supabase_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,15 @@ async def restore_session(self):
if not self.is_signed_in():
raise authentication.FailedAuthentication()

async def refresh_session(self):
try:
await self.auth.refresh_session()
except gotrue.errors.AuthError as err:
raise authentication.AuthenticationError(err) from err

async def sign_in_with_otp_token(self, token):
self.event_loop = asyncio.get_event_loop()
# restore saved session in case otp token fails
# todo
saved_session = await self.auth._storage.get_item(self.auth._storage_key)
try:
url = f"{self.auth_url}/verify?token={token}&type=magiclink"
Expand Down Expand Up @@ -334,18 +339,21 @@ async def update_bot_orders(self, bot_id, formatted_orders) -> dict:

async def fetch_bot_tentacles_data_based_config(self, bot_id: str) -> commons_profiles.ProfileData:
if not bot_id:
raise errors.MissingBotConfigError(f"bot_id is '{bot_id}'")
raise errors.BotNotFoundError(f"bot_id is '{bot_id}'")
commons_logging.get_logger(__name__).debug(f"Fetching {bot_id} bot config")
bot_config = (await self.table("bots").select(
"id,"
"name, "
"bot_config:bot_configs!current_config_id("
"id, "
"options, "
"exchanges, "
"is_simulated"
")"
).eq(enums.BotKeys.ID.value, bot_id).execute()).data[0]
try:
bot_config = (await self.table("bots").select(
"id,"
"name, "
"bot_config:bot_configs!current_config_id!inner("
"id, "
"options, "
"exchanges, "
"is_simulated"
")"
).eq(enums.BotKeys.ID.value, bot_id).execute()).data[0]
except IndexError:
raise errors.MissingBotConfigError(f"No bot config for bot with bot_id: '{bot_id}'")
bot_name = bot_config["name"]
# generic options
profile_data = commons_profiles.ProfileData(
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
OctoBot-Commons==1.9.52
OctoBot-Trading==2.4.98
OctoBot-Evaluators==1.9.5
OctoBot-Tentacles-Manager==2.9.15
OctoBot-Tentacles-Manager==2.9.16
OctoBot-Services==1.6.17
OctoBot-Backtesting==1.9.7
Async-Channel==2.2.1
Expand Down
Loading