Skip to content

Commit

Permalink
fix #71
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Mar 18, 2016
1 parent ffbeef8 commit 9f6e974
Show file tree
Hide file tree
Showing 23 changed files with 251 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@

package com.dangdang.ddframe.job.console.controller;

import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dangdang.ddframe.job.console.domain.ExecutionInfo;
import com.dangdang.ddframe.job.console.domain.JobBriefInfo;
import com.dangdang.ddframe.job.console.domain.JobServer;
import com.dangdang.ddframe.job.console.domain.JobSettings;
import com.dangdang.ddframe.job.console.service.JobDimensionService;
import com.dangdang.ddframe.job.console.service.JobOperationService;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;

@RestController
@RequestMapping("job")
Expand All @@ -40,9 +37,6 @@ public class JobController {
@Resource
private JobDimensionService jobDimensionService;

@Resource
private JobOperationService jobOperationService;

@RequestMapping(value = "jobs", method = RequestMethod.GET)
public Collection<JobBriefInfo> getAllJobsBriefInfo() {
return jobDimensionService.getAllJobsBriefInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public void stopAllJobs(final JobServer jobServer) {
public void resumeAllJobs(final JobServer jobServer) {
jobOperationService.resumeAllJobsByServer(jobServer.getIp());
}

@RequestMapping(value = "shutdown", method = RequestMethod.POST)
public void shutdownJob(final JobServer jobServer) {
jobOperationService.shutdownJob(jobServer.getJobName(), jobServer.getIp());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@

package com.dangdang.ddframe.job.console.controller;

import java.util.Collection;

import javax.annotation.Resource;

import com.dangdang.ddframe.job.console.domain.JobServer;
import com.dangdang.ddframe.job.console.domain.ServerBriefInfo;
import com.dangdang.ddframe.job.console.service.ServerDimensionService;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dangdang.ddframe.job.console.domain.JobServer;
import com.dangdang.ddframe.job.console.domain.ServerBriefInfo;
import com.dangdang.ddframe.job.console.service.JobOperationService;
import com.dangdang.ddframe.job.console.service.ServerDimensionService;
import javax.annotation.Resource;
import java.util.Collection;

@RestController
@RequestMapping("server")
Expand All @@ -38,9 +35,6 @@ public class ServerController {
@Resource
private ServerDimensionService serverDimensionService;

@Resource
private JobOperationService jobOperationService;

@RequestMapping(value = "servers", method = RequestMethod.GET)
public Collection<ServerBriefInfo> getAllServersBriefInfo() {
return serverDimensionService.getAllServersBriefInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public enum ServerStatus {
RUNNING,
DISABLED,
STOPED,
CRASHED;
CRASHED,
SHUTDOWN;

public static ServerStatus getServerStatus(final String status, final boolean disabled, final boolean stopped) {
public static ServerStatus getServerStatus(final String status, final boolean disabled, final boolean stopped, final boolean shutdown) {
if (shutdown) {
return ServerStatus.SHUTDOWN;
}
if (Strings.isNullOrEmpty(status)) {
return ServerStatus.CRASHED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public interface JobOperationService {
void stopAllJobsByServer(String serverIp);

void resumeAllJobsByServer(String serverIp);

void shutdownJob(String jobName, String serverIp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ private JobStatus getJobStatus(final String jobName) {
manuallyDisabledCount++;
break;
case CRASHED:
case SHUTDOWN:
crashedCount++;
break;
default:
Expand Down Expand Up @@ -166,7 +167,8 @@ private ServerStatus getServerStatus(final String jobName, final String serverIp
String status = curatorRepository.getData(JobNodePath.getServerNodePath(jobName, serverIp, "status"));
boolean disabled = curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, serverIp, "disabled"));
boolean stopped = curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, serverIp, "stoped"));
return ServerStatus.getServerStatus(status, disabled, stopped);
boolean shutdown = curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, serverIp, "shutdown"));
return ServerStatus.getServerStatus(status, disabled, stopped, shutdown);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public void resumeAllJobsByServer(final String serverIp) {
curatorRepository.delete(JobNodePath.getServerNodePath(jobName, serverIp, "stoped"));
}
}

@Override
public void shutdownJob(final String jobName, final String serverIp) {
curatorRepository.create(JobNodePath.getServerNodePath(jobName, serverIp, "shutdown"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@

package com.dangdang.ddframe.job.console.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dangdang.ddframe.job.console.domain.JobServer;
import com.dangdang.ddframe.job.console.domain.JobServer.ServerStatus;
import com.dangdang.ddframe.job.console.domain.ServerBriefInfo;
import com.dangdang.ddframe.job.console.domain.ServerBriefInfo.ServerBriefStatus;
import com.dangdang.ddframe.job.console.repository.zookeeper.CuratorRepository;
import com.dangdang.ddframe.job.console.service.ServerDimensionService;
import com.dangdang.ddframe.job.console.util.JobNodePath;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Service
public class ServerDimensionServiceImpl implements ServerDimensionService {
Expand All @@ -53,7 +51,8 @@ public Collection<ServerBriefInfo> getAllServersBriefInfo() {
List<String> servers = curatorRepository.getChildren(JobNodePath.getServerNodePath(jobName));
for (String server : servers) {
serverHostMap.put(server, curatorRepository.getData(JobNodePath.getServerNodePath(jobName, server, "hostName")));
if (curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, server, "status"))) {
if (!curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, server, "shutdown"))
&& curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, server, "status"))) {
serverAliveCountMap.put(server, true);
} else {
serverCrashedCountMap.put(server, true);
Expand Down Expand Up @@ -105,7 +104,8 @@ private JobServer getJob(final String serverIp, final String jobName) {
String status = curatorRepository.getData(JobNodePath.getServerNodePath(jobName, serverIp, "status"));
boolean disabled = curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, serverIp, "disabled"));
boolean stopped = curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, serverIp, "stoped"));
result.setStatus(ServerStatus.getServerStatus(status, disabled, stopped));
boolean shutdown = curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, serverIp, "shutdown"));
result.setStatus(ServerStatus.getServerStatus(status, disabled, stopped, shutdown));
String leaderIp = curatorRepository.getData(JobNodePath.getLeaderNodePath(jobName, "election/host"));
result.setLeader(serverIp.equals(leaderIp));
result.setLeaderStopped(curatorRepository.checkExists(JobNodePath.getServerNodePath(jobName, leaderIp, "stoped")));
Expand Down
27 changes: 22 additions & 5 deletions elastic-job-console/src/main/webapp/js/job_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $(function() {
bindStopAllButtons();
bindResumeButtons();
bindResumeAllButton();
bindShutdownButtons();
});

function renderSettings() {
Expand Down Expand Up @@ -81,21 +82,27 @@ function renderServers() {
}
var baseTd = "<td>" + data[i].ip + "</td><td>" + data[i].hostName + "</td><td>" + status + "</td><td>" + data[i].processSuccessCount + "</td><td>" + data[i].processFailureCount + "</td><td>" + data[i].sharding + "</td><td>" + (true === leader ? "<span class='glyphicon glyphicon-ok'></span>" : "<span class='glyphicon glyphicon-remove'></span>") + "</td>";
var operationTd;
if ("STOPED" === status) {
operationTd = "<td><button operation='resume' class='btn btn-success' ip='" + data[i].ip + "' leader='" + leader + "'>恢复</button></td>";
if ("SHUTDOWN" === status) {
operationTd = "-";
} else if ("STOPED" === status) {
operationTd = "<button operation='resume' class='btn btn-success' ip='" + data[i].ip + "' leader='" + leader + "'>恢复</button>";
} else if ("DISABLED" !== status && "CRASHED" !== status) {
operationTd = "<td><button operation='stop' class='btn btn-danger' ip='" + data[i].ip + "'" + (leader ? "data-toggle='modal' data-target='#stop-leader-confirm-dialog'" : "") + ">暂停</button></td>";
operationTd = "<button operation='stop' class='btn btn-warning' ip='" + data[i].ip + "'" + (leader ? "data-toggle='modal' data-target='#stop-leader-confirm-dialog'" : "") + ">暂停</button>";
} else {
operationTd = "<td>-</td>";
operationTd = "-";
}
if ("-" !== operationTd) {
operationTd = operationTd + "&nbsp;<button operation='shutdown' class='btn btn-danger' ip='" + data[i].ip + "'>关闭</button>";
}
operationTd = "<td>" + operationTd + "</td>";
var trClass = "";
if ("READY" === status) {
trClass = "info";
} else if ("RUNNING" === status) {
trClass = "success";
} else if ("DISABLED" === status || "STOPED" === status) {
trClass = "warning";
} else if ("CRASHED" === status) {
} else if ("CRASHED" === status || "SHUTDOWN" === status) {
trClass = "danger";
}
$("#servers tbody").append("<tr class='" + trClass + "'>" + baseTd + operationTd + "</tr>");
Expand Down Expand Up @@ -170,4 +177,14 @@ function renderExecution() {
$("#execution tbody").append("<tr class='" + trClass + "'>" + baseTd + "</tr>");
}
});
}

function bindShutdownButtons() {
$(document).on("click", "button[operation='shutdown']", function(event) {
var jobName = $("#job-name").text();
$.post("job/shutdown", {jobName : jobName, ip : $(event.currentTarget).attr("ip")}, function (data) {
renderServers();
showSuccessDialog();
});
});
}
31 changes: 25 additions & 6 deletions elastic-job-console/src/main/webapp/js/server_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $(function() {
bindResumeButtons();
bindStopAllButton();
bindResumeAllButton();
bindShutdownButtons();
});

function renderJobs() {
Expand All @@ -16,25 +17,31 @@ function renderJobs() {
var leader = data[i].leader;
var baseTd = "<td>" + data[i].jobName + "</td><td>" + status + "</td><td>" + data[i].processSuccessCount + "</td><td>" + data[i].processFailureCount + "</td><td>" + data[i].sharding + "</td><td>" + (true === leader ? "<span class='glyphicon glyphicon-ok'></span>" : "<span class='glyphicon glyphicon-remove'></span>") + "</td>";
var operationTd;
if ("STOPED" === status) {
if ("SHUTDOWN" === status) {
operationTd = "-";
} else if ("STOPED" === status) {
if (data[i].leaderStoped && !leader) {
operationTd = "<td><button operation='resume' class='btn btn-success disabled' job-name='" + data[i].jobName + "' disabled title='先恢复主节点才能恢复从节点作业'>恢复</button></td>";
operationTd = "<button operation='resume' class='btn btn-success disabled' job-name='" + data[i].jobName + "' disabled title='先恢复主节点才能恢复从节点作业'>恢复</button>";
} else {
operationTd = "<td><button operation='resume' class='btn btn-success' job-name='" + data[i].jobName + "'>恢复</button></td>";
operationTd = "<button operation='resume' class='btn btn-success' job-name='" + data[i].jobName + "'>恢复</button>";
}
} else if ("DISABLED" !== status && "CRASHED" !== status) {
operationTd = "<td><button operation='stop' class='btn btn-danger' job-name='" + data[i].jobName + "'" + (leader ? "data-toggle='modal' data-target='#stop-leader-confirm-dialog'" : "") + ">暂停</button></td>";
operationTd = "<button operation='stop' class='btn btn-warning' job-name='" + data[i].jobName + "'" + (leader ? "data-toggle='modal' data-target='#stop-leader-confirm-dialog'" : "") + ">暂停</button>";
} else {
operationTd = "<td>-</td>";
operationTd = "";
}
if ("-" !== operationTd) {
operationTd = operationTd + "&nbsp;<button operation='shutdown' class='btn btn-danger' job-name='" + data[i].jobName + "'>关闭</button>";
}
operationTd = "<td>" + operationTd + "</td>";
var trClass = "";
if ("READY" === status) {
trClass = "info";
} else if ("RUNNING" === status) {
trClass = "success";
} else if ("DISABLED" === status || "STOPED" === status) {
trClass = "warning";
} else if ("CRASHED" === status) {
} else if ("CRASHED" === status || "SHUTDOWN" === status) {
trClass = "danger";
}
$("#jobs tbody").append("<tr class='" + trClass + "'>" + baseTd + operationTd + "</tr>");
Expand Down Expand Up @@ -90,3 +97,15 @@ function bindResumeAllButton() {
});
});
}

function bindShutdownButtons() {
$(document).on("click", "button[operation='shutdown']", function(event) {
$.post("job/shutdown", {jobName : $(event.currentTarget).attr("job-name"), ip : $("#server-ip").text()}, function (data) {
renderJobs();
showSuccessDialog();
});
});
$(document).on("click", "button[operation='shutdown']", function(event) {
$("#chosen-job-name").text($(event.currentTarget).attr("job-name"));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<tbody>
</tbody>
</table>
<button id="stop-all-jobs-btn" class="btn btn-danger">全部暂停</button>
<button id="stop-all-jobs-btn" class="btn btn-warning">全部暂停</button>
<button id="resume-all-jobs-btn" class="btn btn-success">全部恢复</button>
</div>
<div role="tabpanel" class="tab-pane" id="execution_info">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<tbody>
</tbody>
</table>
<button id="stop-all-jobs-btn" class="btn btn-danger">全部暂停</button>
<button id="stop-all-jobs-btn" class="btn btn-warning">全部暂停</button>
<button id="resume-all-jobs-btn" class="btn btn-success">全部恢复</button>
<span id="chosen-job-name" class="hide"></span>
<@dashboard.successDialog "success-dialog" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ public Date getNextFireTime() {
*/
public void stopJob() {
try {
scheduler.pauseAll();
if (!scheduler.isShutdown()) {
scheduler.pauseAll();
}
} catch (final SchedulerException ex) {
throw new JobException(ex);
}
Expand All @@ -202,7 +204,9 @@ public void resumeJob() {
*/
public void triggerJob() {
try {
scheduler.triggerJob(jobDetail.getKey());
if (!scheduler.isShutdown()) {
scheduler.triggerJob(jobDetail.getKey());
}
} catch (final SchedulerException ex) {
throw new JobException(ex);
}
Expand All @@ -214,7 +218,9 @@ public void triggerJob() {
public void shutdown() {
schedulerFacade.releaseJobResource();
try {
scheduler.shutdown();
if (!scheduler.isShutdown()) {
scheduler.shutdown();
}
} catch (final SchedulerException ex) {
throw new JobException(ex);
}
Expand All @@ -225,7 +231,9 @@ public void shutdown() {
*/
public void rescheduleJob(final String cronExpression) {
try {
scheduler.rescheduleJob(TriggerKey.triggerKey(Joiner.on("_").join(jobName, CRON_TRIGGER_IDENTITY_SUFFIX)), createTrigger(cronExpression));
if (!scheduler.isShutdown()) {
scheduler.rescheduleJob(TriggerKey.triggerKey(Joiner.on("_").join(jobName, CRON_TRIGGER_IDENTITY_SUFFIX)), createTrigger(cronExpression));
}
} catch (final SchedulerException ex) {
throw new JobException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author zhangliang
*/
public class TimeService {

/**
* 获取当前时间的毫秒数.
*
Expand Down
Loading

0 comments on commit 9f6e974

Please sign in to comment.