|
|
package com.monitor.nginxsync.job;
|
|
|
|
|
|
|
|
|
import com.monitor.common.ProjectConstant;
|
|
|
import com.monitor.nginxsync.model.TaskInfo;
|
|
|
import org.apache.commons.exec.*;
|
|
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/8/22.
|
|
|
*/
|
|
|
public class ShellJob implements Callable {
|
|
|
|
|
|
public ExecutorService service = Executors.newSingleThreadExecutor();
|
|
|
|
|
|
public TaskInfo cmdInfo;
|
|
|
|
|
|
public ShellJob(TaskInfo cmdInfo) {
|
|
|
this.cmdInfo = cmdInfo;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Object call() throws Exception {
|
|
|
//ssh master@10.xxx /bin/bash /home/master/nginx-sync/xxx.sh args
|
|
|
CommandLine commandLine = CommandLine.parse("ssh master@"+ ProjectConstant.DEPLOY_IP+" "+cmdInfo.getCmd());
|
|
|
|
|
|
DefaultExecutor executor = new DefaultExecutor();
|
|
|
|
|
|
ExecuteWatchdog watchdog = new ExecuteWatchdog(5 * 60000);
|
|
|
|
|
|
CollectionLogOutputStream outputStream = new CollectionLogOutputStream(cmdInfo.getTaskId());
|
|
|
|
|
|
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, outputStream);
|
|
|
|
|
|
executor.setStreamHandler(pumpStreamHandler);
|
|
|
|
|
|
executor.setWatchdog(watchdog);
|
|
|
|
|
|
executor.setExitValue(0);
|
|
|
|
|
|
service.submit(new LogJob(outputStream));
|
|
|
|
|
|
executor.execute(commandLine, new ExitHandler(outputStream, service));
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public static class ExitHandler implements ExecuteResultHandler {
|
|
|
|
|
|
private CollectionLogOutputStream outputStream;
|
|
|
|
|
|
private ExecutorService service;
|
|
|
|
|
|
public ExitHandler(CollectionLogOutputStream outputStream, ExecutorService service) {
|
|
|
|
|
|
this.outputStream = outputStream;
|
|
|
this.service = service;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onProcessComplete(int i) {
|
|
|
|
|
|
outputStream.getIsFinished().set(1);
|
|
|
|
|
|
service.shutdown();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onProcessFailed(ExecuteException e) {
|
|
|
|
|
|
//执行异常的日志
|
|
|
outputStream.addErrorLog(e.getMessage());
|
|
|
|
|
|
outputStream.getIsFinished().set(1);
|
|
|
|
|
|
service.shutdown();
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|