Skip to content

Commit b262f2e

Browse files
committed
fix bug: schedule task name and thread name conflict, print thread name in !!hooks list tasks
1 parent 7c37108 commit b262f2e

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,39 @@ Apply和加载有区别,Apply是指:**插件创建Task、挂载Task的操作
8686

8787
首先要编写脚本,示例:
8888
``````
89-
tasks:
89+
tasks: # 普通任务
9090
- name: motd # 声明task的名字,别有空格
9191
task_type: shell_command # 任务类型
9292
command: date # 要执行的指令
9393
command_file: {hooks_config_path}/scripts/script.txt(脚本内容路径,如果此路径有效,插件将从command_file中读取command并执行,执行的指令即文件的所有内容(文件扩展名随意写,插件并非直接执行此文件,而是将文件内容读到内存处理后再执行)。command_file和command只用写一个,command_file如果写了command项就会被忽略)。{hooks_config_path}会被替换为hooks插件的配置文件目录,即server.get_data_folder()
9494
hooks: # 要挂载到的hook,必须是数组
9595
- on_server_started
9696
- on_mcdr_started
97+
schedule_tasks: # 定时任务声明
98+
- name: ababababa # 名字
99+
task_type: server_command # 任务类型
100+
command: say 6 # 指令
101+
command_file: {hooks_config_path}/scripts/script.txt(脚本内容路径,如果此路径有效,插件将从command_file中读取command并执行,执行的指令即文件的所有内容(文件扩展名随意写,插件并非直接执行此文件,而是将文件内容读到内存处理后再执行)。command_file和command只用写一个,command_file如果写了command项就会被忽略)。{hooks_config_path}会被替换为hooks插件的配置文件目录,即server.get_data_folder()
102+
hooks:
103+
- on_user_info # 定时任务也可以被挂载哟~
97104
``````
98105

99106
将其命名为`<脚本名字>.yaml`,并且放到`config/hooks/scripts`文件夹或子文件夹中
100107

108+
### 定时任务(`schedule_task`
109+
110+
定时任务就是隔一段时间执行一次的任务
111+
112+
每一个定时任务都会被放到一个单独的线程调度,线程名`hooks - schedule_task_daemon(<任务名>)`,可以使用`!!MCDR status`查看
113+
114+
#### 指令
115+
116+
`!!hooks schedule <name> <exec_interval> <task_type> <command>`
117+
- `<name>`任务名字,定时任务本质上和任务一样,只不过多了执行间隔的属性
118+
- `<exec_interval>`执行间隔,单位秒,必须是整数
119+
- `<task_type>`任务类型
120+
- `<command>`指令
121+
101122
### 其他指令
102123

103124
`!!hooks list mount`

hooks/__init__.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ class TaskType(Enum):
4848

4949
class Task:
5050
def __init__(self, name, task_type, created_by, command):
51-
self.name = name
51+
self.task_name = name
5252
self.task_type = task_type
5353
self.created_by = created_by
5454
self.command = command
5555

56-
name: str = 'undefined'
56+
task_name: str = 'undefined'
5757

5858
task_type: TaskType = TaskType.undefined
5959

@@ -63,20 +63,20 @@ def __init__(self, name, task_type, created_by, command):
6363

6464
@new_thread('hooks - execute')
6565
def execute_task(self, server: PluginServerInterface, hook: str, var_dict: dict = None, obj_dict: dict = None):
66-
server.logger.debug(f'Executing task: {self.name}, task_type: {self.task_type}, command: {self.command}')
66+
server.logger.debug(f'Executing task: {self.task_name}, task_type: {self.task_type}, command: {self.command}')
6767
server.logger.debug(f'objects_dict: {str(var_dict)}')
6868

6969
start_time = time.time()
7070

7171
if self.command is None:
7272
server.logger.error(
73-
f'Task state is not correct! Task: {self.name} Hooks: {hook} TaskType: {self.task_type} '
73+
f'Task state is not correct! Task: {self.task_name} Hooks: {hook} TaskType: {self.task_type} '
7474
f'command: {self.command}')
7575
return
7676

7777
if self.task_type == TaskType.undefined:
7878
server.logger.error(
79-
f'Task state is not correct! Task: {self.name} Hooks: {hook} TaskType: {self.task_type} '
79+
f'Task state is not correct! Task: {self.task_name} Hooks: {hook} TaskType: {self.task_type} '
8080
f'command: {self.command}')
8181
return
8282

@@ -126,7 +126,7 @@ def execute_task(self, server: PluginServerInterface, hook: str, var_dict: dict
126126
else:
127127
exec(self.command, globals(), locals())
128128

129-
server.logger.debug(f'Task finished, name: {self.name}, task_type: {self.task_type}, command: {self.command}, '
129+
server.logger.debug(f'Task finished, name: {self.task_name}, task_type: {self.task_type}, command: {self.command}, '
130130
f'costs {time.time() - start_time} seconds.')
131131

132132

@@ -142,6 +142,9 @@ def stop_all_schedule_daemon_threads():
142142
class AThread(threading.Thread):
143143
def init_thread(self):
144144
super().__init__(daemon=True)
145+
146+
def set_thread_name(self, name: str):
147+
self.name = name
145148

146149

147150
class ScheduleTask(Task, AThread):
@@ -151,6 +154,7 @@ def __init__(self, name, task_type, created_by, command, server_inst, exec_inter
151154
self.exec_interval = exec_interval
152155
self.stop_event = threading.Event()
153156
super().init_thread()
157+
super(Task, self).set_thread_name(f'hooks - schedule_task_daemon({name})')
154158
temp_config.schedule_daemon_threads.append(self)
155159

156160
def break_thread(self):
@@ -159,7 +163,7 @@ def break_thread(self):
159163
def run(self):
160164
if self.exec_interval <= 0:
161165
self.server_inst.logger.warning(
162-
f'Schedule task {self.name} has illegal exec_interval: {self.exec_interval}')
166+
f'Schedule task {self.task_name} has illegal exec_interval: {self.exec_interval}')
163167
return
164168

165169
while True:
@@ -326,7 +330,6 @@ def create_task(task_type: str, command: str, name: str, src: CommandSource, ser
326330
server_inst=server, exec_interval=exec_interval)
327331
temp_config.task[name] = var1
328332
var1.start()
329-
var1.name = f'hooks - schedule_task_daemon({name})'
330333

331334
server.logger.info(f'Successfully created task {name}')
332335
src.reply(RTextMCDRTranslation('hooks.create.success', name))
@@ -365,7 +368,7 @@ def list_task(src: CommandSource):
365368
for t in temp_config.task.values():
366369
rtext_list.append(RTextList(
367370
RText('\n '),
368-
RText(t.name, color=RColor.red).h(t.task_type.name),
371+
RText(t.task_name, color=RColor.red).h(t.task_type.name),
369372
RText(f' created by: "{t.created_by}"', color=RColor.green)
370373
))
371374
src.reply(RTextMCDRTranslation('hooks.list.task', rtext_list))

0 commit comments

Comments
 (0)