Skip to content

Commit

Permalink
openapi service
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Nov 27, 2023
1 parent 69d406e commit 1299ff5
Show file tree
Hide file tree
Showing 24 changed files with 204 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@Configuration
@ComponentScan("cn.ponfee.disjob.test.handler")
@EnableJacksonDateConfigurer // 解决日期反序列化报错的问题
@EnableSupervisor // job-admin必须启用Supervisor角色,即:@EnableSupervisor注解是必须加的
@EnableSupervisor // disjob-admin必须启用Supervisor角色,即:必须加@EnableSupervisor注解
@EnableWorker // 若要取消worker角色可去掉@EnableWorker注解
public class DisjobAdminConfiguration implements WebMvcConfigurer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import cn.ponfee.disjob.supervisor.provider.openapi.request.SchedInstancePageRequest;
import cn.ponfee.disjob.supervisor.provider.openapi.response.SchedInstanceResponse;
import cn.ponfee.disjob.supervisor.provider.openapi.response.SchedTaskResponse;
import cn.ponfee.disjob.supervisor.service.SupervisorAggregator;
import cn.ponfee.disjob.supervisor.service.SupervisorOpenapiService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
Expand Down Expand Up @@ -44,10 +44,10 @@ public class DisjobInstanceController extends BaseController {
private static final int WAIT_SLEEP_ROUND = 9;
private static final long[] WAIT_SLEEP_MILLIS = {2500, 500};

private final SupervisorAggregator supervisorAggregator;
private final SupervisorOpenapiService supervisorOpenapiService;

public DisjobInstanceController(SupervisorAggregator supervisorAggregator) {
this.supervisorAggregator = supervisorAggregator;
public DisjobInstanceController(SupervisorOpenapiService supervisorOpenapiService) {
this.supervisorOpenapiService = supervisorOpenapiService;
}

@RequiresPermissions(PERMISSION_VIEW)
Expand All @@ -66,7 +66,7 @@ public TableDataInfo tree(SchedInstancePageRequest request) {
request.setParent(true);
request.setPageNumber(super.getPageNumber());
request.setPageSize(super.getPageSize());
return PageUtils.toTableDataInfo(supervisorAggregator.queryInstanceForPage(request));
return PageUtils.toTableDataInfo(supervisorOpenapiService.queryInstanceForPage(request));
}

/**
Expand All @@ -79,20 +79,20 @@ public TableDataInfo flat(SchedInstancePageRequest request) {
request.setParent(false);
request.setPageNumber(super.getPageNumber());
request.setPageSize(super.getPageSize());
return PageUtils.toTableDataInfo(supervisorAggregator.queryInstanceForPage(request));
return PageUtils.toTableDataInfo(supervisorOpenapiService.queryInstanceForPage(request));
}

@RequiresPermissions(PERMISSION_QUERY)
@PostMapping("/children")
@ResponseBody
public List<SchedInstanceResponse> children(@RequestParam("pnstanceId") Long pnstanceId) {
return supervisorAggregator.listInstanceChildren(pnstanceId);
return supervisorOpenapiService.listInstanceChildren(pnstanceId);
}

@RequiresPermissions(PERMISSION_QUERY)
@GetMapping("/tasks/{instanceId}")
public String tasks(@PathVariable("instanceId") Long instanceId, ModelMap mmap) {
List<SchedTaskResponse> tasks = supervisorAggregator.getInstanceTasks(instanceId);
List<SchedTaskResponse> tasks = supervisorOpenapiService.getInstanceTasks(instanceId);
mmap.put("tasks", Jsons.toJson(tasks));
return PREFIX + "/tasks";
}
Expand All @@ -107,7 +107,7 @@ public String tasks(@PathVariable("instanceId") Long instanceId, ModelMap mmap)
@PostMapping("/remove/{instanceId}")
@ResponseBody
public AjaxResult remove(@PathVariable("instanceId") Long instanceId) {
supervisorAggregator.deleteInstance(instanceId);
supervisorOpenapiService.deleteInstance(instanceId);
return success();
}

Expand All @@ -119,9 +119,9 @@ public AjaxResult remove(@PathVariable("instanceId") Long instanceId) {
@PostMapping("/pause/{instanceId}")
@ResponseBody
public AjaxResult pause(@PathVariable("instanceId") Long instanceId) {
supervisorAggregator.pauseInstance(instanceId);
supervisorOpenapiService.pauseInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
SchedInstanceResponse instance = supervisorAggregator.getInstance(instanceId, false);
SchedInstanceResponse instance = supervisorOpenapiService.getInstance(instanceId, false);
return !RunState.PAUSABLE_LIST.contains(RunState.of(instance.getRunState()));
});
return success();
Expand All @@ -135,9 +135,9 @@ public AjaxResult pause(@PathVariable("instanceId") Long instanceId) {
@PostMapping("/resume/{instanceId}")
@ResponseBody
public AjaxResult resume(@PathVariable("instanceId") Long instanceId) {
supervisorAggregator.resumeInstance(instanceId);
supervisorOpenapiService.resumeInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, new long[]{500, 200}, () -> {
SchedInstanceResponse instance = supervisorAggregator.getInstance(instanceId, false);
SchedInstanceResponse instance = supervisorOpenapiService.getInstance(instanceId, false);
return !RunState.PAUSED.equals(instance.getRunState());
});
return success();
Expand All @@ -151,9 +151,9 @@ public AjaxResult resume(@PathVariable("instanceId") Long instanceId) {
@PostMapping("/cancel/{instanceId}")
@ResponseBody
public AjaxResult cancel(@PathVariable("instanceId") Long instanceId) {
supervisorAggregator.cancelInstance(instanceId);
supervisorOpenapiService.cancelInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
SchedInstanceResponse instance = supervisorAggregator.getInstance(instanceId, false);
SchedInstanceResponse instance = supervisorOpenapiService.getInstance(instanceId, false);
return RunState.of(instance.getRunState()).isTerminal();
});
return success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import cn.ponfee.disjob.supervisor.provider.openapi.request.SchedJobPageRequest;
import cn.ponfee.disjob.supervisor.provider.openapi.request.UpdateSchedJobRequest;
import cn.ponfee.disjob.supervisor.provider.openapi.response.SchedJobResponse;
import cn.ponfee.disjob.supervisor.service.SupervisorAggregator;
import cn.ponfee.disjob.supervisor.service.SupervisorOpenapiService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
Expand Down Expand Up @@ -50,10 +50,10 @@ public class DisjobJobController extends BaseController {
private static final String PERMISSION_QUERY = "disjob:job:query";
private static final String PERMISSION_OPERATE = "disjob:job:operate";

private final SupervisorAggregator supervisorAggregator;
private final SupervisorOpenapiService supervisorOpenapiService;

public DisjobJobController(SupervisorAggregator supervisorAggregator) {
this.supervisorAggregator = supervisorAggregator;
public DisjobJobController(SupervisorOpenapiService supervisorOpenapiService) {
this.supervisorOpenapiService = supervisorOpenapiService;
}

// -------------------------------------------------------查询
Expand All @@ -73,7 +73,7 @@ public String job() {
public TableDataInfo list(SchedJobPageRequest request) {
request.setPageNumber(super.getPageNumber());
request.setPageSize(super.getPageSize());
PageResponse<SchedJobResponse> response = supervisorAggregator.queryJobForPage(request);
PageResponse<SchedJobResponse> response = supervisorOpenapiService.queryJobForPage(request);
return PageUtils.toTableDataInfo(response);
}

Expand All @@ -83,7 +83,7 @@ public TableDataInfo list(SchedJobPageRequest request) {
@RequiresPermissions(PERMISSION_QUERY)
@GetMapping("/detail/{jobId}")
public String detail(@PathVariable("jobId") long jobId, ModelMap mmap) {
SchedJobResponse job = supervisorAggregator.getJob(jobId);
SchedJobResponse job = supervisorOpenapiService.getJob(jobId);
Assert.notNull(job, () -> "Job id not found: " + jobId);
mmap.put("job", job);
return PREFIX + "/detail";
Expand All @@ -98,7 +98,7 @@ public String detail(@PathVariable("jobId") long jobId, ModelMap mmap) {
@ResponseBody
public AjaxResult export(SchedJobPageRequest request) {
request.setPaged(false);
List<SchedJobResponse> rows = supervisorAggregator.queryJobForPage(request).getRows();
List<SchedJobResponse> rows = supervisorOpenapiService.queryJobForPage(request).getRows();
List<SchedJobExport> list = Collects.convert(rows, SchedJobExport::ofSchedJobResponse);
ExcelUtil<SchedJobExport> excel = new ExcelUtil<>(SchedJobExport.class);
return excel.exportExcel(list, "调度配置数据");
Expand All @@ -121,7 +121,7 @@ public String add(ModelMap mmap) {
@RequiresPermissions(PERMISSION_OPERATE)
@GetMapping("/copy/{id}")
public String copy(@PathVariable("id") long jobId, ModelMap mmap) {
return toAdd(supervisorAggregator.getJob(jobId), mmap);
return toAdd(supervisorOpenapiService.getJob(jobId), mmap);
}

private String toAdd(SchedJobResponse job, ModelMap mmap) {
Expand All @@ -138,7 +138,7 @@ private String toAdd(SchedJobResponse job, ModelMap mmap) {
@ResponseBody
public AjaxResult doAdd(AddSchedJobRequest req) throws JobException {
req.setCreatedBy(getLoginName());
supervisorAggregator.addJob(req);
supervisorOpenapiService.addJob(req);
return success();
}

Expand All @@ -148,7 +148,7 @@ public AjaxResult doAdd(AddSchedJobRequest req) throws JobException {
@RequiresPermissions(PERMISSION_OPERATE)
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") long jobId, ModelMap mmap) {
SchedJobResponse job = supervisorAggregator.getJob(jobId);
SchedJobResponse job = supervisorOpenapiService.getJob(jobId);
Assert.notNull(job, () -> "Job id not found: " + jobId);
mmap.put("job", job);
return PREFIX + "/edit";
Expand All @@ -163,7 +163,7 @@ public String edit(@PathVariable("id") long jobId, ModelMap mmap) {
@ResponseBody
public AjaxResult doEdit(UpdateSchedJobRequest req) throws JobException {
req.setUpdatedBy(getLoginName());
supervisorAggregator.updateJob(req);
supervisorOpenapiService.updateJob(req);
return success();
}

Expand All @@ -182,7 +182,7 @@ public AjaxResult remove(@RequestParam("ids") String ids) {
if (jobIds.isEmpty()) {
return error("Job id不能为空");
}
jobIds.parallelStream().forEach(supervisorAggregator::deleteJob);
jobIds.parallelStream().forEach(supervisorOpenapiService::deleteJob);
return success();
}

Expand All @@ -195,7 +195,7 @@ public AjaxResult remove(@RequestParam("ids") String ids) {
@ResponseBody
public AjaxResult changeState(@RequestParam("jobId") long jobId,
@RequestParam("toState") Integer toState) {
boolean result = supervisorAggregator.changeJobState(jobId, toState);
boolean result = supervisorOpenapiService.changeJobState(jobId, toState);
return toAjax(result);
}

Expand All @@ -207,7 +207,7 @@ public AjaxResult changeState(@RequestParam("jobId") long jobId,
@PostMapping("/trigger")
@ResponseBody
public AjaxResult trigger(@RequestParam("jobId") long jobId) throws JobException {
supervisorAggregator.triggerJob(jobId);
supervisorOpenapiService.triggerJob(jobId);
return success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public <T> T executeInTransaction(ThrowingFunction<ThrowingFunction<String, Prep
});
}

public void createTableIfNotExists(String tableName, String createTableDdl) {
public void createTableIfNotExists(String tableName0, String createTableDdl) {
String tableName = tableName0.toLowerCase();
if (EXISTS_TABLE.contains(tableName)) {
return;
}
Expand Down
31 changes: 15 additions & 16 deletions disjob-common/src/main/java/cn/ponfee/disjob/common/util/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public final class Bytes {

// -----------------------------------------------------------------hexEncode/hexDecode

public static void hexEncode(char[] charArray, int i, byte b) {
public static void encodeHex(char[] charArray, int i, byte b) {
charArray[ i] = HEX_LOWER_CODES[(0xF0 & b) >>> 4];
charArray[++i] = HEX_LOWER_CODES[ 0x0F & b ];
}

public static String hexEncode(byte b, boolean lowercase) {
public static String encodeHex(byte b, boolean lowercase) {
char[] codes = lowercase ? HEX_LOWER_CODES : HEX_UPPER_CODES;
return new String(new char[] {
codes[(0xF0 & b) >>> 4], codes[0x0F & b]
});
}

public static String hexEncode(byte[] bytes) {
return hexEncode(bytes, true);
public static String encodeHex(byte[] bytes) {
return encodeHex(bytes, true);
}

/**
Expand All @@ -52,13 +52,11 @@ public static String hexEncode(byte[] bytes) {
* @param lowercase the boolean
* @return string
*/
public static String hexEncode(byte[] bytes, boolean lowercase) {
public static String encodeHex(byte[] bytes, boolean lowercase) {
//new BigInteger(1, bytes).toString(16);
int len = bytes.length;
char[] out = new char[len << 1];

char[] codes = lowercase ? HEX_LOWER_CODES : HEX_UPPER_CODES;

// one byte -> two char
for (int i = 0, j = 0; i < len; i++) {
out[j++] = codes[(0xF0 & bytes[i]) >>> 4];
Expand All @@ -68,23 +66,22 @@ public static String hexEncode(byte[] bytes, boolean lowercase) {
}

/**
* decode the hex string to byte array
* Decode hex string to byte array
*
* @param hex
* @return
* @param hex the hex string
* @return byte array
*/
public static byte[] hexDecode(String hex) {
char[] data = hex.toCharArray();
int len = data.length;
public static byte[] decodeHex(String hex) {
int len = hex.length();
if ((len & 0x01) == 1) {
throw new IllegalArgumentException("Invalid hex string.");
throw new IllegalArgumentException("Hex string must be twice length.");
}

byte[] out = new byte[len >> 1];

// two char -> one byte
for (int i = 0, j = 0; j < len; i++, j += 2) {
out[i] = (byte) ( Character.digit(data[j], 16) << 4 | Character.digit(data[j + 1], 16) );
char c1 = hex.charAt(j), c2 = hex.charAt(j + 1);
out[i] = (byte) (Character.digit(c1, 16) << 4 | Character.digit(c2, 16));
}
return out;
}
Expand Down Expand Up @@ -173,6 +170,7 @@ public static byte[] toBytes(char[] chars, Charset charset) {
}

// -----------------------------------------------------------------char

public static byte[] toBytes(char value) {
return new byte[]{(byte) (value >>> 8), (byte) value};
}
Expand All @@ -189,6 +187,7 @@ public static char toChar(byte[] bytes, int fromIdx) {
}

// -----------------------------------------------------------------int

public static byte[] toBytes(int value) {
byte[] bytes = new byte[4];
put(value, bytes, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public static String getPackagePath(Class<?> clazz) {
}

// -----------------------------------------------------------------------------constructor & instance

@SuppressWarnings("unchecked")
public static <T> Constructor<T> getConstructor(Class<T> type, Class<?>... parameterTypes) {
boolean noArgs = ArrayUtils.isEmpty(parameterTypes);
Expand Down Expand Up @@ -320,6 +321,7 @@ public static <T> T newInstance(Class<T> type, Object[] args) {
}

// -------------------------------------------------------------------------------------------method & invoke

public static Method getMethod(Object caller, String methodName, Class<?>... parameterTypes) {
Tuple2<Class<?>, Predicates> tuple = obtainClass(caller);
Class<?> type = tuple.a;
Expand Down Expand Up @@ -572,4 +574,5 @@ private static String toString(Class<?>[] parameterTypes) {
? "()"
: "(" + Joiner.on(", ").join(parameterTypes) + ")";
}

}
Loading

0 comments on commit 1299ff5

Please sign in to comment.