mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: Add windows script support.#161
This commit is contained in:
parent
2ed0391d15
commit
65e0d118c3
@ -7,6 +7,8 @@ import com.github.kfcfans.powerjob.worker.log.OmsLogger;
|
|||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.commons.lang3.SystemUtils;
|
||||||
import tech.powerjob.official.processors.CommonBasicProcessor;
|
import tech.powerjob.official.processors.CommonBasicProcessor;
|
||||||
import tech.powerjob.official.processors.util.CommonUtils;
|
import tech.powerjob.official.processors.util.CommonUtils;
|
||||||
|
|
||||||
@ -20,47 +22,62 @@ import java.util.concurrent.ForkJoinPool;
|
|||||||
* 脚本处理器
|
* 脚本处理器
|
||||||
*
|
*
|
||||||
* @author tjq
|
* @author tjq
|
||||||
|
* @author Jiang Jining
|
||||||
* @since 2020/4/16
|
* @since 2020/4/16
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public abstract class AbstractScriptProcessor extends CommonBasicProcessor {
|
public abstract class AbstractScriptProcessor extends CommonBasicProcessor {
|
||||||
|
|
||||||
private static final ForkJoinPool pool = new ForkJoinPool(4 * Runtime.getRuntime().availableProcessors());
|
private static final ForkJoinPool POOL = new ForkJoinPool(4 * Runtime.getRuntime().availableProcessors());
|
||||||
private static final Set<String> DOWNLOAD_PROTOCOL = Sets.newHashSet("http", "https", "ftp");
|
private static final Set<String> DOWNLOAD_PROTOCOL = Sets.newHashSet("http", "https", "ftp");
|
||||||
|
static final String SH_SHELL = "/bin/sh";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ProcessResult process0(TaskContext context) throws Exception {
|
protected ProcessResult process0(TaskContext context) throws Exception {
|
||||||
OmsLogger omsLogger = context.getOmsLogger();
|
OmsLogger omsLogger = context.getOmsLogger();
|
||||||
omsLogger.info("SYSTEM ===> ScriptProcessor start to process");
|
omsLogger.info("SYSTEM ===> ScriptProcessor start to process");
|
||||||
|
String scriptParams = CommonUtils.parseParams(context);
|
||||||
String scriptPath = prepareScriptFile(context.getInstanceId(), CommonUtils.parseParams(context));
|
if (scriptParams == null) {
|
||||||
|
String message = "scriptParams is null, please check jobParam configuration.";
|
||||||
// 1. 授权
|
omsLogger.warn(message);
|
||||||
ProcessBuilder chmodPb = new ProcessBuilder("/bin/chmod", "755", scriptPath);
|
return new ProcessResult(false, message);
|
||||||
// 等待返回,这里不可能导致死锁(shell产生大量数据可能导致死锁)
|
}
|
||||||
chmodPb.start().waitFor();
|
String scriptPath = prepareScriptFile(context.getInstanceId(), scriptParams);
|
||||||
|
|
||||||
|
if (SystemUtils.IS_OS_WINDOWS) {
|
||||||
|
if (StringUtils.equals(getRunCommand(), SH_SHELL)) {
|
||||||
|
String message = String.format("Current OS is %s where shell scripts cannot run.", SystemUtils.OS_NAME);
|
||||||
|
omsLogger.warn(message);
|
||||||
|
return new ProcessResult(false, message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 1. 授权
|
||||||
|
ProcessBuilder chmodPb = new ProcessBuilder("/bin/chmod", "755", scriptPath);
|
||||||
|
// 等待返回,这里不可能导致死锁(shell产生大量数据可能导致死锁)
|
||||||
|
chmodPb.start().waitFor();
|
||||||
|
}
|
||||||
|
|
||||||
// 2. 执行目标脚本
|
// 2. 执行目标脚本
|
||||||
ProcessBuilder pb = new ProcessBuilder(getRunCommand(), scriptPath);
|
ProcessBuilder pb = new ProcessBuilder(getRunCommand(), scriptPath);
|
||||||
Process process = pb.start();
|
Process process = pb.start();
|
||||||
|
|
||||||
StringBuilder inputSB = new StringBuilder();
|
StringBuilder inputBuilder = new StringBuilder();
|
||||||
StringBuilder errorSB = new StringBuilder();
|
StringBuilder errorBuilder = new StringBuilder();
|
||||||
|
|
||||||
boolean success = true;
|
boolean success = true;
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
try (InputStream is = process.getInputStream(); InputStream es = process.getErrorStream()) {
|
try (InputStream is = process.getInputStream(); InputStream es = process.getErrorStream()) {
|
||||||
|
|
||||||
pool.execute(() -> copyStream(is, inputSB, omsLogger));
|
POOL.execute(() -> copyStream(is, inputBuilder, omsLogger));
|
||||||
pool.execute(() -> copyStream(es, errorSB, omsLogger));
|
POOL.execute(() -> copyStream(es, errorBuilder, omsLogger));
|
||||||
|
|
||||||
success = process.waitFor() == 0;
|
success = process.waitFor() == 0;
|
||||||
|
|
||||||
} catch (InterruptedException ie) {
|
} catch (InterruptedException ie) {
|
||||||
omsLogger.info("SYSTEM ===> ScriptProcessor has been interrupted");
|
omsLogger.info("SYSTEM ===> ScriptProcessor has been interrupted");
|
||||||
} finally {
|
} finally {
|
||||||
result = String.format("[INPUT]: %s;[ERROR]: %s", inputSB.toString(), errorSB.toString());
|
result = String.format("[INPUT]: %s;[ERROR]: %s", inputBuilder.toString(), errorBuilder.toString());
|
||||||
}
|
}
|
||||||
return new ProcessResult(success, result);
|
return new ProcessResult(success, result);
|
||||||
}
|
}
|
||||||
@ -112,6 +129,7 @@ public abstract class AbstractScriptProcessor extends CommonBasicProcessor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成脚本名称
|
* 生成脚本名称
|
||||||
|
* @param instanceId id of instance
|
||||||
* @return 文件名称
|
* @return 文件名称
|
||||||
*/
|
*/
|
||||||
protected abstract String getScriptName(Long instanceId);
|
protected abstract String getScriptName(Long instanceId);
|
||||||
|
@ -15,6 +15,6 @@ public class ShellProcessor extends AbstractScriptProcessor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getRunCommand() {
|
protected String getRunCommand() {
|
||||||
return "/bin/sh";
|
return SH_SHELL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user