feat: 完成简单的任务执行
This commit is contained in:
@@ -7,6 +7,8 @@ import com.lanyuanxiaoyao.service.template.controller.SimpleControllerSupport;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@@ -20,8 +22,11 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("task")
|
@RequestMapping("task")
|
||||||
public class TaskController extends SimpleControllerSupport<Task, Void, TaskController.ListItem, TaskController.DetailItem> {
|
public class TaskController extends SimpleControllerSupport<Task, Void, TaskController.ListItem, TaskController.DetailItem> {
|
||||||
|
private final TaskService taskService;
|
||||||
|
|
||||||
public TaskController(TaskService service) {
|
public TaskController(TaskService service) {
|
||||||
super(service);
|
super(service);
|
||||||
|
this.taskService = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -29,6 +34,12 @@ public class TaskController extends SimpleControllerSupport<Task, Void, TaskCont
|
|||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("execute")
|
||||||
|
public GlobalResponse<Object> execute(@RequestBody ExecuteRequest request) {
|
||||||
|
taskService.execute(request.templateId());
|
||||||
|
return GlobalResponse.responseSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Function<Void, Task> saveItemMapper() {
|
protected Function<Void, Task> saveItemMapper() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
@@ -81,4 +92,7 @@ public class TaskController extends SimpleControllerSupport<Task, Void, TaskCont
|
|||||||
LocalDateTime finishedTime
|
LocalDateTime finishedTime
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public record ExecuteRequest(Long templateId) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ public class TaskTemplateController extends SimpleControllerSupport<TaskTemplate
|
|||||||
return switch (item.type) {
|
return switch (item.type) {
|
||||||
case CLASS -> {
|
case CLASS -> {
|
||||||
var template = new ClassTaskTemplate();
|
var template = new ClassTaskTemplate();
|
||||||
|
template.setId(item.id());
|
||||||
template.setName(item.name());
|
template.setName(item.name());
|
||||||
template.setDescription(item.description());
|
template.setDescription(item.description());
|
||||||
template.setClazz(item.clazz());
|
template.setClazz(item.clazz());
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
package com.lanyuanxiaoyao.leopard.server.service;
|
package com.lanyuanxiaoyao.leopard.server.service;
|
||||||
|
|
||||||
import com.lanyuanxiaoyao.leopard.server.entity.Task;
|
import com.lanyuanxiaoyao.leopard.server.entity.Task;
|
||||||
|
import com.lanyuanxiaoyao.leopard.server.entity.templates.ClassTaskTemplate;
|
||||||
import com.lanyuanxiaoyao.leopard.server.repository.TaskRepository;
|
import com.lanyuanxiaoyao.leopard.server.repository.TaskRepository;
|
||||||
|
import com.lanyuanxiaoyao.leopard.server.service.task.TaskRunner;
|
||||||
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
|
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,7 +19,26 @@ import org.springframework.stereotype.Service;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class TaskService extends SimpleServiceSupport<Task> {
|
public class TaskService extends SimpleServiceSupport<Task> {
|
||||||
public TaskService(TaskRepository repository) {
|
private final ApplicationContext context;
|
||||||
|
private final TaskTemplateService taskTemplateService;
|
||||||
|
private final ExecutorService executors = Executors.newFixedThreadPool(50);
|
||||||
|
|
||||||
|
public TaskService(TaskRepository repository, ApplicationContext context, TaskTemplateService taskTemplateService) {
|
||||||
super(repository);
|
super(repository);
|
||||||
|
this.context = context;
|
||||||
|
this.taskTemplateService = taskTemplateService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void execute(Long templateId) {
|
||||||
|
var template = taskTemplateService.detail(templateId);
|
||||||
|
switch (template.getType()) {
|
||||||
|
case CLASS -> {
|
||||||
|
ClassTaskTemplate classTaskTemplate = (ClassTaskTemplate) template;
|
||||||
|
var runner = context.getBean(classTaskTemplate.getClazz(), TaskRunner.class);
|
||||||
|
executors.submit(() -> {
|
||||||
|
runner.runTask(classTaskTemplate, Map.of());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import java.util.Map;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component("com.lanyuanxiaoyao.leopard.server.service.task.UpdateStockInformationTask")
|
||||||
public class UpdateStockInformationTask extends TaskRunner {
|
public class UpdateStockInformationTask extends TaskRunner {
|
||||||
private final StockService stockService;
|
private final StockService stockService;
|
||||||
private final TuShareService tuShareService;
|
private final TuShareService tuShareService;
|
||||||
|
|||||||
@@ -69,8 +69,23 @@ function TaskTemplateList() {
|
|||||||
{
|
{
|
||||||
type: 'operation',
|
type: 'operation',
|
||||||
label: '操作',
|
label: '操作',
|
||||||
width: 100,
|
width: 150,
|
||||||
buttons: [
|
buttons: [
|
||||||
|
{
|
||||||
|
type: 'action',
|
||||||
|
label: '执行',
|
||||||
|
level: 'link',
|
||||||
|
actionType: 'ajax',
|
||||||
|
api: {
|
||||||
|
method: 'post',
|
||||||
|
url: `${commonInfo.baseUrl}/task/execute`,
|
||||||
|
data: {
|
||||||
|
templateId: '${id}',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
confirmText: '确认执行模板<span class="text-lg font-bold mx-2">${name}</span>?',
|
||||||
|
confirmTitle: '删除',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
type: 'action',
|
type: 'action',
|
||||||
label: '详情',
|
label: '详情',
|
||||||
@@ -96,9 +111,9 @@ function TaskTemplateList() {
|
|||||||
level: 'link',
|
level: 'link',
|
||||||
actionType: 'ajax',
|
actionType: 'ajax',
|
||||||
api: `get:${commonInfo.baseUrl}/task_template/remove/\${id}`,
|
api: `get:${commonInfo.baseUrl}/task_template/remove/\${id}`,
|
||||||
confirmText: '确认删除模板[${name}]?',
|
confirmText: '确认删除模板<span class="text-lg font-bold mx-2">${name}</span>?',
|
||||||
confirmTitle: "删除",
|
confirmTitle: '删除',
|
||||||
}
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user