Skip to content

Commit 2c534fe

Browse files
committed
feat: clean up stale games
1 parent 8217f56 commit 2c534fe

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

blackjack/game/blackjackgame.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import logging
3-
3+
from datetime import datetime
44
from enum import Enum
5+
56
import blackjack.errors as errors
67
from blackjack.game import Player, Dealer, Deck
78

@@ -17,6 +18,7 @@ def __init__(self, gametype=None, game_id=None, lang_id="en"):
1718
self.list_won = []
1819
self.list_tie = []
1920
self.list_lost = []
21+
self.datetime_started = datetime.now()
2022
self.bets_active = True
2123
self._current_player = 0
2224
self.players = []

blackjackbot/gamestore.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import logging
3+
from datetime import datetime, timedelta
34
from random import randint
45

56
from .errors.noactivegameexception import NoActiveGameException
@@ -82,3 +83,20 @@ def _game_stopped_callback(self, game):
8283
self.remove_game(self._game_dict[game.id])
8384

8485
self.logger.debug("Current games: {}".format(len(self._chat_dict)))
86+
87+
def cleanup_stale_games(self):
88+
stale_timeout_min = 10
89+
now = datetime.now()
90+
remove_chat_ids = []
91+
92+
for game_id, chat_id in self._game_dict.items():
93+
game = self.get_game(chat_id)
94+
95+
game_older_than_10_mins = game.datetime_started < (now - timedelta(minutes=stale_timeout_min))
96+
if game_older_than_10_mins:
97+
logging.info("Killing game with id {} because it's stale for > {} mins".format(game.id, stale_timeout_min))
98+
# TODO notify chat
99+
remove_chat_ids.append(chat_id)
100+
101+
for chat_id in remove_chat_ids:
102+
self.remove_game(chat_id)

bot.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import logging.handlers
55
import pathlib
66

7-
from telegram.ext import Updater
7+
from telegram.ext import Updater, JobQueue
88

99
import config
1010
from blackjackbot import handlers, error_handler
11+
from blackjackbot.gamestore import GameStore
1112

1213
logdir_path = pathlib.Path(__file__).parent.joinpath("logs").absolute()
1314
logfile_path = logdir_path.joinpath("bot.log")
@@ -32,6 +33,16 @@
3233

3334
updater.dispatcher.add_error_handler(error_handler)
3435

36+
37+
# Set up jobs
38+
def stale_game_cleaner(context):
39+
gs = GameStore()
40+
gs.cleanup_stale_games()
41+
42+
43+
updater.job_queue.run_repeating(callback=stale_game_cleaner, interval=300, first=300)
44+
45+
3546
if config.USE_WEBHOOK:
3647
updater.start_webhook(listen=config.WEBHOOK_IP, port=config.WEBHOOK_PORT, url_path=config.BOT_TOKEN, cert=config.CERTPATH, webhook_url=config.WEBHOOK_URL)
3748
updater.bot.set_webhook(config.WEBHOOK_URL)

0 commit comments

Comments
 (0)