diff --git a/powerjob-client/src/main/java/com/github/kfcfans/powerjob/client/OhMyClient.java b/powerjob-client/src/main/java/com/github/kfcfans/powerjob/client/OhMyClient.java index 540de951..7caf90d5 100644 --- a/powerjob-client/src/main/java/com/github/kfcfans/powerjob/client/OhMyClient.java +++ b/powerjob-client/src/main/java/com/github/kfcfans/powerjob/client/OhMyClient.java @@ -121,6 +121,22 @@ public class OhMyClient { return JSON.parseObject(post, LONG_RESULT_TYPE); } + + /** + * Copy one Job + * + * @param jobId Job id + * @return Id of job copy + */ + public ResultDTO 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 * @@ -328,6 +344,22 @@ public class OhMyClient { return JSON.parseObject(post, LONG_RESULT_TYPE); } + /** + * Copy one workflow + * + * @param workflowId Workflow id + * @return Id of workflow copy + */ + public ResultDTO 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 * @@ -493,10 +525,10 @@ public class OhMyClient { * mark the workflow node as success * * @param wfInstanceId workflow instanceId - * @param nodeId node id + * @param nodeId node id * @return Standard return object */ - public ResultDTO markWorkflowNodeAsSuccess(Long wfInstanceId,Long nodeId) { + public ResultDTO markWorkflowNodeAsSuccess(Long wfInstanceId, Long nodeId) { RequestBody body = new FormBody.Builder() .add("wfInstanceId", wfInstanceId.toString()) .add("nodeId", nodeId.toString()) diff --git a/powerjob-client/src/test/java/com/github/kfcfans/powerjob/client/test/TestClient.java b/powerjob-client/src/test/java/com/github/kfcfans/powerjob/client/test/TestClient.java index f918537a..63d383e6 100644 --- a/powerjob-client/src/test/java/com/github/kfcfans/powerjob/client/test/TestClient.java +++ b/powerjob-client/src/test/java/com/github/kfcfans/powerjob/client/test/TestClient.java @@ -6,8 +6,11 @@ import com.github.kfcfans.powerjob.common.ExecuteType; import com.github.kfcfans.powerjob.common.ProcessorType; import com.github.kfcfans.powerjob.common.TimeExpressionType; 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.ResultDTO; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.concurrent.TimeUnit; @@ -16,6 +19,7 @@ import java.util.concurrent.TimeUnit; * Test cases for {@link OhMyClient} * * @author tjq + * @author Echo009 * @since 2020/4/15 */ class TestClient extends ClientInitializer { @@ -23,12 +27,12 @@ class TestClient extends ClientInitializer { public static final long JOB_ID = 4L; @Test - public void testSaveJob() throws Exception { + void testSaveJob() { SaveJobInfoRequest newJobInfo = new SaveJobInfoRequest(); newJobInfo.setId(JOB_ID); newJobInfo.setJobName("omsOpenAPIJobccccc"); - newJobInfo.setJobDescription("tes OpenAPI"); + newJobInfo.setJobDescription("test OpenAPI"); newJobInfo.setJobParams("{'aa':'bb'}"); newJobInfo.setTimeExpressionType(TimeExpressionType.CRON); newJobInfo.setTimeExpression("0 0 * * * ? "); @@ -43,64 +47,91 @@ class TestClient extends ClientInitializer { ResultDTO resultDTO = ohMyClient.saveJob(newJobInfo); System.out.println(JSONObject.toJSONString(resultDTO)); + Assertions.assertNotNull(resultDTO); } @Test - public void testFetchJob() throws Exception { + void testCopyJob() { + ResultDTO copyJobRes = ohMyClient.copyJob(JOB_ID); + System.out.println(JSONObject.toJSONString(copyJobRes)); + Assertions.assertNotNull(copyJobRes); + } + + @Test + void testFetchJob() { ResultDTO fetchJob = ohMyClient.fetchJob(JOB_ID); System.out.println(JSONObject.toJSONString(fetchJob)); + Assertions.assertNotNull(fetchJob); } @Test - public void testDisableJob() throws Exception { - System.out.println(ohMyClient.disableJob(JOB_ID)); + void testDisableJob() { + ResultDTO res = ohMyClient.disableJob(JOB_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - public void testEnableJob() throws Exception { - System.out.println(ohMyClient.enableJob(JOB_ID)); + void testEnableJob() { + ResultDTO res = ohMyClient.enableJob(JOB_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - public void testDeleteJob() throws Exception { - System.out.println(ohMyClient.deleteJob(JOB_ID)); + void testDeleteJob() { + ResultDTO res = ohMyClient.deleteJob(JOB_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - public void testRun() { - System.out.println(ohMyClient.runJob(JOB_ID)); + void testRun() { + ResultDTO res = ohMyClient.runJob(JOB_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - public void testRunJobDelay() throws Exception { - System.out.println(ohMyClient.runJob(JOB_ID, "this is instanceParams", 60000)); + void testRunJobDelay() { + ResultDTO res = ohMyClient.runJob(JOB_ID, "this is instanceParams", 60000); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - public void testFetchInstanceInfo() throws Exception { - System.out.println(ohMyClient.fetchInstanceInfo(205436386851946560L)); + void testFetchInstanceInfo() { + ResultDTO res = ohMyClient.fetchInstanceInfo(205436386851946560L); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - public void testStopInstance() throws Exception { + void testStopInstance() { ResultDTO res = ohMyClient.stopInstance(205436995885858880L); - System.out.println(res.toString()); - } - @Test - public void testFetchInstanceStatus() throws Exception { - System.out.println(ohMyClient.fetchInstanceStatus(205436995885858880L)); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - public void testCancelInstanceInTimeWheel() throws Exception { + void testFetchInstanceStatus() { + ResultDTO res = ohMyClient.fetchInstanceStatus(205436995885858880L); + System.out.println(res); + Assertions.assertNotNull(res); + } + + @Test + void testCancelInstanceInTimeWheel() { ResultDTO startRes = ohMyClient.runJob(JOB_ID, "start by OhMyClient", 20000); System.out.println("runJob result: " + JSONObject.toJSONString(startRes)); ResultDTO cancelRes = ohMyClient.cancelInstance(startRes.getData()); System.out.println("cancelJob result: " + JSONObject.toJSONString(cancelRes)); + Assertions.assertTrue(cancelRes.isSuccess()); } @Test - public void testCancelInstanceInDatabase() throws Exception { + @SneakyThrows + void testCancelInstanceInDatabase() { ResultDTO startRes = ohMyClient.runJob(15L, "start by OhMyClient", 2000000); System.out.println("runJob result: " + JSONObject.toJSONString(startRes)); @@ -109,11 +140,13 @@ class TestClient extends ClientInitializer { ResultDTO cancelRes = ohMyClient.cancelInstance(startRes.getData()); System.out.println("cancelJob result: " + JSONObject.toJSONString(cancelRes)); + Assertions.assertTrue(cancelRes.isSuccess()); } @Test - public void testRetryInstance() throws Exception { + void testRetryInstance() { ResultDTO res = ohMyClient.retryInstance(169557545206153344L); System.out.println(res); + Assertions.assertNotNull(res); } } diff --git a/powerjob-client/src/test/java/com/github/kfcfans/powerjob/client/test/TestWorkflow.java b/powerjob-client/src/test/java/com/github/kfcfans/powerjob/client/test/TestWorkflow.java index 76e12ec7..ccc74c94 100644 --- a/powerjob-client/src/test/java/com/github/kfcfans/powerjob/client/test/TestWorkflow.java +++ b/powerjob-client/src/test/java/com/github/kfcfans/powerjob/client/test/TestWorkflow.java @@ -7,7 +7,12 @@ import com.github.kfcfans.powerjob.common.ProcessorType; import com.github.kfcfans.powerjob.common.TimeExpressionType; import com.github.kfcfans.powerjob.common.model.PEWorkflowDAG; 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 org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.List; @@ -16,6 +21,7 @@ import java.util.List; * Test cases for {@link OhMyClient} workflow. * * @author tjq + * @author Echo009 * @since 2020/6/2 */ class TestWorkflow extends ClientInitializer { @@ -23,7 +29,7 @@ class TestWorkflow extends ClientInitializer { private static final long WF_ID = 1; @Test - void initTestData() throws Exception { + void initTestData() { SaveJobInfoRequest base = new SaveJobInfoRequest(); base.setJobName("DAG-Node-"); base.setTimeExpressionType(TimeExpressionType.WORKFLOW); @@ -34,13 +40,15 @@ class TestWorkflow extends ClientInitializer { for (int i = 0; i < 5; i++) { SaveJobInfoRequest request = JSONObject.parseObject(JSONObject.toJSONBytes(base), SaveJobInfoRequest.class); request.setJobName(request.getJobName() + i); - System.out.println(ohMyClient.saveJob(request)); + ResultDTO res = ohMyClient.saveJob(request); + System.out.println(res); + Assertions.assertNotNull(res); + } } @Test - void testSaveWorkflow() throws Exception { - + void testSaveWorkflow() { SaveWorkflowRequest req = new SaveWorkflowRequest(); @@ -50,16 +58,27 @@ class TestWorkflow extends ClientInitializer { req.setTimeExpressionType(TimeExpressionType.API); System.out.println("req ->" + JSONObject.toJSON(req)); - System.out.println(ohMyClient.saveWorkflow(req)); + ResultDTO res = ohMyClient.saveWorkflow(req); + System.out.println(res); + Assertions.assertNotNull(res); } + @Test + void testCopyWorkflow() { + ResultDTO res = ohMyClient.copyWorkflow(WF_ID); + System.out.println(res); + Assertions.assertNotNull(res); + } + @Test void testAddWorkflowNode() { AddWorkflowNodeRequest addWorkflowNodeRequest = new AddWorkflowNodeRequest(); addWorkflowNodeRequest.setJobId(1L); addWorkflowNodeRequest.setWorkflowId(WF_ID); - System.out.println(ohMyClient.addWorkflowNode(Lists.newArrayList(addWorkflowNodeRequest))); + ResultDTO> res = ohMyClient.addWorkflowNode(Lists.newArrayList(addWorkflowNodeRequest)); + System.out.println(res); + Assertions.assertNotNull(res); } @Test @@ -70,7 +89,9 @@ class TestWorkflow extends ClientInitializer { modifyWorkflowNodeRequest.setNodeAlias("(๑•̀ㅂ•́)و✧"); modifyWorkflowNodeRequest.setEnable(false); modifyWorkflowNodeRequest.setSkipWhenFailed(false); - System.out.println(ohMyClient.modifyWorkflowNode(modifyWorkflowNodeRequest)); + ResultDTO res = ohMyClient.modifyWorkflowNode(modifyWorkflowNodeRequest); + System.out.println(res); + Assertions.assertNotNull(res); } @Test @@ -91,58 +112,79 @@ class TestWorkflow extends ClientInitializer { SaveWorkflowDAGRequest saveWorkflowDAGRequest = new SaveWorkflowDAGRequest(); saveWorkflowDAGRequest.setId(WF_ID); saveWorkflowDAGRequest.setDag(peWorkflowDAG); - - System.out.println(ohMyClient.saveWorkflowDag(saveWorkflowDAGRequest)); + ResultDTO res = ohMyClient.saveWorkflowDag(saveWorkflowDAGRequest); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - void testDisableWorkflow() throws Exception { - System.out.println(ohMyClient.disableWorkflow(WF_ID)); + void testDisableWorkflow() { + ResultDTO res = ohMyClient.disableWorkflow(WF_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - void testDeleteWorkflow() throws Exception { - System.out.println(ohMyClient.deleteWorkflow(WF_ID)); + void testDeleteWorkflow() { + ResultDTO res = ohMyClient.deleteWorkflow(WF_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - void testEnableWorkflow() throws Exception { - System.out.println(ohMyClient.enableWorkflow(WF_ID)); + void testEnableWorkflow() { + ResultDTO res = ohMyClient.enableWorkflow(WF_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - void testFetchWorkflowInfo() throws Exception { - System.out.println(ohMyClient.fetchWorkflow(WF_ID)); + void testFetchWorkflowInfo() { + ResultDTO res = ohMyClient.fetchWorkflow(WF_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - void testRunWorkflow() throws Exception { - System.out.println(ohMyClient.runWorkflow(WF_ID)); + void testRunWorkflow() { + ResultDTO res = ohMyClient.runWorkflow(WF_ID); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - void testStopWorkflowInstance() throws Exception { - System.out.println(ohMyClient.stopWorkflowInstance(149962433421639744L)); + void testStopWorkflowInstance() { + ResultDTO res = ohMyClient.stopWorkflowInstance(149962433421639744L); + System.out.println(res); + Assertions.assertNotNull(res); } @Test void testRetryWorkflowInstance() { - System.out.println(ohMyClient.retryWorkflowInstance(149962433421639744L)); + ResultDTO res = ohMyClient.retryWorkflowInstance(149962433421639744L); + System.out.println(res); + Assertions.assertNotNull(res); } @Test void testMarkWorkflowNodeAsSuccess() { - System.out.println(ohMyClient.markWorkflowNodeAsSuccess(149962433421639744L, 1L)); + ResultDTO res = ohMyClient.markWorkflowNodeAsSuccess(149962433421639744L, 1L); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - void testFetchWfInstanceInfo() throws Exception { - System.out.println(ohMyClient.fetchWorkflowInstanceInfo(149962433421639744L)); + void testFetchWfInstanceInfo() { + ResultDTO res = ohMyClient.fetchWorkflowInstanceInfo(149962433421639744L); + System.out.println(res); + Assertions.assertNotNull(res); } @Test - void testRunWorkflowPlus() throws Exception { - System.out.println(ohMyClient.runWorkflow(WF_ID, "this is init Params 2", 90000)); + void testRunWorkflowPlus() { + ResultDTO res = ohMyClient.runWorkflow(WF_ID, "this is init Params 2", 90000); + System.out.println(res); + Assertions.assertNotNull(res); } } diff --git a/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/service/JobService.java b/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/service/JobService.java index 54303528..bc1f93ad 100644 --- a/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/service/JobService.java +++ b/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/service/JobService.java @@ -104,6 +104,9 @@ public class JobService { public JobInfoDO copyJob(Long 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(); // 值拷贝 BeanUtils.copyProperties(origin, copyJob); diff --git a/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/web/controller/OpenAPIController.java b/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/web/controller/OpenAPIController.java index 979383c6..ae6fe8b3 100644 --- a/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/web/controller/OpenAPIController.java +++ b/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/web/controller/OpenAPIController.java @@ -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.OpenAPIConstant; 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.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.service.AppInfoService; import com.github.kfcfans.powerjob.server.service.CacheService; import com.github.kfcfans.powerjob.server.service.JobService; 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.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 org.springframework.web.bind.annotation.*; @@ -66,8 +64,8 @@ public class OpenAPIController { } @PostMapping(OpenAPIConstant.COPY_JOB) - public ResultDTO copyJob(Long jobId) { - return ResultDTO.success(JobInfoVO.from(jobService.copyJob(jobId))); + public ResultDTO copyJob(Long jobId) { + return ResultDTO.success(jobService.copyJob(jobId).getId()); } @PostMapping(OpenAPIConstant.FETCH_JOB)