Skip to content

Commit

Permalink
定时任务根据chat_id存储
Browse files Browse the repository at this point in the history
  • Loading branch information
黄传 committed Jan 7, 2025
1 parent 6785a45 commit 6ed61ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions plugins/scheduler_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def execute(self, chat_id: str, action: str, params: Dict[str, Any]) -> Di
return {"error": "任务不存在"}

elif action == "get_all_tasks":
tasks = self.scheduler.get_all_tasks()
tasks = self.scheduler.get_all_tasks(chat_id)
return {
"tasks": [
{
Expand All @@ -110,7 +110,7 @@ async def execute(self, chat_id: str, action: str, params: Dict[str, Any]) -> Di
"message": "任务删除成功" if success else "任务不存在"
}
elif action == "delete_all_task":
success = self.scheduler.delete_all_task()
success = self.scheduler.delete_all_task(chat_id)
return {
"success": success,
"message": "所有任务删除成功" if success else "任务删除失败"
Expand Down
18 changes: 12 additions & 6 deletions plugins/scheduler_plugin/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,27 @@ def get_task(self, task_id: str) -> Optional[ScheduledTask]:
"""获取任务信息"""
return self.storage.get_task(task_id)

def get_all_tasks(self) -> List[ScheduledTask]:
def get_all_tasks(self, chat_id: str = None) -> List[ScheduledTask]:
"""获取所有任务"""
return self.storage.get_all_tasks()
return self.storage.get_all_tasks(chat_id)

def delete_task(self, task_id: str) -> bool:
"""删除任务"""
if self.scheduler.get_job(task_id):
self.scheduler.remove_job(task_id)
return self.storage.delete_task(task_id)

def delete_all_task(self) -> bool:
for task in self.storage.get_all_tasks():
self.scheduler.remove_job(task.id)
def delete_all_task(self, chat_id: str = None) -> bool:
if chat_id:
tasks = self.storage.get_all_tasks(chat_id)
else:
tasks = self.storage.get_all_tasks()

for task in tasks:
if self.scheduler.get_job(task.id):
self.scheduler.remove_job(task.id)
"""删除任务"""
return self.storage.delete_all_task()
return self.storage.delete_all_task(chat_id)

async def _execute_task(self, task: ScheduledTask):
"""执行任务"""
Expand Down
18 changes: 12 additions & 6 deletions plugins/scheduler_plugin/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ def get_task(self, task_id: str) -> Optional[ScheduledTask]:
return self._row_to_task(row)
return None

def get_all_tasks(self) -> List[ScheduledTask]:
"""获取所有任务"""
def get_all_tasks(self, chat_id: str = None) -> List[ScheduledTask]:
"""获取所有任务,可选择按chat_id过滤"""
tasks = []
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute('SELECT * FROM scheduled_tasks')
if chat_id:
cursor = conn.execute('SELECT * FROM scheduled_tasks WHERE chat_id = ?', (chat_id,))
else:
cursor = conn.execute('SELECT * FROM scheduled_tasks')
for row in cursor:
tasks.append(self._row_to_task(row))
return tasks
Expand All @@ -70,10 +73,13 @@ def delete_task(self, task_id: str) -> bool:
cursor = conn.execute('DELETE FROM scheduled_tasks WHERE id = ?', (task_id,))
return cursor.rowcount > 0

def delete_all_task(self) -> bool:
"""删除任务"""
def delete_all_task(self, chat_id: str = None) -> bool:
"""删除任务,可选择按chat_id删除"""
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute('DELETE FROM scheduled_tasks')
if chat_id:
cursor = conn.execute('DELETE FROM scheduled_tasks WHERE chat_id = ?', (chat_id,))
else:
cursor = conn.execute('DELETE FROM scheduled_tasks')
return True

def _row_to_task(self, row) -> ScheduledTask:
Expand Down

0 comments on commit 6ed61ec

Please sign in to comment.