diff --git a/LICENSE b/LICENSE index 4c96865..f49a4e1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,201 @@ -MIT License - -Copyright (c) 2022 Chenchen - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md index 3c2f878..55942ea 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ # dolphinscheduler-sdk-java 😄[中文文档](README_zh.md) -😃[英文文档](README.md) -the java sdk for operating dolphinscheduler +the java sdk for operating dolphinscheduler,which supports multi versions # 1 Why do this? In our company,dolphin scheduler becomes base system for our job.Then we want to operate dolphin scheduler by RPC instead of the web view. @@ -13,16 +12,17 @@ however,dolphin scheduler has not support java sdk,only python sdk.so I do this # 2 features ## 2.1 easy to operate -just use this sdk like using web view +just use this sdk like using web ui -## 2.2 multi version support +## 2.2 support multiple versions -with dolphinscheduler's develop,the rest api maybe change,so I want to support dolphinscheduler `2.0.5` and `3.1.4` version +with dolphinscheduler's develop,the rest api maybe change,so there are two versions supported: -​ +* `2.0.5-release` in branch `2.0.5-release` +* `3.1.4-release` in branch `3.1.4-release` -## 2.3 supported operations +## 2.3 support multiple operations Project: @@ -36,11 +36,8 @@ Project: Process Definition: * create process definition - * update process definition - * delete process definition - * release(online/offline) process definition @@ -49,9 +46,10 @@ Process Instance * start process instance * rerun process instance -* get process instance info * delete process instance * list process instance +* stop process instance +* pause process instance @@ -62,6 +60,7 @@ Schedule * online schedule * offline schedule * delete schedule +* get schedule by workflow code @@ -88,6 +87,13 @@ Alert * create alert plugin * list alert plugin +Tenant: +* create tenant +* update tenant +* delete tenant +* list tenant + + # 3 Use guide @@ -120,6 +126,15 @@ in your project,import `dolphinscheduler-sdk-java` ## 3.3 create dolphin client +necessary parameters from dolphin scheduler: + +| parameters | comment | +| -------------- | ------------------------------------------------------------ | +| token | dolphin scheduler token, created by web ui | +| dolphinAddress | dolphin scheduler endpoint, for example:`http://localhost:12345/dolphinscheduler` | + + + `springboot` project: ```java @@ -232,4 +247,5 @@ dolphinClient.opsForSchedule().update(...); dolphinClient.opsForSchedule().online(...); dolphinClient.opsForSchedule().offline(...); dolphinClient.opsForSchedule().delete(...); +dolphinClient.opsForSchedule().getByWorkflowCode(...); ``` \ No newline at end of file diff --git a/README_zh.md b/README_zh.md index 0dd0a0e..9143651 100644 --- a/README_zh.md +++ b/README_zh.md @@ -1,4 +1,3 @@ -😄[中文文档](README_zh.md) 😃[英文文档](README.md) # 1 为什么做这个 @@ -15,13 +14,12 @@ 你可以像使用页面那样的流程去操作定义dolphin scheduler上的资源,不过不同的是,你是通过代码的方式去实现。 - - ## 2.2 多版本支持 -随着dolphin scheduler的不断发展,rest api很可能会发生变化,所以我想支持dolphin scheduler`2.0.5`和`3.1.4`版本。如果你使用的是其他版本,可以考虑自行做修改,一般来说改动量不大 - +随着dolphin scheduler的不断发展,rest api很可能会发生变化,所以我目前支持了dolphin scheduler`2.0.5`和`3.1.4`版本。如果你使用的是其他版本,可以考虑自行做修改,一般来说改动量不大 +* `2.0.5-release` in branch `2.0.5-release` +* `3.1.4-release` in branch `3.1.4-release` ## 2.3 支持的操作 @@ -37,11 +35,8 @@ 工作流相关: * create process definition - * update process definition - * delete process definition - * release(online/offline) process definition @@ -50,9 +45,10 @@ * start process instance * rerun process instance -* get process instance info * delete process instance * list process instance +* stop process instance +* pause process instance @@ -63,6 +59,7 @@ * online schedule * offline schedule * delete schedule +* get schedule by workflow code @@ -86,11 +83,22 @@ 告警相关: + * create alert plugin * list alert plugin +租户相关: + +* create tenant +* update tenant +* delete tenant +* list tenant + + + + # 3 使用指南 ## 3.1 编译安装 @@ -118,6 +126,15 @@ mvn install -Dmaven.test.skip=true ## 3.3 创建dolphin client +在使用dolphin client之前,请先准备好这些参数 + +| parameters | comment | +| -------------- | ------------------------------------------------------------ | +| token | dolphin scheduler token, 可以在web ui创建 | +| dolphinAddress | dolphin scheduler入口,例如:`http://localhost:12345/dolphinscheduler` | + + + `springboot` project: ```java @@ -232,4 +249,5 @@ dolphinClient.opsForSchedule().update(...); dolphinClient.opsForSchedule().online(...); dolphinClient.opsForSchedule().offline(...); dolphinClient.opsForSchedule().delete(...); +dolphinClient.opsForSchedule().getByWorkflowCode(...); ``` \ No newline at end of file diff --git a/src/main/java/com/github/weaksloth/dolphins/core/DolphinClient.java b/src/main/java/com/github/weaksloth/dolphins/core/DolphinClient.java index 1dc0b3a..178cc38 100755 --- a/src/main/java/com/github/weaksloth/dolphins/core/DolphinClient.java +++ b/src/main/java/com/github/weaksloth/dolphins/core/DolphinClient.java @@ -7,6 +7,7 @@ import com.github.weaksloth.dolphins.remote.DolphinsRestTemplate; import com.github.weaksloth.dolphins.resource.ResourceOperator; import com.github.weaksloth.dolphins.schedule.ScheduleOperator; +import com.github.weaksloth.dolphins.tenant.TenantOperator; import lombok.extern.slf4j.Slf4j; /** dolphin scheduler client to operate dolphin scheduler */ @@ -23,6 +24,7 @@ public class DolphinClient { private ProcessInstanceOperator processInstanceOperator; private ScheduleOperator scheduleOperator; private ProjectOperator projectOperator; + private TenantOperator tenantOperator; public DolphinClient( String token, String dolphinAddress, DolphinsRestTemplate dolphinsRestTemplate) { @@ -45,6 +47,8 @@ public void initOperators() { new ScheduleOperator(this.dolphinAddress, this.token, this.dolphinsRestTemplate); this.projectOperator = new ProjectOperator(this.dolphinAddress, this.token, this.dolphinsRestTemplate); + this.tenantOperator = + new TenantOperator(this.dolphinAddress, this.token, this.dolphinsRestTemplate); } public DataSourceOperator opsForDataSource() { @@ -70,4 +74,8 @@ public ScheduleOperator opsForSchedule() { public ProjectOperator opsForProject() { return this.projectOperator; } + + public TenantOperator opsForTenant() { + return this.tenantOperator; + } } diff --git a/src/main/java/com/github/weaksloth/dolphins/core/DolphinClientConstant.java b/src/main/java/com/github/weaksloth/dolphins/core/DolphinClientConstant.java index 5e1fc17..3fd57aa 100755 --- a/src/main/java/com/github/weaksloth/dolphins/core/DolphinClientConstant.java +++ b/src/main/java/com/github/weaksloth/dolphins/core/DolphinClientConstant.java @@ -10,7 +10,12 @@ public static class Page { public static final String OFFLINE_RELEASE_STATE = "OFFLINE"; // 工作流下线状态 public static final String ONLINE_RELEASE_STATE = "ONLINE"; // 工作流上线状态 - public static final String RE_RUN_EXECUTE_TYPE = "REPEAT_RUNNING"; + /** the same with org.apache.dolphinscheduler.api.enums.ExecuteType */ + public static class ExecuteType { + public static final String RE_RUN = "REPEAT_RUNNING"; + public static final String STOP = "STOP"; + public static final String PAUSE = "PAUSE"; + } public static class HttpTask { public static final String HTTP_PARAMETER_TYPE_PARAMETER = "PARAMETER"; diff --git a/src/main/java/com/github/weaksloth/dolphins/instance/ProcessInstanceOperator.java b/src/main/java/com/github/weaksloth/dolphins/instance/ProcessInstanceOperator.java index 7f0dde2..2215398 100644 --- a/src/main/java/com/github/weaksloth/dolphins/instance/ProcessInstanceOperator.java +++ b/src/main/java/com/github/weaksloth/dolphins/instance/ProcessInstanceOperator.java @@ -86,18 +86,53 @@ public List page( * @return true for success,otherwise false */ public Boolean reRun(Long projectCode, Long processInstanceId) { - log.info("repeat run workflow instance,id:{}", processInstanceId); + return execute(projectCode, processInstanceId, DolphinClientConstant.ExecuteType.RE_RUN); + } + + /** + * stop dolphin scheduler workflow instance + * + * @param projectCode project code + * @param processInstanceId process instance id + * @return true for success,otherwise false + */ + public Boolean stop(Long projectCode, Long processInstanceId) { + log.info("stop workflow instance,id:{}", processInstanceId); + return execute(projectCode, processInstanceId, DolphinClientConstant.ExecuteType.STOP); + } + + /** + * pause dolphin scheduler workflow instance + * + * @param projectCode project code + * @param processInstanceId process instance id + * @return true for success,otherwise false + */ + public Boolean pause(Long projectCode, Long processInstanceId) { + log.info("stop workflow instance,id:{}", processInstanceId); + return execute(projectCode, processInstanceId, DolphinClientConstant.ExecuteType.PAUSE); + } + + /** + * execute dolphin scheduler workflow instance with custom execute type + * + * @param projectCode project code + * @param processInstanceId process instance id + * @param executeType {@link com.github.weaksloth.dolphins.core.DolphinClientConstant.ExecuteType} + * @return true for success,otherwise false + */ + public Boolean execute(Long projectCode, Long processInstanceId, String executeType) { String url = dolphinAddress + "/projects/" + projectCode + "/executors/execute"; ProcessInstanceRunParam reProcessInstanceRunParam = new ProcessInstanceRunParam() .setProcessInstanceId(processInstanceId) - .setExecuteType(DolphinClientConstant.RE_RUN_EXECUTE_TYPE); + .setExecuteType(executeType); try { HttpRestResult restResult = dolphinsRestTemplate.postForm(url, getHeader(), reProcessInstanceRunParam, String.class); return restResult.getSuccess(); } catch (Exception e) { - throw new DolphinException("repeat run dolphin scheduler process instance fail", e); + throw new DolphinException(executeType + " dolphin scheduler process instance fail", e); } } diff --git a/src/main/java/com/github/weaksloth/dolphins/project/ProjectOperator.java b/src/main/java/com/github/weaksloth/dolphins/project/ProjectOperator.java index 29a9376..99450e1 100644 --- a/src/main/java/com/github/weaksloth/dolphins/project/ProjectOperator.java +++ b/src/main/java/com/github/weaksloth/dolphins/project/ProjectOperator.java @@ -27,7 +27,7 @@ public ProjectOperator( * create project, api:/dolphinscheduler/projects * * @param projectCreateParam create project param - * @return true for success,otherwise false + * @return project info */ public ProjectInfoResp create(ProjectCreateParam projectCreateParam) { String url = dolphinAddress + "/projects"; diff --git a/src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleDefineResp.java b/src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleInfoResp.java similarity index 98% rename from src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleDefineResp.java rename to src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleInfoResp.java index 35f3744..fe0a6a9 100644 --- a/src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleDefineResp.java +++ b/src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleInfoResp.java @@ -10,7 +10,7 @@ @Data @Accessors(chain = true) @JsonIgnoreProperties(ignoreUnknown = true) -public class ScheduleDefineResp { +public class ScheduleInfoResp { private int id; diff --git a/src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleOperator.java b/src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleOperator.java index fd8320e..096c1a2 100644 --- a/src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleOperator.java +++ b/src/main/java/com/github/weaksloth/dolphins/schedule/ScheduleOperator.java @@ -1,9 +1,15 @@ package com.github.weaksloth.dolphins.schedule; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.github.weaksloth.dolphins.common.PageInfo; import com.github.weaksloth.dolphins.core.AbstractOperator; import com.github.weaksloth.dolphins.core.DolphinException; import com.github.weaksloth.dolphins.remote.DolphinsRestTemplate; import com.github.weaksloth.dolphins.remote.HttpRestResult; +import com.github.weaksloth.dolphins.remote.Query; +import com.github.weaksloth.dolphins.util.JacksonUtils; +import java.util.List; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -19,15 +25,15 @@ public ScheduleOperator( * * @param projectCode project code * @param scheduleDefineParam define param - * @return {@link ScheduleDefineResp} + * @return {@link ScheduleInfoResp} */ - public ScheduleDefineResp create(Long projectCode, ScheduleDefineParam scheduleDefineParam) { + public ScheduleInfoResp create(Long projectCode, ScheduleDefineParam scheduleDefineParam) { String url = dolphinAddress + "/projects/" + projectCode + "/schedules"; log.info("create schedule, url:{}, defineParam:{}", url, scheduleDefineParam); try { - HttpRestResult restResult = + HttpRestResult restResult = dolphinsRestTemplate.postForm( - url, getHeader(), scheduleDefineParam, ScheduleDefineResp.class); + url, getHeader(), scheduleDefineParam, ScheduleInfoResp.class); if (restResult.getSuccess()) { return restResult.getData(); } else { @@ -39,21 +45,47 @@ public ScheduleDefineResp create(Long projectCode, ScheduleDefineParam scheduleD } } + /** + * get schedule by workflow + * + * @param projectCode project's code + * @param processDefinitionCode workflow code + * @return {@link List} + */ + public List getByWorkflowCode(Long projectCode, Long processDefinitionCode) { + String url = dolphinAddress + "/projects/" + projectCode + "/schedules"; + Query query = + new Query() + .addParam("pageNo", "1") + .addParam("pageSize", "10") + .addParam("processDefinitionCode", String.valueOf(processDefinitionCode)); + try { + HttpRestResult stringHttpRestResult = + dolphinsRestTemplate.get(url, getHeader(), query, JsonNode.class); + return JacksonUtils.parseObject( + stringHttpRestResult.getData().toString(), + new TypeReference>() {}) + .getTotalList(); + } catch (Exception e) { + throw new DolphinException("list dolphin scheduler schedule fail", e); + } + } + /** * update schedule * * @param projectCode project code * @param scheduleDefineParam define param - * @return {@link ScheduleDefineResp} + * @return {@link ScheduleInfoResp} */ - public ScheduleDefineResp update( + public ScheduleInfoResp update( Long projectCode, Long scheduleId, ScheduleDefineParam scheduleDefineParam) { String url = dolphinAddress + "/projects/" + projectCode + "/schedules/" + scheduleId; log.info("update schedule, url:{}, defineParam:{}", url, scheduleDefineParam); try { - HttpRestResult restResult = + HttpRestResult restResult = dolphinsRestTemplate.putForm( - url, getHeader(), scheduleDefineParam, ScheduleDefineResp.class); + url, getHeader(), scheduleDefineParam, ScheduleInfoResp.class); if (restResult.getSuccess()) { return restResult.getData(); } else { diff --git a/src/main/java/com/github/weaksloth/dolphins/task/HttpTask.java b/src/main/java/com/github/weaksloth/dolphins/task/HttpTask.java index dcb91e0..eeed701 100644 --- a/src/main/java/com/github/weaksloth/dolphins/task/HttpTask.java +++ b/src/main/java/com/github/weaksloth/dolphins/task/HttpTask.java @@ -26,9 +26,9 @@ public class HttpTask extends AbstractTask { private String httpMethod; private String httpCheckCondition; // STATUS_CODE_DEFAULT - private String condition; - private Integer connectTimeout; - private Integer socketTimeout; + private String condition = ""; + private Integer connectTimeout = 60000; + private Integer socketTimeout = 60000; @Override public String getTaskType() { diff --git a/src/main/java/com/github/weaksloth/dolphins/tenant/TenantDefineParam.java b/src/main/java/com/github/weaksloth/dolphins/tenant/TenantDefineParam.java new file mode 100644 index 0000000..ebd4d63 --- /dev/null +++ b/src/main/java/com/github/weaksloth/dolphins/tenant/TenantDefineParam.java @@ -0,0 +1,17 @@ +package com.github.weaksloth.dolphins.tenant; + +import lombok.Data; +import lombok.experimental.Accessors; + +/** create or update tenant param */ +@Data +@Accessors(chain = true) +public class TenantDefineParam { + + private String tenantCode; + + /** the default queue id is 1, which means default in dolphin scheduler */ + private Integer queueId = 1; + + private String description; +} diff --git a/src/main/java/com/github/weaksloth/dolphins/tenant/TenantInfoResp.java b/src/main/java/com/github/weaksloth/dolphins/tenant/TenantInfoResp.java new file mode 100644 index 0000000..5edbe7a --- /dev/null +++ b/src/main/java/com/github/weaksloth/dolphins/tenant/TenantInfoResp.java @@ -0,0 +1,36 @@ +package com.github.weaksloth.dolphins.tenant; + +import com.fasterxml.jackson.annotation.JsonFormat; +import java.util.Date; +import lombok.Data; + +/** tenant info resp, copied from org.apache.dolphinscheduler.dao.entity.Tenant */ +@Data +public class TenantInfoResp { + + /** id */ + private Integer id; + + /** tenant code */ + private String tenantCode; + + /** description */ + private String description; + + /** queue id */ + private int queueId; + + /** queue name */ + private String queueName; + + /** queue */ + private String queue; + + /** create time */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createTime; + + /** update time */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateTime; +} diff --git a/src/main/java/com/github/weaksloth/dolphins/tenant/TenantOperator.java b/src/main/java/com/github/weaksloth/dolphins/tenant/TenantOperator.java new file mode 100644 index 0000000..3d091e9 --- /dev/null +++ b/src/main/java/com/github/weaksloth/dolphins/tenant/TenantOperator.java @@ -0,0 +1,115 @@ +package com.github.weaksloth.dolphins.tenant; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.github.weaksloth.dolphins.common.PageInfo; +import com.github.weaksloth.dolphins.core.AbstractOperator; +import com.github.weaksloth.dolphins.core.DolphinClientConstant; +import com.github.weaksloth.dolphins.core.DolphinException; +import com.github.weaksloth.dolphins.remote.DolphinsRestTemplate; +import com.github.weaksloth.dolphins.remote.HttpRestResult; +import com.github.weaksloth.dolphins.remote.Query; +import com.github.weaksloth.dolphins.util.JacksonUtils; +import java.util.List; +import java.util.Optional; +import lombok.extern.slf4j.Slf4j; + +/** operator for tenant */ +@Slf4j +public class TenantOperator extends AbstractOperator { + + public TenantOperator( + String dolphinAddress, String token, DolphinsRestTemplate dolphinsRestTemplate) { + super(dolphinAddress, token, dolphinsRestTemplate); + } + + /** + * create tenant, api: /dolphinscheduler/tenants + * + * @param tenantDefineParam create tenant param + * @return tenant info + */ + public TenantInfoResp create(TenantDefineParam tenantDefineParam) { + String url = dolphinAddress + "/tenants"; + try { + HttpRestResult result = + dolphinsRestTemplate.postForm(url, getHeader(), tenantDefineParam, TenantInfoResp.class); + if (result.getSuccess()) { + return result.getData(); + } else { + log.error("create tenant response:{}", result); + throw new DolphinException("create dolphin scheduler tenant fail"); + } + } catch (Exception e) { + throw new DolphinException("create dolphin scheduler tenant fail", e); + } + } + + /** + * create tenant, api: /dolphinscheduler/tenants/{id} + * + * @param updateParam update tenant param + * @return tenant info + */ + public Boolean update(Long tenantId, TenantDefineParam updateParam) { + String url = dolphinAddress + "/tenants/" + tenantId; + try { + HttpRestResult result = + dolphinsRestTemplate.putForm(url, getHeader(), updateParam, String.class); + log.info("update tenant response:{}", result); + return result.getSuccess(); + } catch (Exception e) { + throw new DolphinException("update dolphin scheduler tenant fail", e); + } + } + + /** + * delete dolphin scheduler tenant + * + * @param tenantId dolphin scheduler tenant id + * @return true for success,otherwise false + */ + public Boolean delete(Long tenantId) { + String url = dolphinAddress + "/tenants/" + tenantId; + try { + HttpRestResult result = + dolphinsRestTemplate.delete(url, getHeader(), null, String.class); + log.info("delete tenant response:{}", result); + return result.getSuccess(); + } catch (Exception e) { + throw new DolphinException("delete dolphin scheduler tenant fail", e); + } + } + + /** + * page query tenant list ,api:/dolphinscheduler/tenants + * + * @param page page number + * @param size page size + * @param tenantCode tenant code, such as root. + * @return {@link List} + */ + public List page(Integer page, Integer size, String tenantCode) { + + page = Optional.ofNullable(page).orElse(DolphinClientConstant.Page.DEFAULT_PAGE); + size = Optional.ofNullable(size).orElse(DolphinClientConstant.Page.DEFAULT_SIZE); + + String url = dolphinAddress + "/tenants"; + Query query = + new Query() + .addParam("pageNo", String.valueOf(page)) + .addParam("pageSize", String.valueOf(size)) + .addParam("searchVal", tenantCode) + .build(); + try { + HttpRestResult stringHttpRestResult = + dolphinsRestTemplate.get(url, getHeader(), query, JsonNode.class); + return JacksonUtils.parseObject( + stringHttpRestResult.getData().toString(), + new TypeReference>() {}) + .getTotalList(); + } catch (Exception e) { + throw new DolphinException("list dolphin scheduler tenant fail", e); + } + } +} diff --git a/src/test/java/com/github/weaksloth/dolphins/insatnce/ProcessInstanceTest.java b/src/test/java/com/github/weaksloth/dolphins/insatnce/ProcessInstanceTest.java index 17c112e..9d9270d 100644 --- a/src/test/java/com/github/weaksloth/dolphins/insatnce/ProcessInstanceTest.java +++ b/src/test/java/com/github/weaksloth/dolphins/insatnce/ProcessInstanceTest.java @@ -7,14 +7,14 @@ public class ProcessInstanceTest extends BaseTest { + public static final Long WORKFLOW_CODE = 6920291870528L; + /** the workflow must in online state,otherwise will cause error */ @Test public void testStartInstance() { - - Long workflowId = 6920291870528L; ProcessInstanceCreateParam startParam = new ProcessInstanceCreateParam(); startParam - .setProcessDefinitionCode(workflowId) + .setProcessDefinitionCode(WORKFLOW_CODE) .setScheduleTime("") .setFailureStrategy("CONTINUE") .setWarningType("NONE") diff --git a/src/test/java/com/github/weaksloth/dolphins/schedule/ScheduleTest.java b/src/test/java/com/github/weaksloth/dolphins/schedule/ScheduleTest.java index bd05c5b..40c7fa9 100644 --- a/src/test/java/com/github/weaksloth/dolphins/schedule/ScheduleTest.java +++ b/src/test/java/com/github/weaksloth/dolphins/schedule/ScheduleTest.java @@ -1,43 +1,58 @@ package com.github.weaksloth.dolphins.schedule; import com.github.weaksloth.dolphins.BaseTest; +import java.util.List; import org.junit.Assert; import org.junit.Test; public class ScheduleTest extends BaseTest { - /** the workflow must in offline state */ + public static final Long WORKFLOW_CODE = 9488530859328L; + + /** the workflow must in online state */ @Test public void testCreate() { - Long workflowCode = 6920291870528L; ScheduleDefineParam scheduleDefineParam = new ScheduleDefineParam(); scheduleDefineParam - .setProcessDefinitionCode(workflowCode) + .setProcessDefinitionCode(WORKFLOW_CODE) .setSchedule( new ScheduleDefineParam.Schedule() .setStartTime("2022-09-18 00:00:00") - .setEndTime("2022-09-20 00:00:00") + .setEndTime("2023-09-20 00:00:00") .setCrontab("0 0 * * * ? *")); - ScheduleDefineResp scheduleDefineResp = + ScheduleInfoResp scheduleInfoResp = getClient().opsForSchedule().create(projectCode, scheduleDefineParam); - System.out.println(scheduleDefineResp); + System.out.println(scheduleInfoResp); + } + + @Test + public void testGetByProject() { + List resp = + getClient().opsForSchedule().getByWorkflowCode(projectCode, WORKFLOW_CODE); + Assert.assertEquals(1, resp.size()); } @Test public void testOnline() { - Long id = 2L; + List resp = + getClient().opsForSchedule().getByWorkflowCode(projectCode, WORKFLOW_CODE); + long id = resp.get(0).getId(); Assert.assertTrue(getClient().opsForSchedule().online(projectCode, id)); } @Test public void testOffline() { - Long id = 2L; + List resp = + getClient().opsForSchedule().getByWorkflowCode(projectCode, WORKFLOW_CODE); + long id = resp.get(0).getId(); Assert.assertTrue(getClient().opsForSchedule().offline(projectCode, id)); } @Test public void testDelete() { - Long id = 2L; + List resp = + getClient().opsForSchedule().getByWorkflowCode(projectCode, WORKFLOW_CODE); + long id = resp.get(0).getId(); Assert.assertTrue(getClient().opsForSchedule().delete(projectCode, id)); } } diff --git a/src/test/java/com/github/weaksloth/dolphins/task/TaskTest.java b/src/test/java/com/github/weaksloth/dolphins/task/TaskTest.java index 926fed3..b37f956 100644 --- a/src/test/java/com/github/weaksloth/dolphins/task/TaskTest.java +++ b/src/test/java/com/github/weaksloth/dolphins/task/TaskTest.java @@ -50,9 +50,6 @@ public void testHttpTask() { .setUrl("http://www.baidu.com") .setHttpMethod("GET") .setHttpCheckCondition("STATUS_CODE_DEFAULT") - .setCondition("") - .setConnectTimeout(60000) - .setSocketTimeout(60000) .setConditionResult(TaskUtils.createEmptyConditionResult()); // use utils to create task definition with default config diff --git a/src/test/java/com/github/weaksloth/dolphins/tenant/TenantTest.java b/src/test/java/com/github/weaksloth/dolphins/tenant/TenantTest.java new file mode 100644 index 0000000..f0cf6b7 --- /dev/null +++ b/src/test/java/com/github/weaksloth/dolphins/tenant/TenantTest.java @@ -0,0 +1,40 @@ +package com.github.weaksloth.dolphins.tenant; + +import com.github.weaksloth.dolphins.BaseTest; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +public class TenantTest extends BaseTest { + + public static final String TENANT_CODE = "xcchen"; + + @Test + public void testCreateTenant() { + TenantDefineParam createParam = new TenantDefineParam(); + createParam.setTenantCode(TENANT_CODE).setDescription("create by dolphin scheduler api"); + TenantInfoResp tenantInfoResp = getClient().opsForTenant().create(createParam); + System.out.println(tenantInfoResp); + Assert.assertEquals(TENANT_CODE, tenantInfoResp.getTenantCode()); + } + + @Test + public void testUpdateTenant() { + long tenantId = getClient().opsForTenant().page(null, null, TENANT_CODE).get(0).getId(); + TenantDefineParam updateParam = new TenantDefineParam(); + updateParam.setTenantCode(TENANT_CODE).setDescription("update by dolphin scheduler"); + Assert.assertTrue(getClient().opsForTenant().update(tenantId, updateParam)); + } + + @Test + public void testPageTenant() { + List page = getClient().opsForTenant().page(null, null, ""); + page.forEach(System.out::println); + } + + @Test + public void testDeleteTenant() { + long tenantId = getClient().opsForTenant().page(null, null, TENANT_CODE).get(0).getId(); + Assert.assertTrue(getClient().opsForTenant().delete(tenantId)); + } +} diff --git a/src/test/java/com/github/weaksloth/dolphins/workflow/WorkflowTest.java b/src/test/java/com/github/weaksloth/dolphins/workflow/WorkflowTest.java index 8f90323..64b3aea 100644 --- a/src/test/java/com/github/weaksloth/dolphins/workflow/WorkflowTest.java +++ b/src/test/java/com/github/weaksloth/dolphins/workflow/WorkflowTest.java @@ -16,6 +16,8 @@ /** the test for workflow/process */ public class WorkflowTest extends BaseTest { + public static final String WORKFLOW_NAME = "test-dag"; + /** * create simple workflow like: shellTask -> httpTask * @@ -48,15 +50,12 @@ public void testCreateProcessDefinition() { .setUrl("http://www.baidu.com") .setHttpMethod("GET") .setHttpCheckCondition("STATUS_CODE_DEFAULT") - .setCondition("") - .setConnectTimeout(60000) - .setSocketTimeout(60000) .setConditionResult(TaskUtils.createEmptyConditionResult()); TaskDefinition httpTaskDefinition = TaskDefinitionUtils.createDefaultTaskDefinition(taskCodes.get(1), httpTask); ProcessDefineParam pcr = new ProcessDefineParam(); - pcr.setName("test-dag") + pcr.setName(WORKFLOW_NAME) .setLocations(TaskLocationUtils.horizontalLocation(taskCodes.toArray(new Long[0]))) .setDescription("test-dag-description") .setTenantCode(tenantCode) @@ -70,27 +69,31 @@ public void testCreateProcessDefinition() { @Test public void testPage() { - List page = getClient().opsForProcess().page(projectCode, null, null, null); + List page = + getClient().opsForProcess().page(projectCode, null, null, WORKFLOW_NAME); int expectedWorkflowNumber = 1; Assert.assertEquals(expectedWorkflowNumber, page.size()); } @Test public void testOnlineWorkflow() { - List page = getClient().opsForProcess().page(projectCode, null, null, null); + List page = + getClient().opsForProcess().page(projectCode, null, null, WORKFLOW_NAME); Assert.assertTrue(getClient().opsForProcess().online(projectCode, page.get(0).getCode())); } @Test public void testOfflineWorkflow() { - List page = getClient().opsForProcess().page(projectCode, null, null, null); + List page = + getClient().opsForProcess().page(projectCode, null, null, WORKFLOW_NAME); Assert.assertTrue(getClient().opsForProcess().offline(projectCode, page.get(0).getCode())); } /** the workflow must in offline state */ @Test public void testDeleteWorkflow() { - List page = getClient().opsForProcess().page(projectCode, null, null, null); + List page = + getClient().opsForProcess().page(projectCode, null, null, WORKFLOW_NAME); Assert.assertTrue(getClient().opsForProcess().delete(projectCode, page.get(0).getCode())); } }