Skip to content

Commit

Permalink
Fix db connection not closing due to unhandled exception
Browse files Browse the repository at this point in the history
Also: fix version numbering.
  • Loading branch information
499602D2 committed Aug 15, 2021
1 parent 8666e21 commit 3ac120c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# LaunchBot – a rocket launch information and notifications bot for Telegram πŸš€
LaunchBot keeps you up to date with what's going up, around the clock, since 2019. Over 350,000 notifications delivered to thousands of chats and groups!
LaunchBot keeps you up to date with what's going up, around the clock, since 2019. Over 450,000 notifications delivered to thousands of chats and groups!

Reachable as [@rocketrybot](https://t.me/rocketrybot) on Telegram.

Expand Down Expand Up @@ -105,7 +105,7 @@ Please note, that the above only applies on a per-bot basis. The creator of the
- βœ… allow users to set their own timezone


## 1.6 / major back-end changes (October 2020)
## 2.0 / major back-end changes (October 2020)

- βœ… upgrade to the LL2 API (LL1 closes at the end of October)

Expand Down Expand Up @@ -137,14 +137,10 @@ Please note, that the above only applies on a per-bot basis. The creator of the

- βœ… re-add statistics to all needed places

- add "show changelog" button under /statistics or /help

- load from a changelog.txt file?

- βœ… open-source LaunchBot ✨


## 1.7 / performance optimizations
## 2.1 / performance optimizations

- send notifications for launches entering into the middle of notification windows

Expand Down Expand Up @@ -173,4 +169,8 @@ Please note, that the above only applies on a per-bot basis. The creator of the
- enable the disabling of postpone notifications

- globally or on a per-launch basis

- add "show changelog" button under /statistics or /help

- load from a changelog.txt file?
</details>
9 changes: 8 additions & 1 deletion tg-launchbot/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,14 @@ def update_stats_db(stats_update: dict, db_path: str):
rd.hset('stats', stat, val)
else:
stats_cursor.execute(f"UPDATE stats SET {stat} = {stat} + {val}")
rd.hset('stats', stat, int(rd.hget('stats', stat)) + int(val))
try:
rd.hset('stats', stat, int(rd.hget('stats', stat)) + int(val))
except TypeError:
logging.exception(f'⚠️ Error updating stat for {stat} with ++{val}! stats_update: {stats_update}')
logging.warning('⚠️ Not commiting database change: closing connection and exiting...')

stats_conn.close()
return

# commit changes
stats_conn.commit()
Expand Down
28 changes: 12 additions & 16 deletions tg-launchbot/launchbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,7 @@ def timer_handle(update, context, command, chat, user):
context.bot.send_message(
chat,
'⚠️ *Please do not spam the bot.* Your user ID has been blocked and all commands by you will be ignored for an indefinite amount of time.',
parse_mode='Markdown')
parse_mode='Markdown', reply_to_message_id=update.message.message_id)
else:
run_time_ = int(time.time()) - STARTUP_TIME
logging.info(f'''
Expand Down Expand Up @@ -1652,16 +1652,16 @@ def notify(update, context):
[InlineKeyboardButton(text='βœ… Save and exit', callback_data='notify/done')]])

try:
context.bot.send_message(
chat, inspect.cleandoc(message_text), parse_mode='Markdown', reply_markup=keyboard)
context.bot.send_message(chat, inspect.cleandoc(message_text),
parse_mode='Markdown', reply_markup=keyboard)
except telegram.error.RetryAfter as error:
logging.exception(f'🚧 Got a telegram.error.retryAfter: sleeping for {error.retry_after} sec.')
retry_after(error.retry_after)
context.bot.send_message(
chat, inspect.cleandoc(message_text), parse_mode='Markdown', reply_markup=keyboard)
context.bot.send_message(chat, inspect.cleandoc(message_text),
parse_mode='Markdown', reply_markup=keyboard)

# update stats
update_stats_db(stats_update={'commands':1}, db_path=DATA_DIR)
update_stats_db(stats_update={'commands': 1}, db_path=DATA_DIR)


def feedback(update, context):
Expand Down Expand Up @@ -1719,17 +1719,13 @@ def feedback(update, context):

def generate_changelog():
changelog = f'''
πŸš€ *LaunchBot* v{VERSION} | Changelog
πŸš€ *LaunchBot* {VERSION} | Changelog
- Handle scenario where database connection failed to close due to redis error
- added Blue Origin
- tuned postpone notifications
- support local bot API servers
- show hold/launched status in /next
- implemented caching with redis
- tiny improvements everywhere
- massive amounts of bug-fixes
- Added changelogs to statistics
*More information on Github*
More information on Github
https://github.com/499602D2/tg-launchbot
'''

Expand Down Expand Up @@ -2870,7 +2866,7 @@ def apscheduler_event_listener(event):

if __name__ == '__main__':
# current version, set DATA_DIR
VERSION = '1.7.13'
VERSION = '2.1.15'
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
OLD_DATA_DIR = os.path.join(os.path.dirname(__file__), 'launchbot')

Expand Down

0 comments on commit 3ac120c

Please sign in to comment.