-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from RockChinQ/feat/heartbeat-checking
Feat: heartbeat and auto-disable
- Loading branch information
Showing
15 changed files
with
236 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import asyncio | ||
import traceback | ||
import logging | ||
|
||
from ....models.watchdog import task | ||
from ....models.channel import mgr as chanmgr | ||
from ....entities import channel | ||
|
||
|
||
class HeartBeatTask(task.AbsTask): | ||
"""HeartBeat task.""" | ||
|
||
channel: chanmgr.AbsChannelManager | ||
|
||
cfg: dict | ||
|
||
def __init__(self, chan: chanmgr.AbsChannelManager, cfg: dict): | ||
self.channel = chan | ||
self.cfg = cfg | ||
|
||
self.delay = 10 | ||
self.interval = cfg['interval'] | ||
|
||
async def trigger(self): | ||
"""Trigger this task.""" | ||
|
||
process_task = [] | ||
|
||
for chan in self.channel.channels: | ||
if chan.enabled: | ||
async def process(ch: channel.Channel): | ||
fail_count = await ch.heartbeat(timeout=self.cfg["timeout"]) | ||
if fail_count > self.cfg["fail_limit"]: | ||
try: | ||
self.channel.disable_channel(ch.id) | ||
logging.info(f"Disabled channel {ch.id} due to heartbeat failed {fail_count} times") | ||
except Exception: | ||
logging.warn(f"Failed to disable channel {ch.id}, traceback: {traceback.format_exc()}") | ||
await self.channel.update_channel(ch) | ||
|
||
process_task.append(process(chan)) | ||
|
||
logging.info(f"Start heartbeat task, {len(process_task)} channels to process") | ||
await asyncio.gather(*process_task) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import asyncio | ||
|
||
from ...models.watchdog import wd | ||
from ...models.watchdog import task | ||
|
||
|
||
class WatchDog(wd.AbsWatchDog): | ||
"""WatchDog implementation.""" | ||
|
||
def __init__(self): | ||
self.tasks = [] | ||
|
||
async def run(self): | ||
cor = [] | ||
|
||
for task in self.tasks: | ||
cor.append(task.loop()) | ||
|
||
await asyncio.gather(*cor) | ||
|
||
def add_task(self, task: task.AbsTask): | ||
"""Add a task.""" | ||
self.tasks.append(task) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import abc | ||
import asyncio | ||
import traceback | ||
import logging | ||
|
||
|
||
class AbsTask(metaclass=abc.ABCMeta): | ||
"""Task of WatchDog. | ||
A task may need instances of other modules to work. These dependencies should be set by outer constructor. | ||
Task's delay and interval may be set by outer constructor also, and be scheduled by WatchDog implementation. | ||
""" | ||
|
||
delay: int = 0 | ||
"""Delay before first trigger.""" | ||
|
||
interval: int = 60 | ||
"""Interval between two triggers.""" | ||
|
||
@abc.abstractmethod | ||
async def trigger(self): | ||
"""Trigger this task.""" | ||
raise NotImplementedError | ||
|
||
async def loop(self): | ||
"""Loop this task.""" | ||
await asyncio.sleep(self.delay) | ||
while True: | ||
try: | ||
await self.trigger() | ||
except Exception: | ||
logging.warn(f"Failed to trigger task {self.__class__.__name__}, traceback: {traceback.format_exc()}") | ||
await asyncio.sleep(self.interval) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import abc | ||
|
||
from . import task | ||
|
||
class AbsWatchDog(metaclass=abc.ABCMeta): | ||
"""Model of WatchDog.""" | ||
|
||
tasks: list[task.AbsTask] | ||
"""Added tasks.""" | ||
|
||
@abc.abstractmethod | ||
async def run(self): | ||
"""Run WatchDog system.""" | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
import os | ||
import sys | ||
import asyncio | ||
import logging | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
if not os.path.exists('./data'): | ||
os.mkdir('./data') | ||
|
||
from free_one_api.impls import app | ||
|
||
def main(): | ||
application = asyncio.run(app.make_application("./data/config.yaml")) | ||
loop = asyncio.get_event_loop() | ||
|
||
application = loop.run_until_complete(app.make_application("./data/config.yaml")) | ||
|
||
application.run() | ||
loop.run_until_complete(application.run()) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ claude-api | |
bardapi | ||
hugchat | ||
g4f | ||
revTongYi | ||
revTongYi | ||
colorlog |