Skip to content

Commit

Permalink
feat: add feature flag (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMissx authored Jul 12, 2023
1 parent 4d027b4 commit 7a08b4a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
7 changes: 4 additions & 3 deletions anjani/core/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ async def start(self: "Anjani") -> None:

self.log.info("Bot is ready")

self.log.info("Catching up on missed events")
await self.dispatch_missed_events()
self.log.info("Finished catching up")
if not self.config.is_flag_active("disable_catchup"):
self.log.info("Catching up on missed events")
await self.dispatch_missed_events()
self.log.info("Finished catching up")

# Dispatch final late start event
await self.dispatch_event("started")
Expand Down
15 changes: 10 additions & 5 deletions anjani/plugins/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,21 @@ async def on_stop(self) -> None:
if not await file.exists():
return

data = await self.bot.client.invoke(GetState())
state = {}
if not self.bot.config.is_flag_active("disable_catchup"):
data = await self.bot.client.invoke(GetState())
state = {
"date": data.date,
"pts": data.pts,
"qts": data.qts,
"seq": data.seq,
}
await self.db.update_one(
{"_id": sha256(self.bot.config.BOT_TOKEN.encode()).hexdigest()},
{
"$set": {
"session": Binary(await file.read_bytes()),
"date": data.date,
"pts": data.pts,
"qts": data.qts,
"seq": data.seq,
**state,
}
},
upsert=True,
Expand Down
11 changes: 10 additions & 1 deletion anjani/util/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Config:

LOGIN_URL: Optional[str]
PLUGIN_FLAG: list[str]
FEATURE_FLAG: list[str]

IS_CI: bool

Expand All @@ -45,7 +46,12 @@ def __init__(self) -> None:
self.SW_API = getenv("SW_API")

self.LOGIN_URL = getenv("LOGIN_URL")
self.PLUGIN_FLAG = list(filter(None, [i.strip() for i in getenv("PLUGIN_FLAG", "").split(";")]))
self.PLUGIN_FLAG = list(
filter(None, [i.strip() for i in getenv("PLUGIN_FLAG", "").split(";")])
)
self.FEATURE_FLAG = list(
filter(None, [i.strip() for i in getenv("FEATURE_FLAG", "").split(";")])
)

self.IS_CI = getenv("IS_CI", "false").lower() == "true"

Expand All @@ -65,3 +71,6 @@ def __init__(self) -> None:

def is_plugin_disabled(self, name: str) -> bool:
return f'disable_{name.lower().replace(" ", "_")}_plugin' in self.PLUGIN_FLAG

def is_flag_active(self, name: str) -> bool:
return name in self.FEATURE_FLAG

0 comments on commit 7a08b4a

Please sign in to comment.