Skip to content

Commit cf90f1c

Browse files
authored
Refactor for modern asyncio use and pin dependencies (#407)
* Refactor for modern asyncio use * Removing unused dependency * Formatting * Refactor to use asyncio.create_task * Pin requirements to latest versions * Using Tornado to run loop * Formatting
1 parent edb5262 commit cf90f1c

File tree

7 files changed

+26
-35
lines changed

7 files changed

+26
-35
lines changed

bot.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pytz
1313
import sentry_sdk
14+
import tornado
1415

1516
from client_wrapper import ClientWrapper
1617
from constants import (
@@ -131,7 +132,6 @@ def __init__(
131132
npm_token,
132133
timezone,
133134
repos_info,
134-
loop,
135135
):
136136
"""
137137
Create the slack bot
@@ -143,15 +143,13 @@ def __init__(
143143
npm_token (str): The NPM token to publish npm packages
144144
timezone (tzinfo): The time zone of the team interacting with the bot
145145
repos_info (list of RepoInfo): Information about the repositories connected to channels
146-
loop (asyncio.events.AbstractEventLoop): The asyncio event loop
147146
"""
148147
self.doof_id = doof_id
149148
self.slack_access_token = slack_access_token
150149
self.github_access_token = github_access_token
151150
self.npm_token = npm_token
152151
self.timezone = timezone
153152
self.repos_info = repos_info
154-
self.loop = loop
155153
# Keep track of long running or scheduled tasks
156154
self.tasks = set()
157155
self.doof_boot = now_in_utc()
@@ -1038,7 +1036,7 @@ async def start_new_releases(self, command_args): # pylint: disable=too-many-lo
10381036
title=f"Starting release {version} with these commits",
10391037
text=release_notes,
10401038
)
1041-
self.loop.create_task(
1039+
asyncio.create_task(
10421040
self._new_release(
10431041
repo_info=repo_info,
10441042
version=version,
@@ -1549,7 +1547,7 @@ async def startup(self):
15491547
if not release_pr:
15501548
continue
15511549

1552-
self.loop.create_task(
1550+
asyncio.create_task(
15531551
self.run_release_lifecycle(
15541552
repo_info=repo_info, manager=None, release_pr=release_pr
15551553
)
@@ -1640,7 +1638,6 @@ async def async_main():
16401638
npm_token=envs["NPM_TOKEN"],
16411639
timezone=pytz.timezone(envs["TIMEZONE"]),
16421640
repos_info=repos_info,
1643-
loop=asyncio.get_event_loop(),
16441641
doof_id=doof_id,
16451642
)
16461643
app = make_app(secret=envs["SLACK_SECRET"], bot=bot)
@@ -1651,9 +1648,7 @@ async def async_main():
16511648

16521649
def main():
16531650
"""main function for bot command"""
1654-
loop = asyncio.get_event_loop()
1655-
loop.run_until_complete(async_main())
1656-
loop.run_forever()
1651+
tornado.ioloop.IOLoop.current().run_sync(async_main)
16571652

16581653

16591654
if __name__ == "__main__":

bot_local.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ async def async_main():
4949
timezone=envs["TIMEZONE"],
5050
npm_token=envs["NPM_TOKEN"],
5151
repos_info=repos_info,
52-
loop=asyncio.get_event_loop(),
5352
)
5453

5554
await bot.startup()
@@ -63,8 +62,7 @@ async def async_main():
6362

6463
def main():
6564
"""Main function"""
66-
loop = asyncio.get_event_loop()
67-
loop.run_until_complete(async_main())
65+
asyncio.run(async_main())
6866

6967

7068
if __name__ == "__main__":

bot_test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
class DoofSpoof(Bot):
5151
"""Testing bot"""
5252

53-
def __init__(self, *, loop):
53+
def __init__(self):
5454
"""Since the testing bot isn't contacting slack or github we don't need these tokens here"""
5555
super().__init__(
5656
doof_id="Doofenshmirtz",
@@ -59,7 +59,6 @@ def __init__(self, *, loop):
5959
npm_token=NPM_TOKEN,
6060
timezone=pytz.timezone("America/New_York"),
6161
repos_info=[WEB_TEST_REPO_INFO, LIBRARY_TEST_REPO_INFO],
62-
loop=loop,
6362
)
6463

6564
self.slack_users = []
@@ -130,9 +129,9 @@ def sleep_sync_mock(mocker):
130129

131130

132131
@pytest.fixture
133-
def doof(event_loop, sleep_sync_mock): # pylint: disable=unused-argument
132+
def doof(sleep_sync_mock): # pylint: disable=unused-argument
134133
"""Create a Doof"""
135-
yield DoofSpoof(loop=event_loop)
134+
yield DoofSpoof()
136135

137136

138137
@pytest.fixture

requirements.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
python-dateutil
2-
pytz
3-
requests
4-
sentry-sdk
5-
tornado
6-
virtualenv
7-
packaging
1+
packaging==25.0
2+
python-dateutil==2.9.0.post0
3+
pytz==2025.2
4+
requests==2.32.5
5+
sentry-sdk==2.35.1
6+
tornado==6.5.2
7+
virtualenv==20.34.0

test_requirements.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
black==25.1.0
2-
codecov
3-
pdbpp
4-
pylint
5-
pytest
6-
pytest-asyncio
7-
pytest-cov
8-
pytest-mock
2+
codecov==2.1.13
3+
pdbpp==0.11.7
4+
pylint==3.3.8
5+
pytest==8.4.1
6+
pytest-asyncio==1.1.0
7+
pytest-cov==6.2.1
8+
pytest-mock==3.14.1

web.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Web server for handling slack webhooks
33
"""
44

5+
import asyncio
56
import hmac
67
import json
78

@@ -54,7 +55,7 @@ async def post(self, *args, **kwargs): # pylint: disable=unused-argument
5455
arguments = json.loads(
5556
self.get_argument("payload")
5657
) # pylint: disable=no-value-for-parameter
57-
self.bot.loop.create_task(self.bot.handle_webhook(webhook_dict=arguments))
58+
asyncio.create_task(self.bot.handle_webhook(webhook_dict=arguments))
5859
await self.finish("")
5960

6061

@@ -87,7 +88,7 @@ async def post(self, *args, **kwargs): # pylint: disable=unused-argument
8788
await self.finish(challenge)
8889
return
8990

90-
self.bot.loop.create_task(self.bot.handle_event(webhook_dict=arguments))
91+
asyncio.create_task(self.bot.handle_event(webhook_dict=arguments))
9192

9293
await self.finish("")
9394

web_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tests for the web server"""
22

3-
import asyncio
43
import json
54
from unittest.mock import patch
65
import urllib.parse
@@ -21,8 +20,7 @@ class FinishReleaseTests(AsyncHTTPTestCase):
2120

2221
def setUp(self):
2322
self.secret = uuid.uuid4().hex
24-
self.loop = asyncio.get_event_loop()
25-
self.doof = DoofSpoof(loop=self.loop)
23+
self.doof = DoofSpoof()
2624
self.app = make_app(secret=self.secret, bot=self.doof)
2725

2826
super().setUp()

0 commit comments

Comments
 (0)