新增magic-api.task.log开关,用于控制定时任务是否打印日志

This commit is contained in:
jmxd 2023-03-23 09:27:24 +08:00
parent 1bf5769acf
commit d7685a345a
3 changed files with 25 additions and 4 deletions

View File

@ -23,9 +23,12 @@ public class TaskMagicDynamicRegistry extends AbstractMagicDynamicRegistry<TaskI
private static final Logger logger = LoggerFactory.getLogger(TaskMagicDynamicRegistry.class);
public TaskMagicDynamicRegistry(MagicResourceStorage<TaskInfo> magicResourceStorage, TaskScheduler taskScheduler) {
private final boolean showLog;
public TaskMagicDynamicRegistry(MagicResourceStorage<TaskInfo> magicResourceStorage, TaskScheduler taskScheduler, boolean showLog) {
super(magicResourceStorage);
this.taskScheduler = taskScheduler;
this.showLog = showLog;
}
@EventListener(condition = "#event.type == 'task'")
@ -54,14 +57,18 @@ public class TaskMagicDynamicRegistry extends AbstractMagicDynamicRegistry<TaskI
CronTask cronTask = new CronTask(() -> {
if (entity.isEnabled()) {
try {
logger.info("定时任务:[{}]开始执行", scriptName);
if (showLog) {
logger.info("定时任务:[{}]开始执行", scriptName);
}
MagicScriptContext magicScriptContext = new MagicScriptContext();
magicScriptContext.setScriptName(scriptName);
ScriptManager.executeScript(entity.getScript(), magicScriptContext);
} catch (Exception e) {
logger.error("定时任务执行出错", e);
} finally {
logger.info("定时任务:[{}]执行完毕", scriptName);
if (showLog) {
logger.info("定时任务:[{}]执行完毕", scriptName);
}
}
}
}, trigger);

View File

@ -43,7 +43,7 @@ public class MagicAPITaskConfiguration implements MagicPluginConfiguration {
poolTaskScheduler.setThreadNamePrefix(config.getThreadNamePrefix());
poolTaskScheduler.initialize();
}
return new TaskMagicDynamicRegistry(taskInfoMagicResourceStorage, poolTaskScheduler);
return new TaskMagicDynamicRegistry(taskInfoMagicResourceStorage, poolTaskScheduler, config.isLog());
}
@Override

View File

@ -13,6 +13,12 @@ public class MagicTaskConfig {
*/
private boolean enable = true;
/**
* 是否打印日志
* @since 2.1.0
*/
private boolean log = false;
/**
* 线程池相关配置
*/
@ -28,6 +34,14 @@ public class MagicTaskConfig {
*/
private String threadNamePrefix = "magic-task-";
public boolean isLog() {
return log;
}
public void setLog(boolean log) {
this.log = log;
}
public Pool getPool() {
return this.pool;
}