作业提交机器代码优化

This commit is contained in:
lqyan
2024-02-05 19:24:51 +08:00
parent 7ea3380b7d
commit 86e3fcb7f0
12 changed files with 142 additions and 97 deletions

View File

@@ -6,7 +6,6 @@ import com.jhinno.sdk.openapi.ArgsException;
import com.jhinno.sdk.openapi.CommonConstant;
import com.jhinno.sdk.openapi.ServiceException;
import com.jhinno.sdk.openapi.api.auth.AuthPathConstant;
import com.jhinno.sdk.openapi.api.auth.Token;
import com.jhinno.sdk.openapi.client.JHApiClient;
import org.apache.commons.lang3.StringUtils;
@@ -104,11 +103,11 @@ public class JHApiExecution {
String base64 = aes.encryptBase64(String.format("%s,%s", username, System.currentTimeMillis()));
params.put("username", base64);
String url = JHApiClient.getUrl(AuthPathConstant.AUTH_TOKEN_PATH, params);
Token token = get(url, new TypeReference<ResponseResult<Token>>() {
Map<String, String> token = get(url, new TypeReference<ResponseResult<Map<String, String>>>() {
});
tokenInfo = new TokenInfo();
tokenInfo.setUserName(username);
tokenInfo.setToken(token.getToken());
tokenInfo.setToken(token.get("token"));
tokenInfo.setCurrentTimestamp(System.currentTimeMillis());
TOKEN_INFO_MAP.put(username, tokenInfo);
}

View File

@@ -1,8 +1,6 @@
package com.jhinno.sdk.openapi.api.app;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 启动会话的请求参数

View File

@@ -1,19 +0,0 @@
package com.jhinno.sdk.openapi.api.app;
import lombok.Data;
/**
* 应用链接URL信息
*
* @author yanlongqi
* @date 2024/2/4 16:44
*/
@Data
public class AppUrlInfo {
/**
* 应用的url
*/
private String url;
}

View File

@@ -328,18 +328,18 @@ public class JHAppApiExecution extends JHApiExecution {
*
* @param username 应户名
* @param appName 应用名
* @return 应用链接信息
* @return 应用链接地址
*/
public AppUrlInfo getAppUrl(String username, String appName) {
public String getAppUrl(String username, String appName) {
if (StringUtils.isBlank(appName)) {
throw new ArgsException("appName为必填字段");
}
String path = AppPathConstant.APPS_GET_URL_PATH.replace("{appName}", appName);
List<AppUrlInfo> apps = get(path, username, new TypeReference<ResponseResult<List<AppUrlInfo>>>() {
List<Map<String, String>> apps = get(path, username, new TypeReference<ResponseResult<List<Map<String, String>>>>() {
});
if (CollectionUtil.isEmpty(apps)) {
throw new ServiceException(path, 500, "应用信息为空!");
}
return apps.get(0);
return apps.get(0).get("url");
}
}

View File

@@ -1,19 +0,0 @@
package com.jhinno.sdk.openapi.api.auth;
import lombok.Data;
/**
* 请求令牌,用户请求其他接口
*
* @author yanlongqi
* @date 2024/1/31 10:30
*/
@Data
public class Token {
/**
* 令牌
*/
private String token;
}

View File

@@ -1,21 +0,0 @@
package com.jhinno.sdk.openapi.api.file;
import lombok.Data;
/**
* 文件下载地址信息
*
* @author yanlongqi
* @date 2024/2/5 17:53
*/
@Data
public class DownloadInfo {
/**
* 文件地址
*/
private String url;
}

View File

@@ -1,18 +0,0 @@
package com.jhinno.sdk.openapi.api.file;
import lombok.Data;
/**
* 文件创建消息
*
* @author yanlongqi
* @date 2024/2/5 14:17
*/
@Data
public class FilePathInfo {
/**
* 新的文件夹的位置
*/
private String dirPath;
}

View File

@@ -1,5 +1,6 @@
package com.jhinno.sdk.openapi.api.file;
import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.jhinno.sdk.openapi.ArgsException;
import com.jhinno.sdk.openapi.CommonConstant;
@@ -116,7 +117,7 @@ public class JHFileApiExecution extends JHApiExecution {
* @param isForce 是否强制创建非必传默认false
* @return 新建后的文件路径
*/
public FilePathInfo mkdir(String username, String dirPath, Boolean isForce) {
public String mkdir(String username, String dirPath, Boolean isForce) {
if (StringUtils.isBlank(dirPath)) {
throw new ArgsException("dirPath不能为空");
}
@@ -125,8 +126,12 @@ public class JHFileApiExecution extends JHApiExecution {
if (isForce != null) {
body.put("isForce", isForce.toString());
}
return post(FilePathConstant.FILE_MKDIR_PATH, username, body, new TypeReference<ResponseResult<FilePathInfo>>() {
Map<String, String> result = post(FilePathConstant.FILE_MKDIR_PATH, username, body, new TypeReference<ResponseResult<Map<String, String>>>() {
});
if (CollectionUtil.isEmpty(result)) {
return null;
}
return result.get("dirPath");
}
@@ -137,7 +142,7 @@ public class JHFileApiExecution extends JHApiExecution {
* @param dirPath 文件路径
* @return 新建后的文件路径
*/
public FilePathInfo mkdir(String username, String dirPath) {
public String mkdir(String username, String dirPath) {
return mkdir(username, dirPath, null);
}
@@ -149,14 +154,18 @@ public class JHFileApiExecution extends JHApiExecution {
* @param filePath 文件路径
* @return 新的文件路径
*/
public FilePathInfo mkFile(String username, String filePath) {
public String mkFile(String username, String filePath) {
if (StringUtils.isBlank(filePath)) {
throw new ArgsException("filePath不能为空");
}
Map<String, Object> body = new HashMap<>(1);
body.put("filePath", filePath);
return post(FilePathConstant.FILE_MKFILE_PATH, username, body, new TypeReference<ResponseResult<FilePathInfo>>() {
Map<String, String> result = post(FilePathConstant.FILE_MKFILE_PATH, username, body, new TypeReference<ResponseResult<Map<String, String>>>() {
});
if (CollectionUtil.isEmpty(result)) {
return null;
}
return result.get("dirPath");
}
/**
@@ -217,15 +226,19 @@ public class JHFileApiExecution extends JHApiExecution {
* @param filePath 文件路径
* @return 文件地址信息
*/
public DownloadInfo getFileDownloadUrl(String username, String filePath) {
public String getFileDownloadUrl(String username, String filePath) {
if (StringUtils.isBlank(filePath)) {
throw new ArgsException("filePath不能为空");
}
Map<String, Object> params = new HashMap<>(1);
params.put("filePath", filePath);
String path = JHApiClient.getUrl(FilePathConstant.FILE_DOWNLOAD_PATH, params);
return get(path, username, new TypeReference<ResponseResult<DownloadInfo>>() {
Map<String, String> downloadInfo = get(path, username, new TypeReference<ResponseResult<Map<String, String>>>() {
});
if (CollectionUtil.isEmpty(downloadInfo)) {
return null;
}
return downloadInfo.get("url");
}
/**

View File

@@ -0,0 +1,60 @@
package com.jhinno.sdk.openapi.api.job;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.jhinno.sdk.openapi.ArgsException;
import com.jhinno.sdk.openapi.ServiceException;
import com.jhinno.sdk.openapi.api.JHApiExecution;
import com.jhinno.sdk.openapi.api.ResponseResult;
import com.jhinno.sdk.openapi.client.JHApiClient;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author yanlongqi
* @date 2024/2/5 18:44
*/
public class JHJobApiExecution extends JHApiExecution {
/**
* 获取一个执行器的实例
*
* @param jhApiClient 请求的客户端
*/
public JHJobApiExecution(JHApiClient jhApiClient) {
super(jhApiClient);
}
/**
* 提交作业
*
* @param username 用户名
* @param appId 应用id
* @param params 作业提交参数
* @return 作业id
*/
public String submit(String username, String appId, Map<String, Object> params) {
if (StringUtils.isBlank(appId)) {
throw new ArgsException("appId不能为空");
}
if (CollectionUtil.isEmpty(params)) {
throw new ArgsException("params不能为空");
}
Map<String, Object> map = new HashMap<>(2);
map.put("appId", appId);
map.put("params", JSONUtil.toJsonStr(params));
String path = JHApiClient.getUrl(JobPathConstant.JOB_SUBMIT_PATH, map);
List<Map<String, String>> result = post(path, username, null, new TypeReference<ResponseResult<List<Map<String, String>>>>() {
});
if (CollectionUtil.isEmpty(result)) {
throw new ServiceException(path, 500, "作业提交返回为空!");
}
return result.get(0).get("jobid");
}
}

View File

@@ -0,0 +1,16 @@
package com.jhinno.sdk.openapi.api.job;
/**
* 作业相关接口路径
*
* @author yanlongqi
* @date 2024/2/5 18:44
*/
public class JobPathConstant {
/**
* 提交作业
*/
public static final String JOB_SUBMIT_PATH = "/ws/api/jobs/jsub";
}

View File

@@ -1,6 +1,8 @@
package com.jhinno.sdk.openapi.test.app;
import cn.hutool.core.lang.ConsoleTable;
import com.jhinno.sdk.openapi.api.app.*;
import com.jhinno.sdk.openapi.api.file.FileInfo;
import com.jhinno.sdk.openapi.test.JHClientConfig;
import org.junit.Test;
@@ -155,8 +157,15 @@ public class AppApiTest {
*/
@Test
public void testGetAppList() {
List<AppInfo> appList = jhAppApiExecution.getAppList("lqyan");
System.out.println(appList);
List<AppInfo> appList = jhAppApiExecution.getAppList("jhadmin");
ConsoleTable consoleTable = ConsoleTable.create();
consoleTable.setSBCMode(false);
consoleTable.addHeader("id", "name", "type", "category", "protocol");
for (AppInfo appInfo : appList) {
consoleTable.addBody(appInfo.getId(), appInfo.getName(), appInfo.getType(), appInfo.getCategory(), appInfo.getProtocol());
}
consoleTable.print();
}
@@ -165,8 +174,7 @@ public class AppApiTest {
*/
@Test
public void testGetAppUrl() {
AppUrlInfo appUrl = jhAppApiExecution.getAppUrl("lqyan", "myjobmana");
System.out.println(appUrl);
System.out.println(jhAppApiExecution.getAppUrl("jhadmin", "myjobmana"));
}
}

View File

@@ -0,0 +1,28 @@
package com.jhinno.sdk.openapi.test.job;
import com.jhinno.sdk.openapi.api.job.JHJobApiExecution;
import com.jhinno.sdk.openapi.test.JHClientConfig;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* @author yanlongqi
* @date 2024/2/5 18:56
*/
public class JobApiTest {
private static final JHJobApiExecution execution = new JHJobApiExecution(JHClientConfig.client);
/**
* 测试提交作业
*/
@Test
public void testSubmitJob() {
Map<String, Object> params = new HashMap<>();
params.put("JH_CAS", "$HOME/aaa.sh");
params.put("JH_NCPU", "1");
System.out.println(execution.submit("jhadmin", "common_sub", params));
}
}