Skip to content

Commit efb43ed

Browse files
committed
Add monitoring
1 parent aeadfd8 commit efb43ed

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

bot/bot.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
from typing import List
22

3-
from aiotg import Bot as BaseBot, API_TIMEOUT, Chat
3+
from aiotg import Bot as BaseBot, API_TIMEOUT, Chat, asyncio, aiohttp
44
import re
55

66

77
class Bot(BaseBot):
8-
def __init__(self, api_token: str, moderators: List[int],
9-
api_timeout: int = API_TIMEOUT,
10-
botan_token=None,
11-
name=None):
8+
healthcheckio_url = 'https://hchk.io/{token}'
9+
healthcheckio_interval = 150 # seconds
10+
healthcheckio_token = None
11+
12+
def __init__(
13+
self,
14+
api_token: str, moderators: List[int],
15+
api_timeout: int = API_TIMEOUT,
16+
botan_token: str = None,
17+
healthcheckio_token: str = None,
18+
name=None):
1219
super(Bot, self).__init__(
1320
api_token=api_token,
1421
api_timeout=api_timeout,
1522
botan_token=botan_token,
1623
name=name
1724
)
18-
25+
self.healthcheckio_token = healthcheckio_token
1926
self.moderators = moderators
2027
self._moderators_commands = []
2128

29+
async def still_alive(self):
30+
async with aiohttp.ClientSession() as session:
31+
async with session.get(self.healthcheckio_url.format(token=self.healthcheckio_token)):
32+
pass
33+
await asyncio.sleep(self.healthcheckio_interval)
34+
await self.still_alive()
35+
36+
def run(self, debug=False, reload=None):
37+
if self.healthcheckio_token:
38+
loop = asyncio.get_event_loop()
39+
loop.create_task(self.still_alive())
40+
super(Bot, self).run(debug=debug, reload=reload)
41+
2242
def add_moderators_command(self, regexp, fn):
2343
"""
2444
Manually register regexp based command

bot/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def get_moderators():
3636

3737
bot = Bot(
3838
api_token=os.environ["BOT_TOKEN"],
39+
healthcheckio_token=os.environ["HEALTHCHECKIO_TOKEN"],
3940
moderators=get_moderators()
4041
)
4142

check.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import asyncio
2+
3+
4+
async def primary_task(step=1):
5+
print('primary task {step}'.format(step=step))
6+
await asyncio.sleep(1)
7+
await primary_task(step + 1)
8+
9+
10+
async def secondary_task(step=1):
11+
print('secondary task {step}'.format(step=step))
12+
await asyncio.sleep(1)
13+
await secondary_task(step + 1)
14+
15+
16+
if __name__ == '__main__':
17+
loop = asyncio.get_event_loop()
18+
loop.create_task(secondary_task())
19+
loop.run_until_complete(primary_task())

0 commit comments

Comments
 (0)