feat: update powerjob client,copy job and workflow are supported

This commit is contained in:
Echo009 2021-03-04 15:10:44 +08:00
parent 0c424b52df
commit 59e3fee086
5 changed files with 170 additions and 62 deletions

View File

@ -121,6 +121,22 @@ public class OhMyClient {
return JSON.parseObject(post, LONG_RESULT_TYPE); return JSON.parseObject(post, LONG_RESULT_TYPE);
} }
/**
* Copy one Job
*
* @param jobId Job id
* @return Id of job copy
*/
public ResultDTO<Long> copyJob(Long jobId) {
RequestBody body = new FormBody.Builder()
.add("jobId", jobId.toString())
.add("appId", appId.toString())
.build();
String post = postHA(OpenAPIConstant.COPY_JOB, body);
return JSON.parseObject(post, LONG_RESULT_TYPE);
}
/** /**
* Query JobInfo by jobId * Query JobInfo by jobId
* *
@ -328,6 +344,22 @@ public class OhMyClient {
return JSON.parseObject(post, LONG_RESULT_TYPE); return JSON.parseObject(post, LONG_RESULT_TYPE);
} }
/**
* Copy one workflow
*
* @param workflowId Workflow id
* @return Id of workflow copy
*/
public ResultDTO<Long> copyWorkflow(Long workflowId) {
RequestBody body = new FormBody.Builder()
.add("workflowId", workflowId.toString())
.add("appId", appId.toString())
.build();
String post = postHA(OpenAPIConstant.COPY_WORKFLOW, body);
return JSON.parseObject(post, LONG_RESULT_TYPE);
}
/** /**
* 保存工作流 DAG * 保存工作流 DAG
* *

View File

@ -6,8 +6,11 @@ import com.github.kfcfans.powerjob.common.ExecuteType;
import com.github.kfcfans.powerjob.common.ProcessorType; import com.github.kfcfans.powerjob.common.ProcessorType;
import com.github.kfcfans.powerjob.common.TimeExpressionType; import com.github.kfcfans.powerjob.common.TimeExpressionType;
import com.github.kfcfans.powerjob.common.request.http.SaveJobInfoRequest; import com.github.kfcfans.powerjob.common.request.http.SaveJobInfoRequest;
import com.github.kfcfans.powerjob.common.response.InstanceInfoDTO;
import com.github.kfcfans.powerjob.common.response.JobInfoDTO; import com.github.kfcfans.powerjob.common.response.JobInfoDTO;
import com.github.kfcfans.powerjob.common.response.ResultDTO; import com.github.kfcfans.powerjob.common.response.ResultDTO;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -16,6 +19,7 @@ import java.util.concurrent.TimeUnit;
* Test cases for {@link OhMyClient} * Test cases for {@link OhMyClient}
* *
* @author tjq * @author tjq
* @author Echo009
* @since 2020/4/15 * @since 2020/4/15
*/ */
class TestClient extends ClientInitializer { class TestClient extends ClientInitializer {
@ -23,12 +27,12 @@ class TestClient extends ClientInitializer {
public static final long JOB_ID = 4L; public static final long JOB_ID = 4L;
@Test @Test
public void testSaveJob() throws Exception { void testSaveJob() {
SaveJobInfoRequest newJobInfo = new SaveJobInfoRequest(); SaveJobInfoRequest newJobInfo = new SaveJobInfoRequest();
newJobInfo.setId(JOB_ID); newJobInfo.setId(JOB_ID);
newJobInfo.setJobName("omsOpenAPIJobccccc"); newJobInfo.setJobName("omsOpenAPIJobccccc");
newJobInfo.setJobDescription("tes OpenAPI"); newJobInfo.setJobDescription("test OpenAPI");
newJobInfo.setJobParams("{'aa':'bb'}"); newJobInfo.setJobParams("{'aa':'bb'}");
newJobInfo.setTimeExpressionType(TimeExpressionType.CRON); newJobInfo.setTimeExpressionType(TimeExpressionType.CRON);
newJobInfo.setTimeExpression("0 0 * * * ? "); newJobInfo.setTimeExpression("0 0 * * * ? ");
@ -43,64 +47,91 @@ class TestClient extends ClientInitializer {
ResultDTO<Long> resultDTO = ohMyClient.saveJob(newJobInfo); ResultDTO<Long> resultDTO = ohMyClient.saveJob(newJobInfo);
System.out.println(JSONObject.toJSONString(resultDTO)); System.out.println(JSONObject.toJSONString(resultDTO));
Assertions.assertNotNull(resultDTO);
} }
@Test @Test
public void testFetchJob() throws Exception { void testCopyJob() {
ResultDTO<Long> copyJobRes = ohMyClient.copyJob(JOB_ID);
System.out.println(JSONObject.toJSONString(copyJobRes));
Assertions.assertNotNull(copyJobRes);
}
@Test
void testFetchJob() {
ResultDTO<JobInfoDTO> fetchJob = ohMyClient.fetchJob(JOB_ID); ResultDTO<JobInfoDTO> fetchJob = ohMyClient.fetchJob(JOB_ID);
System.out.println(JSONObject.toJSONString(fetchJob)); System.out.println(JSONObject.toJSONString(fetchJob));
Assertions.assertNotNull(fetchJob);
} }
@Test @Test
public void testDisableJob() throws Exception { void testDisableJob() {
System.out.println(ohMyClient.disableJob(JOB_ID)); ResultDTO<Void> res = ohMyClient.disableJob(JOB_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
public void testEnableJob() throws Exception { void testEnableJob() {
System.out.println(ohMyClient.enableJob(JOB_ID)); ResultDTO<Void> res = ohMyClient.enableJob(JOB_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
public void testDeleteJob() throws Exception { void testDeleteJob() {
System.out.println(ohMyClient.deleteJob(JOB_ID)); ResultDTO<Void> res = ohMyClient.deleteJob(JOB_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
public void testRun() { void testRun() {
System.out.println(ohMyClient.runJob(JOB_ID)); ResultDTO<Long> res = ohMyClient.runJob(JOB_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
public void testRunJobDelay() throws Exception { void testRunJobDelay() {
System.out.println(ohMyClient.runJob(JOB_ID, "this is instanceParams", 60000)); ResultDTO<Long> res = ohMyClient.runJob(JOB_ID, "this is instanceParams", 60000);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
public void testFetchInstanceInfo() throws Exception { void testFetchInstanceInfo() {
System.out.println(ohMyClient.fetchInstanceInfo(205436386851946560L)); ResultDTO<InstanceInfoDTO> res = ohMyClient.fetchInstanceInfo(205436386851946560L);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
public void testStopInstance() throws Exception { void testStopInstance() {
ResultDTO<Void> res = ohMyClient.stopInstance(205436995885858880L); ResultDTO<Void> res = ohMyClient.stopInstance(205436995885858880L);
System.out.println(res.toString()); System.out.println(res);
} Assertions.assertNotNull(res);
@Test
public void testFetchInstanceStatus() throws Exception {
System.out.println(ohMyClient.fetchInstanceStatus(205436995885858880L));
} }
@Test @Test
public void testCancelInstanceInTimeWheel() throws Exception { void testFetchInstanceStatus() {
ResultDTO<Integer> res = ohMyClient.fetchInstanceStatus(205436995885858880L);
System.out.println(res);
Assertions.assertNotNull(res);
}
@Test
void testCancelInstanceInTimeWheel() {
ResultDTO<Long> startRes = ohMyClient.runJob(JOB_ID, "start by OhMyClient", 20000); ResultDTO<Long> startRes = ohMyClient.runJob(JOB_ID, "start by OhMyClient", 20000);
System.out.println("runJob result: " + JSONObject.toJSONString(startRes)); System.out.println("runJob result: " + JSONObject.toJSONString(startRes));
ResultDTO<Void> cancelRes = ohMyClient.cancelInstance(startRes.getData()); ResultDTO<Void> cancelRes = ohMyClient.cancelInstance(startRes.getData());
System.out.println("cancelJob result: " + JSONObject.toJSONString(cancelRes)); System.out.println("cancelJob result: " + JSONObject.toJSONString(cancelRes));
Assertions.assertTrue(cancelRes.isSuccess());
} }
@Test @Test
public void testCancelInstanceInDatabase() throws Exception { @SneakyThrows
void testCancelInstanceInDatabase() {
ResultDTO<Long> startRes = ohMyClient.runJob(15L, "start by OhMyClient", 2000000); ResultDTO<Long> startRes = ohMyClient.runJob(15L, "start by OhMyClient", 2000000);
System.out.println("runJob result: " + JSONObject.toJSONString(startRes)); System.out.println("runJob result: " + JSONObject.toJSONString(startRes));
@ -109,11 +140,13 @@ class TestClient extends ClientInitializer {
ResultDTO<Void> cancelRes = ohMyClient.cancelInstance(startRes.getData()); ResultDTO<Void> cancelRes = ohMyClient.cancelInstance(startRes.getData());
System.out.println("cancelJob result: " + JSONObject.toJSONString(cancelRes)); System.out.println("cancelJob result: " + JSONObject.toJSONString(cancelRes));
Assertions.assertTrue(cancelRes.isSuccess());
} }
@Test @Test
public void testRetryInstance() throws Exception { void testRetryInstance() {
ResultDTO<Void> res = ohMyClient.retryInstance(169557545206153344L); ResultDTO<Void> res = ohMyClient.retryInstance(169557545206153344L);
System.out.println(res); System.out.println(res);
Assertions.assertNotNull(res);
} }
} }

View File

@ -7,7 +7,12 @@ import com.github.kfcfans.powerjob.common.ProcessorType;
import com.github.kfcfans.powerjob.common.TimeExpressionType; import com.github.kfcfans.powerjob.common.TimeExpressionType;
import com.github.kfcfans.powerjob.common.model.PEWorkflowDAG; import com.github.kfcfans.powerjob.common.model.PEWorkflowDAG;
import com.github.kfcfans.powerjob.common.request.http.*; import com.github.kfcfans.powerjob.common.request.http.*;
import com.github.kfcfans.powerjob.common.response.ResultDTO;
import com.github.kfcfans.powerjob.common.response.WorkflowInfoDTO;
import com.github.kfcfans.powerjob.common.response.WorkflowInstanceInfoDTO;
import com.github.kfcfans.powerjob.common.response.WorkflowNodeInfoDTO;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
@ -16,6 +21,7 @@ import java.util.List;
* Test cases for {@link OhMyClient} workflow. * Test cases for {@link OhMyClient} workflow.
* *
* @author tjq * @author tjq
* @author Echo009
* @since 2020/6/2 * @since 2020/6/2
*/ */
class TestWorkflow extends ClientInitializer { class TestWorkflow extends ClientInitializer {
@ -23,7 +29,7 @@ class TestWorkflow extends ClientInitializer {
private static final long WF_ID = 1; private static final long WF_ID = 1;
@Test @Test
void initTestData() throws Exception { void initTestData() {
SaveJobInfoRequest base = new SaveJobInfoRequest(); SaveJobInfoRequest base = new SaveJobInfoRequest();
base.setJobName("DAG-Node-"); base.setJobName("DAG-Node-");
base.setTimeExpressionType(TimeExpressionType.WORKFLOW); base.setTimeExpressionType(TimeExpressionType.WORKFLOW);
@ -34,13 +40,15 @@ class TestWorkflow extends ClientInitializer {
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
SaveJobInfoRequest request = JSONObject.parseObject(JSONObject.toJSONBytes(base), SaveJobInfoRequest.class); SaveJobInfoRequest request = JSONObject.parseObject(JSONObject.toJSONBytes(base), SaveJobInfoRequest.class);
request.setJobName(request.getJobName() + i); request.setJobName(request.getJobName() + i);
System.out.println(ohMyClient.saveJob(request)); ResultDTO<Long> res = ohMyClient.saveJob(request);
System.out.println(res);
Assertions.assertNotNull(res);
} }
} }
@Test @Test
void testSaveWorkflow() throws Exception { void testSaveWorkflow() {
SaveWorkflowRequest req = new SaveWorkflowRequest(); SaveWorkflowRequest req = new SaveWorkflowRequest();
@ -50,16 +58,27 @@ class TestWorkflow extends ClientInitializer {
req.setTimeExpressionType(TimeExpressionType.API); req.setTimeExpressionType(TimeExpressionType.API);
System.out.println("req ->" + JSONObject.toJSON(req)); System.out.println("req ->" + JSONObject.toJSON(req));
System.out.println(ohMyClient.saveWorkflow(req)); ResultDTO<Long> res = ohMyClient.saveWorkflow(req);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test
void testCopyWorkflow() {
ResultDTO<Long> res = ohMyClient.copyWorkflow(WF_ID);
System.out.println(res);
Assertions.assertNotNull(res);
}
@Test @Test
void testAddWorkflowNode() { void testAddWorkflowNode() {
AddWorkflowNodeRequest addWorkflowNodeRequest = new AddWorkflowNodeRequest(); AddWorkflowNodeRequest addWorkflowNodeRequest = new AddWorkflowNodeRequest();
addWorkflowNodeRequest.setJobId(1L); addWorkflowNodeRequest.setJobId(1L);
addWorkflowNodeRequest.setWorkflowId(WF_ID); addWorkflowNodeRequest.setWorkflowId(WF_ID);
System.out.println(ohMyClient.addWorkflowNode(Lists.newArrayList(addWorkflowNodeRequest))); ResultDTO<List<WorkflowNodeInfoDTO>> res = ohMyClient.addWorkflowNode(Lists.newArrayList(addWorkflowNodeRequest));
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
@ -70,7 +89,9 @@ class TestWorkflow extends ClientInitializer {
modifyWorkflowNodeRequest.setNodeAlias("(๑•̀ㅂ•́)و✧"); modifyWorkflowNodeRequest.setNodeAlias("(๑•̀ㅂ•́)و✧");
modifyWorkflowNodeRequest.setEnable(false); modifyWorkflowNodeRequest.setEnable(false);
modifyWorkflowNodeRequest.setSkipWhenFailed(false); modifyWorkflowNodeRequest.setSkipWhenFailed(false);
System.out.println(ohMyClient.modifyWorkflowNode(modifyWorkflowNodeRequest)); ResultDTO<Void> res = ohMyClient.modifyWorkflowNode(modifyWorkflowNodeRequest);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
@ -91,58 +112,79 @@ class TestWorkflow extends ClientInitializer {
SaveWorkflowDAGRequest saveWorkflowDAGRequest = new SaveWorkflowDAGRequest(); SaveWorkflowDAGRequest saveWorkflowDAGRequest = new SaveWorkflowDAGRequest();
saveWorkflowDAGRequest.setId(WF_ID); saveWorkflowDAGRequest.setId(WF_ID);
saveWorkflowDAGRequest.setDag(peWorkflowDAG); saveWorkflowDAGRequest.setDag(peWorkflowDAG);
ResultDTO<Void> res = ohMyClient.saveWorkflowDag(saveWorkflowDAGRequest);
System.out.println(ohMyClient.saveWorkflowDag(saveWorkflowDAGRequest)); System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testDisableWorkflow() throws Exception { void testDisableWorkflow() {
System.out.println(ohMyClient.disableWorkflow(WF_ID)); ResultDTO<Void> res = ohMyClient.disableWorkflow(WF_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testDeleteWorkflow() throws Exception { void testDeleteWorkflow() {
System.out.println(ohMyClient.deleteWorkflow(WF_ID)); ResultDTO<Void> res = ohMyClient.deleteWorkflow(WF_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testEnableWorkflow() throws Exception { void testEnableWorkflow() {
System.out.println(ohMyClient.enableWorkflow(WF_ID)); ResultDTO<Void> res = ohMyClient.enableWorkflow(WF_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testFetchWorkflowInfo() throws Exception { void testFetchWorkflowInfo() {
System.out.println(ohMyClient.fetchWorkflow(WF_ID)); ResultDTO<WorkflowInfoDTO> res = ohMyClient.fetchWorkflow(WF_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testRunWorkflow() throws Exception { void testRunWorkflow() {
System.out.println(ohMyClient.runWorkflow(WF_ID)); ResultDTO<Long> res = ohMyClient.runWorkflow(WF_ID);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testStopWorkflowInstance() throws Exception { void testStopWorkflowInstance() {
System.out.println(ohMyClient.stopWorkflowInstance(149962433421639744L)); ResultDTO<Void> res = ohMyClient.stopWorkflowInstance(149962433421639744L);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testRetryWorkflowInstance() { void testRetryWorkflowInstance() {
System.out.println(ohMyClient.retryWorkflowInstance(149962433421639744L)); ResultDTO<Void> res = ohMyClient.retryWorkflowInstance(149962433421639744L);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testMarkWorkflowNodeAsSuccess() { void testMarkWorkflowNodeAsSuccess() {
System.out.println(ohMyClient.markWorkflowNodeAsSuccess(149962433421639744L, 1L)); ResultDTO<Void> res = ohMyClient.markWorkflowNodeAsSuccess(149962433421639744L, 1L);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testFetchWfInstanceInfo() throws Exception { void testFetchWfInstanceInfo() {
System.out.println(ohMyClient.fetchWorkflowInstanceInfo(149962433421639744L)); ResultDTO<WorkflowInstanceInfoDTO> res = ohMyClient.fetchWorkflowInstanceInfo(149962433421639744L);
System.out.println(res);
Assertions.assertNotNull(res);
} }
@Test @Test
void testRunWorkflowPlus() throws Exception { void testRunWorkflowPlus() {
System.out.println(ohMyClient.runWorkflow(WF_ID, "this is init Params 2", 90000)); ResultDTO<Long> res = ohMyClient.runWorkflow(WF_ID, "this is init Params 2", 90000);
System.out.println(res);
Assertions.assertNotNull(res);
} }
} }

View File

@ -104,6 +104,9 @@ public class JobService {
public JobInfoDO copyJob(Long jobId) { public JobInfoDO copyJob(Long jobId) {
JobInfoDO origin = jobInfoRepository.findById(jobId).orElseThrow(() -> new IllegalArgumentException("can't find job by jobId: " + jobId)); JobInfoDO origin = jobInfoRepository.findById(jobId).orElseThrow(() -> new IllegalArgumentException("can't find job by jobId: " + jobId));
if (origin.getStatus() == SwitchableStatus.DELETED.getV()) {
throw new IllegalStateException("can't copy the job which has been deleted!");
}
JobInfoDO copyJob = new JobInfoDO(); JobInfoDO copyJob = new JobInfoDO();
// 值拷贝 // 值拷贝
BeanUtils.copyProperties(origin, copyJob); BeanUtils.copyProperties(origin, copyJob);

View File

@ -3,21 +3,19 @@ package com.github.kfcfans.powerjob.server.web.controller;
import com.github.kfcfans.powerjob.common.InstanceStatus; import com.github.kfcfans.powerjob.common.InstanceStatus;
import com.github.kfcfans.powerjob.common.OpenAPIConstant; import com.github.kfcfans.powerjob.common.OpenAPIConstant;
import com.github.kfcfans.powerjob.common.PowerQuery; import com.github.kfcfans.powerjob.common.PowerQuery;
import com.github.kfcfans.powerjob.common.request.http.SaveWorkflowRequest; import com.github.kfcfans.powerjob.common.request.http.*;
import com.github.kfcfans.powerjob.common.request.query.JobInfoQuery; import com.github.kfcfans.powerjob.common.request.query.JobInfoQuery;
import com.github.kfcfans.powerjob.common.response.InstanceInfoDTO;
import com.github.kfcfans.powerjob.common.response.JobInfoDTO;
import com.github.kfcfans.powerjob.common.response.ResultDTO;
import com.github.kfcfans.powerjob.common.response.WorkflowInstanceInfoDTO;
import com.github.kfcfans.powerjob.server.persistence.core.model.WorkflowNodeInfoDO; import com.github.kfcfans.powerjob.server.persistence.core.model.WorkflowNodeInfoDO;
import com.github.kfcfans.powerjob.server.service.AppInfoService; import com.github.kfcfans.powerjob.server.service.AppInfoService;
import com.github.kfcfans.powerjob.server.service.CacheService; import com.github.kfcfans.powerjob.server.service.CacheService;
import com.github.kfcfans.powerjob.server.service.JobService; import com.github.kfcfans.powerjob.server.service.JobService;
import com.github.kfcfans.powerjob.server.service.instance.InstanceService; import com.github.kfcfans.powerjob.server.service.instance.InstanceService;
import com.github.kfcfans.powerjob.common.request.http.SaveJobInfoRequest;
import com.github.kfcfans.powerjob.server.service.workflow.WorkflowInstanceService; import com.github.kfcfans.powerjob.server.service.workflow.WorkflowInstanceService;
import com.github.kfcfans.powerjob.server.service.workflow.WorkflowService; import com.github.kfcfans.powerjob.server.service.workflow.WorkflowService;
import com.github.kfcfans.powerjob.common.response.*;
import com.github.kfcfans.powerjob.common.request.http.AddWorkflowNodeRequest;
import com.github.kfcfans.powerjob.common.request.http.ModifyWorkflowNodeRequest;
import com.github.kfcfans.powerjob.common.request.http.SaveWorkflowDAGRequest;
import com.github.kfcfans.powerjob.server.web.response.JobInfoVO;
import com.github.kfcfans.powerjob.server.web.response.WorkflowInfoVO; import com.github.kfcfans.powerjob.server.web.response.WorkflowInfoVO;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -66,8 +64,8 @@ public class OpenAPIController {
} }
@PostMapping(OpenAPIConstant.COPY_JOB) @PostMapping(OpenAPIConstant.COPY_JOB)
public ResultDTO<JobInfoVO> copyJob(Long jobId) { public ResultDTO<Long> copyJob(Long jobId) {
return ResultDTO.success(JobInfoVO.from(jobService.copyJob(jobId))); return ResultDTO.success(jobService.copyJob(jobId).getId());
} }
@PostMapping(OpenAPIConstant.FETCH_JOB) @PostMapping(OpenAPIConstant.FETCH_JOB)