Skip to content

Commit

Permalink
feat: add test option to start script
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-ang committed Jan 9, 2025
1 parent 83fbd83 commit 3d01def
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ COPY . .

RUN ["chmod", "+x", "./src/main.sh"]

CMD ./src/main.sh
CMD ["./src/main.sh", "--test"]

# docker compose build
# docker compose up
Expand Down
31 changes: 1 addition & 30 deletions src/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
DRIVER: Optional[webdriver.Chrome] = None
WAIT: Optional[WebDriverWait] = None
BOT = None
timeout: Optional[pendulum.DateTime] = None


def setup_chrome_options(env: Environment) -> webdriver.ChromeOptions:
Expand Down Expand Up @@ -254,13 +253,6 @@ async def notify_users_and_unsubscribe(course: Course, msg: str, users: list[str
DB.unsubscribe(str(course), uid)


async def notify_admin(msg: str):
"""Sends a notification to Telegram feedback channel."""
await BOT.send_message(
FEEDBACK_CHANNEL_ID, msg, write_timeout=TimeConstants.TIMEOUT_SECONDS
)


def driver_alive() -> bool:
"""Check if the WebDriver is still operational."""
if DRIVER is None:
Expand All @@ -283,26 +275,5 @@ def init(context: ContextTypes.DEFAULT_TYPE):


async def run(context: ContextTypes.DEFAULT_TYPE):
"""Runs the finder every minute."""
global timeout

if (now := pendulum.now()).minute % 30 == 0:
print("Finder started at", now.to_rss_string())

init(context)

try:
await search_courses()
except TimeoutException:
if timeout is None:
timeout = pendulum.now()
else:
minutes_since_last_timeout = pendulum.now().diff(timeout).in_minutes()
if minutes_since_last_timeout % 20 == 0:
await notify_admin(
f"Finder timed out for {minutes_since_last_timeout} minutes."
)
except Exception as exc:
await notify_admin(repr(exc))
else:
timeout = None
await search_courses()
8 changes: 7 additions & 1 deletion src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)

python -m fastapi run $SCRIPT_DIR/server.py &

python $(dirname $SCRIPT_DIR)/test/bot.py &
if [ "$1" == "--test" ]; then
BOT_PROGRAM="$(dirname $SCRIPT_DIR)/test/bot.py"
else
BOT_PROGRAM="$SCRIPT_DIR/bot.py"
fi

python $BOT_PROGRAM &

wait -n

Expand Down
3 changes: 1 addition & 2 deletions test/bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import sys
import logging
import asyncio

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src import bot
Expand All @@ -14,4 +13,4 @@


if __name__ == "__main__":
asyncio.run(bot.main(Environment.DEV))
bot.main(Environment.DEV)

0 comments on commit 3d01def

Please sign in to comment.