v2.1
This commit is contained in:
父節點
d0734ef962
當前提交
3a73df1ef9
@ -17,8 +17,8 @@ import com.weilab.biology.core.validation.EnumValidation;
|
||||
import com.weilab.biology.mapper.JobMapper;
|
||||
import com.weilab.biology.service.AppConfigService;
|
||||
import com.weilab.biology.service.JobService;
|
||||
import com.weilab.biology.util.JobIdUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@ -43,12 +43,9 @@ public class JobController {
|
||||
@Autowired
|
||||
private JobMapper jobMapper;
|
||||
|
||||
@Value("${biology.request-path}")
|
||||
private String requestPath;
|
||||
|
||||
@PostMapping("/submit")
|
||||
public Result submit(@RequestParam String appName,
|
||||
@RequestParam(required = false) Long jobId,
|
||||
@RequestParam(required = false) String jobId,
|
||||
@RequestParam(required = false) String dataStr,
|
||||
@RequestParam(required = false) MultipartFile dataFile,
|
||||
@RequestParam String param,
|
||||
@ -64,7 +61,7 @@ public class JobController {
|
||||
}
|
||||
|
||||
Job job = null;
|
||||
if (jobId != null) {
|
||||
if (StrUtil.isNotBlank(jobId)) {
|
||||
// jobId不为空,从数据库获取已创建的任务
|
||||
job = jobMapper.selectById(jobId);
|
||||
if (job == null) {
|
||||
@ -72,18 +69,23 @@ public class JobController {
|
||||
}
|
||||
} else {
|
||||
job = new Job();
|
||||
job.setJobId(JobIdUtil.newId());
|
||||
}
|
||||
|
||||
// 解析param字符串为map对象
|
||||
JSONObject reqParamMap = null;
|
||||
try {
|
||||
reqParamMap = JSON.parseObject(param);
|
||||
reqParamMap.put("type", type);
|
||||
} catch (Exception e) {
|
||||
return Result.getResult(CommonError.PARAM_WRONG);
|
||||
}
|
||||
|
||||
// 将reqParam覆盖添加到原有job的param中
|
||||
JSONObject params = JSON.parseObject(job.getParam());
|
||||
if (params == null) {
|
||||
params = new JSONObject();
|
||||
}
|
||||
params.putAll(reqParamMap);
|
||||
job.setParam(JSON.toJSONString(params));
|
||||
job.setAppId(appConfig.getAppId());
|
||||
@ -103,6 +105,7 @@ public class JobController {
|
||||
}
|
||||
|
||||
Job job = new Job();
|
||||
job.setJobId(JobIdUtil.newId());
|
||||
job.setAppId(appConfig.getAppId());
|
||||
job.setParam("{}");
|
||||
job.setStatus(JobStatusEnum.CREATING.getKey());
|
||||
@ -115,7 +118,7 @@ public class JobController {
|
||||
* 上传文件接口,将文件地址保存至本地,并将路径放入参数中
|
||||
*/
|
||||
@PostMapping("/file/upload")
|
||||
public Result uploadFile(@RequestParam Long jobId,
|
||||
public Result uploadFile(@RequestParam String jobId,
|
||||
@RequestParam String fileKey,
|
||||
@RequestParam MultipartFile file) {
|
||||
Job job = jobMapper.selectById(jobId);
|
||||
@ -145,7 +148,7 @@ public class JobController {
|
||||
}
|
||||
|
||||
@PostMapping("/status/update")
|
||||
public Result updateJobStatus(@RequestParam Integer jobId,
|
||||
public Result updateJobStatus(@RequestParam String jobId,
|
||||
@EnumValidation(clazz = JobStatusEnum.class, message = "没有此状态") @RequestParam Integer status,
|
||||
@RequestParam(required = false) String result) {
|
||||
if (status.equals(JobStatusEnum.WAITING.getKey())) {
|
||||
@ -167,7 +170,7 @@ public class JobController {
|
||||
}
|
||||
|
||||
@GetMapping("/info/{jobId}")
|
||||
public Result getJobInfo(@PathVariable Integer jobId) {
|
||||
public Result getJobInfo(@PathVariable String jobId) {
|
||||
return jobService.getJobInfo(jobId);
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ public class JobDto implements Serializable {
|
||||
/**
|
||||
* jobId
|
||||
*/
|
||||
private Integer jobId;
|
||||
private String jobId;
|
||||
|
||||
/**
|
||||
* 任务状态
|
||||
|
@ -21,7 +21,7 @@ public class JobLessDto {
|
||||
/**
|
||||
* jobId
|
||||
*/
|
||||
private Integer jobId;
|
||||
private String jobId;
|
||||
|
||||
/**
|
||||
* 任务状态
|
||||
|
@ -23,8 +23,8 @@ public class Job implements Serializable {
|
||||
/**
|
||||
* jobId
|
||||
*/
|
||||
@TableId(value = "job_id", type = IdType.AUTO)
|
||||
private Integer jobId;
|
||||
@TableId(value = "job_id", type = IdType.INPUT)
|
||||
private String jobId;
|
||||
|
||||
/**
|
||||
* 应用ID
|
||||
|
@ -2,6 +2,7 @@ package com.weilab.biology.mapper;
|
||||
|
||||
import com.weilab.biology.core.data.po.AppConfig;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
@ -12,6 +13,7 @@ import org.apache.ibatis.annotations.Param;
|
||||
* @author
|
||||
* @since 2023-05-05
|
||||
*/
|
||||
@Mapper
|
||||
public interface AppConfigMapper extends BaseMapper<AppConfig> {
|
||||
|
||||
AppConfig getAppConfigByAppName(@Param("appName") String appName);
|
||||
|
@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.weilab.biology.core.data.dto.AppConfigDto;
|
||||
import com.weilab.biology.core.data.po.AppConfig;
|
||||
import com.weilab.biology.mapper.AppConfigMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@ -18,18 +18,18 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class AppConfigService extends ServiceImpl<AppConfigMapper, AppConfig> {
|
||||
|
||||
@Mapper
|
||||
@Autowired
|
||||
private AppConfigMapper appConfigMapper;
|
||||
|
||||
/**
|
||||
* 通过appName获取应用配置
|
||||
*
|
||||
* @param appName 唯一应用名
|
||||
* @return AppConfigDto
|
||||
*/
|
||||
public AppConfigDto getAppConfig( String appName) {
|
||||
return AppConfigDto.parse(appConfigMapper.getAppConfigByAppName(appName));
|
||||
public AppConfigDto getAppConfig(String appName) {
|
||||
return AppConfigDto.parse(appConfigMapper.getAppConfigByAppName(appName));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ public class JobService extends ServiceImpl<JobMapper, Job> {
|
||||
try {
|
||||
// dataStr和dataFile均为蛋白质序列,两者含义相同只保留其一,dataFile的优先级大于dataStr
|
||||
// 若dataFile为空,则将dataStr写入本地文件,之后向python传递文件路径参数
|
||||
String dataPath = String.format(appConfig.getRequestPath() + "job-%d-dataStr.txt", job.getJobId());
|
||||
String dataPath = String.format(appConfig.getRequestPath() + "job-%s-dataStr.txt", job.getJobId());
|
||||
if (dataFile != null) {
|
||||
FileUtil.writeFromStream(dataFile.getInputStream(), dataPath);
|
||||
} else if (!StrUtil.isBlank(dataStr)) {
|
||||
@ -82,7 +82,7 @@ public class JobService extends ServiceImpl<JobMapper, Job> {
|
||||
return getJobInfo(job.getJobId());
|
||||
}
|
||||
|
||||
public Result getJobInfo(Integer jobId) {
|
||||
public Result getJobInfo(String jobId) {
|
||||
Job job = jobMapper.selectById(jobId);
|
||||
if (job == null) {
|
||||
return Result.getResult(CommonError.CONTENT_NOT_FOUND);
|
||||
@ -160,6 +160,7 @@ public class JobService extends ServiceImpl<JobMapper, Job> {
|
||||
|
||||
private void runJob(AppConfigDto appConfig, Job job) {
|
||||
try {
|
||||
FileUtil.mkdir(appConfig.getLogPath());
|
||||
String logFilePath = String.format(appConfig.getLogPath() + "task-log-%s.txt", job.getJobId());
|
||||
String cmd = String.format("%s -setting '%s' >> %s 2>&1", appConfig.getCmd(), job.getParam(), logFilePath);
|
||||
String[] cmds = new String[]{"/bin/sh", "-c", cmd};
|
||||
@ -180,7 +181,7 @@ public class JobService extends ServiceImpl<JobMapper, Job> {
|
||||
/**
|
||||
* 开启异步定时任务,校验超时任务
|
||||
*/
|
||||
private void asyncScheduleTask(AppConfigDto appConfig, Integer jobId) {
|
||||
private void asyncScheduleTask(AppConfigDto appConfig, String jobId) {
|
||||
//等待120秒,检查是否已运行
|
||||
taskExecutorUtil.schedule(() -> {
|
||||
Job job = jobMapper.selectById(jobId);
|
||||
@ -205,8 +206,8 @@ public class JobService extends ServiceImpl<JobMapper, Job> {
|
||||
}
|
||||
|
||||
// 多个参数
|
||||
String param1 = job.getRequestTime().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + job.getJobId();
|
||||
String param2 = param1;
|
||||
String jobId = job.getJobId();
|
||||
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
String content = appConfig.getEmailTemplate().getString(status.getRemark());
|
||||
String subject = appConfig.getEmailTemplate().getString("subject");
|
||||
@ -214,7 +215,11 @@ public class JobService extends ServiceImpl<JobMapper, Job> {
|
||||
return;
|
||||
}
|
||||
|
||||
mailUtil.send(mail, String.format(subject, param1), String.format(content, param1, param2));
|
||||
// 格式化填充参数
|
||||
content = content.replaceAll("%jobId%", jobId).replaceAll("%time%", time);
|
||||
subject = subject.replaceAll("%jobId%", jobId).replaceAll("%time%", time);
|
||||
|
||||
mailUtil.send(mail, subject, content);
|
||||
}
|
||||
|
||||
}
|
||||
|
20
src/main/java/com/weilab/biology/util/JobIdUtil.java
Normal file
20
src/main/java/com/weilab/biology/util/JobIdUtil.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.weilab.biology.util;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* @author yurui
|
||||
* @date 2023/6/13
|
||||
*/
|
||||
public class JobIdUtil {
|
||||
|
||||
public static String newId() {
|
||||
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
|
||||
+ IdUtil.fastUUID().substring(0, 8);
|
||||
}
|
||||
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.weilab.biology.mapper.JobMapper">
|
||||
<resultMap id="BaseResultMap" type="com.weilab.biology.core.data.po.Job">
|
||||
<id column="job_id" jdbcType="INTEGER" property="jobId"/>
|
||||
<id column="job_id" jdbcType="VARCHAR" property="jobId"/>
|
||||
<result column="param" jdbcType="VARCHAR" property="param"/>
|
||||
<result column="mail" jdbcType="VARCHAR" property="mail"/>
|
||||
<result column="result" jdbcType="LONGVARCHAR" property="result"/>
|
||||
|
載入中…
x
新增問題並參考
Block a user