Skip to content

Commit 39d9967

Browse files
committed
parse schedule_tasks in script & fix bugs
1 parent dffb9f1 commit 39d9967

File tree

1 file changed

+76
-31
lines changed

1 file changed

+76
-31
lines changed

hooks/__init__.py

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -428,43 +428,88 @@ def man_run_task(task: str, env_str: str, src: CommandSource, server: PluginServ
428428
e)
429429

430430

431-
def parse_and_apply_scripts(script: str, server: PluginServerInterface):
431+
def _parse_and_apply_scripts(script: str, server: PluginServerInterface):
432432
try:
433433
# 读取
434434
with open(temp_config.scripts_list.get(script), 'r') as f:
435435
content: dict[str, Union[str, Union[list, dict]]] = yaml.load(f.read(), Loader=yaml.Loader)
436436

437-
for task in content.get('tasks'):
438-
use_cmd_file: bool = False
439-
cmd_file_path: str = ''
440-
441-
if task.get('command_file') is not None:
442-
tmp_var1 = str(task.get('command_file')).replace('{hooks_config_path}', server.get_data_folder())
437+
if content.get('tasks') is not None:
438+
for task in content.get('tasks'):
439+
use_cmd_file: bool = False
440+
cmd_file_path: str = ''
441+
442+
if task.get('command_file') is not None:
443+
var1 = str(task.get('command_file')).replace('{hooks_config_path}', server.get_data_folder())
444+
445+
if os.path.isfile(var1):
446+
cmd_file_path = var1
447+
use_cmd_file = True
448+
else:
449+
server.logger.warning(
450+
f'Script path for task {task.get("name")} is invalid, use command instead! '
451+
f'{task.get("command_file")}')
443452

444-
if os.path.isfile(tmp_var1):
445-
cmd_file_path = tmp_var1
446-
use_cmd_file = True
453+
if use_cmd_file:
454+
with open(cmd_file_path, 'r') as command_file:
455+
command_file_content = command_file.read()
456+
# 创建task
457+
create_task(task.get('task_type'), command_file_content, task.get('name'),
458+
server.get_plugin_command_source(),
459+
server, created_by=script)
447460
else:
448-
server.logger.warning(
449-
f'Script path for task {task.get("name")} is invalid, use command instead! '
450-
f'{task.get("command_file")}')
451-
452-
if use_cmd_file:
453-
with open(cmd_file_path, 'r') as command_file:
454-
command_file_content = command_file.read()
455-
# 创建task
456-
create_task(task.get('task_type'), command_file_content, task.get('name'),
457-
server.get_plugin_command_source(),
458-
server, created_by=script)
459-
else:
460-
# 创建task
461-
create_task(task.get('task_type'), task.get('command'), task.get('name'),
462-
server.get_plugin_command_source(),
463-
server, created_by=script)
464-
465-
for hook in task.get('hooks'):
466-
# 挂载
467-
mount_task(hook, task.get('name'), server.get_plugin_command_source(), server)
461+
# 创建task
462+
create_task(task.get('task_type'), task.get('command'), task.get('name'),
463+
server.get_plugin_command_source(),
464+
server, created_by=script)
465+
466+
if task.get('hooks') is None:
467+
continue
468+
for hook in task.get('hooks'):
469+
# 挂载
470+
mount_task(hook, task.get('name'), server.get_plugin_command_source(), server)
471+
472+
473+
if content.get('schedule_tasks') is not None:
474+
for schedule in content.get('schedule_tasks'):
475+
use_cmd_file: bool = False
476+
cmd_file_path: str = ''
477+
478+
if int(schedule.get('exec_interval')) <= 0:
479+
server.logger.warning(f'Invalid exec_interval in schedule task {schedule.get("name")}!')
480+
481+
if schedule.get('command_file') is not None:
482+
var1 = str(schedule.get('command_file')).replace('{hooks_config_path}', server.get_data_folder())
483+
484+
if os.path.isfile(var1):
485+
cmd_file_path = var1
486+
use_cmd_file = True
487+
else:
488+
server.logger.warning(
489+
f'Script path for task {schedule.get("name")} is invalid, use command instead! '
490+
f'{schedule.get("command_file")}')
491+
492+
if use_cmd_file:
493+
with open(cmd_file_path, 'r') as command_file:
494+
command_file_content = command_file.read()
495+
# 创建task
496+
create_task(schedule.get('task_type'), command_file_content, schedule.get('name'),
497+
server.get_plugin_command_source(),
498+
server, created_by=script, is_schedule=True,
499+
exec_interval=schedule.get('exec_interval'))
500+
else:
501+
# 创建task
502+
create_task(schedule.get('task_type'), schedule.get('command'), schedule.get('name'),
503+
server.get_plugin_command_source(),
504+
server, created_by=script, is_schedule=True,
505+
exec_interval=schedule.get('exec_interval'))
506+
507+
if schedule.get('hooks') is None:
508+
continue
509+
for hook in schedule.get('hooks'):
510+
# 挂载
511+
mount_task(hook, schedule.get('name'), server.get_plugin_command_source(), server)
512+
468513
except Exception as e:
469514
server.logger.exception(f'Unexpected exception when parse or apply scripts {os.path.basename(script)}! Please '
470515
f'check your scripts.', e)
@@ -498,7 +543,7 @@ def list_all_files(root_dir) -> list[str]:
498543

499544
# 遍历所有已成功注册的脚本
500545
for script in temp_config.scripts_list.keys():
501-
parse_and_apply_scripts(script, server)
546+
_parse_and_apply_scripts(script, server)
502547

503548

504549
def on_load(server: PluginServerInterface, old_module):

0 commit comments

Comments
 (0)