mirror of
https://gitee.com/ssssssss-team/magic-api.git
synced 2026-05-09 22:02:37 +08:00
增加magic-api.task.enable配置
This commit is contained in:
@@ -46,11 +46,11 @@ public class TaskMagicDynamicRegistry extends AbstractMagicDynamicRegistry<TaskI
|
||||
@Override
|
||||
protected boolean register(MappingNode<TaskInfo> mappingNode) {
|
||||
TaskInfo info = mappingNode.getEntity();
|
||||
if(taskScheduler != null){
|
||||
if (taskScheduler != null) {
|
||||
CronTask cronTask = new CronTask(() -> {
|
||||
TaskInfo entity = mappingNode.getEntity();
|
||||
String scriptName = MagicConfiguration.getMagicResourceService().getScriptName(entity);
|
||||
if(entity.isEnabled()){
|
||||
if (entity.isEnabled()) {
|
||||
try {
|
||||
logger.info("定时任务:[{}]开始执行", scriptName);
|
||||
MagicScriptContext magicScriptContext = new MagicScriptContext();
|
||||
@@ -65,8 +65,6 @@ public class TaskMagicDynamicRegistry extends AbstractMagicDynamicRegistry<TaskI
|
||||
}, info.getCron());
|
||||
mappingNode.setMappingData(taskScheduler.schedule(cronTask.getRunnable(), cronTask.getTrigger()));
|
||||
logger.debug("注册定时任务:[{},{}]", MagicConfiguration.getMagicResourceService().getScriptName(info), info.getCron());
|
||||
} else {
|
||||
logger.debug("注册定时任务失败:[{}, {}], 当前 TaskScheduler 为空", MagicConfiguration.getMagicResourceService().getScriptName(info), info.getCron());
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -74,10 +72,13 @@ public class TaskMagicDynamicRegistry extends AbstractMagicDynamicRegistry<TaskI
|
||||
|
||||
@Override
|
||||
protected void unregister(MappingNode<TaskInfo> mappingNode) {
|
||||
if (taskScheduler == null) {
|
||||
return;
|
||||
}
|
||||
TaskInfo info = mappingNode.getEntity();
|
||||
logger.debug("取消注册定时任务:[{}, {}, {}]", info.getName(), info.getPath(), info.getCron());
|
||||
ScheduledFuture<?> scheduledFuture = (ScheduledFuture<?>) mappingNode.getMappingData();
|
||||
if(scheduledFuture != null){
|
||||
if (scheduledFuture != null) {
|
||||
try {
|
||||
scheduledFuture.cancel(true);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -32,14 +32,17 @@ public class MagicAPITaskConfiguration implements MagicPluginConfiguration {
|
||||
@ConditionalOnMissingBean
|
||||
public TaskMagicDynamicRegistry taskMagicDynamicRegistry(TaskInfoMagicResourceStorage taskInfoMagicResourceStorage) {
|
||||
MagicTaskConfig.Shutdown shutdown = config.getShutdown();
|
||||
ThreadPoolTaskScheduler poolTaskScheduler = new ThreadPoolTaskScheduler();
|
||||
poolTaskScheduler.setPoolSize(config.getPool().getSize());
|
||||
poolTaskScheduler.setWaitForTasksToCompleteOnShutdown(shutdown.isAwaitTermination());
|
||||
if(shutdown.getAwaitTerminationPeriod() != null){
|
||||
poolTaskScheduler.setAwaitTerminationSeconds((int) shutdown.getAwaitTerminationPeriod().getSeconds());
|
||||
ThreadPoolTaskScheduler poolTaskScheduler = null;
|
||||
if(config.isEnable()){
|
||||
poolTaskScheduler = new ThreadPoolTaskScheduler();
|
||||
poolTaskScheduler.setPoolSize(config.getPool().getSize());
|
||||
poolTaskScheduler.setWaitForTasksToCompleteOnShutdown(shutdown.isAwaitTermination());
|
||||
if(shutdown.getAwaitTerminationPeriod() != null){
|
||||
poolTaskScheduler.setAwaitTerminationSeconds((int) shutdown.getAwaitTerminationPeriod().getSeconds());
|
||||
}
|
||||
poolTaskScheduler.setThreadNamePrefix(config.getThreadNamePrefix());
|
||||
poolTaskScheduler.initialize();
|
||||
}
|
||||
poolTaskScheduler.setThreadNamePrefix(config.getThreadNamePrefix());
|
||||
poolTaskScheduler.initialize();
|
||||
return new TaskMagicDynamicRegistry(taskInfoMagicResourceStorage, poolTaskScheduler);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,11 @@ import java.time.Duration;
|
||||
@ConfigurationProperties("magic-api.task")
|
||||
public class MagicTaskConfig {
|
||||
|
||||
/**
|
||||
* 是否启用定时任务
|
||||
*/
|
||||
private boolean enable = true;
|
||||
|
||||
/**
|
||||
* 线程池相关配置
|
||||
*/
|
||||
@@ -39,6 +44,14 @@ public class MagicTaskConfig {
|
||||
this.threadNamePrefix = threadNamePrefix;
|
||||
}
|
||||
|
||||
public boolean isEnable() {
|
||||
return enable;
|
||||
}
|
||||
|
||||
public void setEnable(boolean enable) {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
public static class Pool {
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user