Skip to content

Commit

Permalink
fix #95
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed May 24, 2016
1 parent edbfae2 commit 39576a5
Show file tree
Hide file tree
Showing 36 changed files with 488 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public final class JobBriefInfo implements Serializable, Comparable<JobBriefInfo

private String jobName;

private String jobClass;

private JobStatus status;

private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ public final class JobSettings implements Serializable {
private String jobShardingStrategyClass;

private String description;

private String scriptCommandLine;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public JobSettings getJobSettings(final String jobName) {
result.setMisfire(Boolean.valueOf(registryCenter.get(jobNodePath.getConfigNodePath("misfire"))));
result.setJobShardingStrategyClass(registryCenter.get(jobNodePath.getConfigNodePath("jobShardingStrategyClass")));
result.setDescription(registryCenter.get(jobNodePath.getConfigNodePath("description")));
result.setScriptCommandLine(registryCenter.get(jobNodePath.getConfigNodePath("scriptCommandLine")));
return result;
}

Expand All @@ -77,6 +78,7 @@ public void updateJobSettings(final JobSettings jobSettings) {
updateIfChanged(jobNodePath.getConfigNodePath("misfire"), jobSettings.isMisfire());
updateIfChanged(jobNodePath.getConfigNodePath("jobShardingStrategyClass"), jobSettings.getJobShardingStrategyClass());
updateIfChanged(jobNodePath.getConfigNodePath("description"), jobSettings.getDescription());
updateIfChanged(jobNodePath.getConfigNodePath("scriptCommandLine"), jobSettings.getScriptCommandLine());
}

private void updateIfChanged(final String nodePath, final Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Collection<JobBriefInfo> getAllJobsBriefInfo() {
JobNodePath jobNodePath = new JobNodePath(each);
JobBriefInfo jobBriefInfo = new JobBriefInfo();
jobBriefInfo.setJobName(each);
jobBriefInfo.setJobClass(registryCenter.get(jobNodePath.getConfigNodePath("jobClass")));
jobBriefInfo.setDescription(registryCenter.get(jobNodePath.getConfigNodePath("description")));
jobBriefInfo.setStatus(getJobStatus(each));
jobBriefInfo.setCron(registryCenter.get(jobNodePath.getConfigNodePath("cron")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public String registryCenterPage(final ModelMap model) {
}

@RequestMapping(value = "job_detail", method = RequestMethod.GET)
public String jobDetail(@RequestParam final String jobName, final ModelMap model) {
public String jobDetail(@RequestParam final String jobName, @RequestParam final String jobClass, final ModelMap model) {
model.put("jobName", jobName);
model.put("jobClass", jobClass);
return "job_detail";
}

Expand Down
2 changes: 1 addition & 1 deletion elastic-job-console/src/main/webapp/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function renderJobsForDashboardNav() {
var $jobsDimension = $("#jobs-dimension");
$jobsDimension.empty();
for (var i = 0; i < data.length; i++) {
var liContent = "<a href='job_detail?jobName=" + data[i].jobName + "' data-placement='right' title='" + data[i].description + "'>" + data[i].jobName + "</a>";
var liContent = "<a href='job_detail?jobName=" + data[i].jobName + "&jobClass=" + data[i].jobClass + "' data-placement='right' title='" + data[i].description + "'>" + data[i].jobName + "</a>";
if (currentJob && currentJob === data[i].jobName) {
$jobsDimension.append("<li class='open'>" + liContent + "</li>");
} else {
Expand Down
4 changes: 3 additions & 1 deletion elastic-job-console/src/main/webapp/js/job_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function renderSettings() {
if (!data.monitorExecution) {
$("#execution_info_tab").addClass("disabled");
}
$("#scriptCommandLine").attr("value", data.scriptCommandLine);
});
}

Expand All @@ -61,8 +62,9 @@ function bindSubmitJobSettingsForm() {
var misfire = $("#misfire").prop("checked");
var shardingItemParameters = $("#shardingItemParameters").val();
var jobShardingStrategyClass = $("#jobShardingStrategyClass").val();
var scriptCommandLine = $("#scriptCommandLine").val();
var description = $("#description").val();
$.post("job/settings", {jobName: jobName, jobClass : jobClass, shardingTotalCount: shardingTotalCount, jobParameter: jobParameter, cron: cron, concurrentDataProcessThreadCount: concurrentDataProcessThreadCount, processCountIntervalSeconds: processCountIntervalSeconds, fetchDataCount: fetchDataCount, maxTimeDiffSeconds: maxTimeDiffSeconds, monitorPort: monitorPort, monitorExecution: monitorExecution, failover: failover, misfire: misfire, shardingItemParameters: shardingItemParameters, jobShardingStrategyClass: jobShardingStrategyClass, description: description}, function(data) {
$.post("job/settings", {jobName: jobName, jobClass : jobClass, shardingTotalCount: shardingTotalCount, jobParameter: jobParameter, cron: cron, concurrentDataProcessThreadCount: concurrentDataProcessThreadCount, processCountIntervalSeconds: processCountIntervalSeconds, fetchDataCount: fetchDataCount, maxTimeDiffSeconds: maxTimeDiffSeconds, monitorPort: monitorPort, monitorExecution: monitorExecution, failover: failover, misfire: misfire, shardingItemParameters: shardingItemParameters, jobShardingStrategyClass: jobShardingStrategyClass, description: description, scriptCommandLine: scriptCommandLine}, function(data) {
showSuccessDialog();
if (monitorExecution) {
$("#execution_info_tab").removeClass("disabled");
Expand Down
2 changes: 1 addition & 1 deletion elastic-job-console/src/main/webapp/js/overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function renderJobsOverview() {
$("#jobs-overview-tbl tbody").empty();
for (var i = 0;i < data.length;i++) {
var status = data[i].status;
var baseTd = "<td>" + "<a href='job_detail?jobName=" + data[i].jobName + "'>" + data[i].jobName + "</a>" + "</td><td>" + status + "</td><td>" + data[i].cron + "</td><td>" + data[i].description + "</td>";
var baseTd = "<td>" + "<a href='job_detail?jobName=" + data[i].jobName + "&jobClass=" + data[i].jobClass + "'>" + data[i].jobName + "</a>" + "</td><td>" + status + "</td><td>" + data[i].cron + "</td><td>" + data[i].description + "</td>";
var trClass = "";
if ("OK" === status) {
trClass = "success";
Expand Down
8 changes: 8 additions & 0 deletions elastic-job-console/src/main/webapp/templates/job_detail.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@
<textarea id="description" name="description" class="form-control"></textarea>
</div>
</div>
<#if jobClass == "com.dangdang.ddframe.job.plugin.job.type.integrated.ScriptElasticJob">
<div class="form-group">
<label for="scriptCommandLine" class="col-sm-2 control-label">脚本作业全路径</label>
<div class="col-sm-9">
<input type="text" id="scriptCommandLine" name="scriptCommandLine" class="form-control" data-toggle="tooltip" data-placement="bottom" title="执行脚本的全路径名称,可以包含参数" />
</div>
</div>
</#if>
<button type="reset" class="btn btn-inverse">重置</button>
<button type="submit" class="btn btn-primary">更新</button>
</form>
Expand Down
8 changes: 8 additions & 0 deletions elastic-job-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
Expand All @@ -41,6 +45,10 @@
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,13 @@ public class JobConfiguration {
* 如果可覆盖, 每次启动作业都以本地配置为准.
*/
private boolean overwrite;

/**
* 作业执行脚本命令行.
*
* <p>
* 只对脚本类型作业起作用.
* </p>
*/
private String scriptCommandLine = "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@

package com.dangdang.ddframe.job.api;

import com.dangdang.ddframe.job.exception.JobException;
import com.dangdang.ddframe.job.internal.job.AbstractJobExecutionShardingContext;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.dangdang.ddframe.job.exception.JobException;
import com.dangdang.ddframe.job.internal.job.AbstractJobExecutionShardingContext;

import lombok.Getter;
import lombok.Setter;

/**
* 作业运行时多片分片上下文.
*
Expand Down Expand Up @@ -84,4 +83,8 @@ public String toString() {
getJobName(), getShardingTotalCount(), shardingItems, shardingItemParameters, getJobParameter()
);
}

public String toScriptArguments() {
return new Gson().toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public final class ConfigurationNode {
static final String DESCRIPTION = ROOT + "/description";

static final String MONITOR_PORT = ROOT + "/monitorPort";

static final String SCRIPT_COMMAND_LINE = ROOT + "/scriptCommandLine";

private final JobNodePath jobNodePath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

package com.dangdang.ddframe.job.internal.config;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.dangdang.ddframe.job.api.JobConfiguration;
import com.dangdang.ddframe.job.exception.JobConflictException;
import com.dangdang.ddframe.job.exception.ShardingItemParametersException;
Expand All @@ -29,6 +25,10 @@
import com.dangdang.ddframe.reg.base.CoordinatorRegistryCenter;
import com.google.common.base.Strings;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* 弹性化分布式作业配置服务.
*
Expand Down Expand Up @@ -76,6 +76,7 @@ private void registerJobInfo() {
jobNodeStorage.fillJobNodeIfNullOrOverwrite(ConfigurationNode.JOB_SHARDING_STRATEGY_CLASS, jobNodeStorage.getJobConfiguration().getJobShardingStrategyClass());
jobNodeStorage.fillJobNodeIfNullOrOverwrite(ConfigurationNode.DESCRIPTION, jobNodeStorage.getJobConfiguration().getDescription());
jobNodeStorage.fillJobNodeIfNullOrOverwrite(ConfigurationNode.MONITOR_PORT, jobNodeStorage.getJobConfiguration().getMonitorPort());
jobNodeStorage.fillJobNodeIfNullOrOverwrite(ConfigurationNode.SCRIPT_COMMAND_LINE, jobNodeStorage.getJobConfiguration().getScriptCommandLine());
}

/**
Expand Down Expand Up @@ -230,4 +231,18 @@ public int getMonitorPort() {
public String getJobName() {
return jobNodeStorage.getJobConfiguration().getJobName();
}

/**
* 获取作业执行脚本命令行.
*
* <p>
* 仅脚本型作业有效.
* </p>
*
* @return 脚本型作业执行脚本命令行
*/
public String getScriptCommandLine() {
return jobNodeStorage.getJobNodeData(ConfigurationNode.SCRIPT_COMMAND_LINE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,20 @@ public String getJobName() {
public int getConcurrentDataProcessThreadCount() {
return configService.getConcurrentDataProcessThreadCount();
}


/**
* 获取脚本型作业执行命令行.
*
* <p>
* 仅脚本作业有效.
* </p>
*
* @return 脚本型作业执行命令行
*/
public String getScriptCommandLine() {
return configService.getScriptCommandLine();
}

/**
* 检查本机与注册中心的时间误差秒数是否在允许范围.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* 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.
* </p>
*/

package com.dangdang.ddframe.job.plugin.job.type.integrated;

import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
import com.dangdang.ddframe.job.exception.JobException;
import com.dangdang.ddframe.job.internal.job.AbstractElasticJob;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;

import java.io.IOException;

/**
* 脚本类型作业.
*
* @author caohao
*/
public final class ScriptElasticJob extends AbstractElasticJob {

@Override
protected void executeJob(final JobExecutionMultipleShardingContext shardingContext) {
String scriptCommandLine = getJobFacade().getScriptCommandLine();
Preconditions.checkArgument(!Strings.isNullOrEmpty(scriptCommandLine), "Cannot find script command line.");
CommandLine cmdLine = CommandLine.parse(scriptCommandLine);
cmdLine.addArgument(shardingContext.toScriptArguments(), false);
DefaultExecutor executor = new DefaultExecutor();
try {
executor.execute(cmdLine);
} catch (final IOException ex) {
throw new JobException(ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* * Copyright 1999-2015 dangdang.com.
* * <p>
* * 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.
* * </p>
*
*/

package com.dangdang.ddframe.job.fixture;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ScriptElasticJobHelper {

public static String buildScriptCommandLine() {
try {
if (System.getProperties().getProperty("os.name").contains("Windows")) {
return Paths.get(ScriptElasticJobHelper.class.getResource("/script/test.bat").getPath().substring(1)).toString();
} else {
Path result = Paths.get(ScriptElasticJobHelper.class.getResource("/script/test.sh").getPath());
changeFilePermissions(result);
return result.toString();
}
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}

private static void changeFilePermissions(final Path path) throws IOException {
Set<PosixFilePermission> permissionsSet = new HashSet<>();
permissionsSet.add(PosixFilePermission.OWNER_READ);
permissionsSet.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(path, permissionsSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

package com.dangdang.ddframe.job.integrate;

import org.junit.After;
import org.junit.Before;

import com.dangdang.ddframe.job.api.ElasticJob;
import com.dangdang.ddframe.job.api.JobConfiguration;
import org.junit.After;
import org.junit.Before;

public abstract class AbstractBaseStdJobAutoInitTest extends AbstractBaseStdJobTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.dangdang.ddframe.job.integrate;

import com.dangdang.ddframe.job.integrate.std.integrated.ScriptElasticJobTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
Expand Down Expand Up @@ -45,7 +46,8 @@
StreamingThroughputDataFlowElasticJobForMultipleThreadsTest.class,
StreamingThroughputDataFlowElasticJobForExecuteFailureTest.class,
StreamingThroughputDataFlowElasticJobForExecuteThrowsExceptionTest.class,
StreamingThroughputDataFlowElasticJobForPausedTest.class
StreamingThroughputDataFlowElasticJobForPausedTest.class,
ScriptElasticJobTest.class
})
public final class AllIntegrateTests {
}
Loading

0 comments on commit 39576a5

Please sign in to comment.