use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class SaturnScriptJob method innerHandle.
protected SaturnJobReturn innerHandle(ShardingItemCallable callable) {
SaturnJobReturn saturnJobReturn = null;
try {
String saturnOutputPath = String.format(ScriptPidUtils.JOBITEMOUTPUTPATH, callable.getShardingContext().getExecutorName(), jobName, callable.getItem(), random.nextInt(10000), System.currentTimeMillis());
callable.getEnvMap().put(SystemEnvProperties.NAME_VIP_SATURN_OUTPUT_PATH, saturnOutputPath);
ScriptJobRunner scriptJobRunner = new ScriptJobRunner(callable.getEnvMap(), this, callable.getItem(), callable.getItemValue(), callable.getShardingContext());
SaturnExecuteWatchdog watchDog = scriptJobRunner.getWatchdog();
synchronized (watchDogList) {
watchDogList.add(watchDog);
}
saturnJobReturn = scriptJobRunner.runJob();
synchronized (watchDogLock) {
watchDogList.remove(watchDog);
}
callable.setBusinessReturned(scriptJobRunner.isBusinessReturned());
} catch (Throwable t) {
LogUtils.error(log, jobName, t.getMessage(), t);
saturnJobReturn = new SaturnJobReturn(SaturnSystemReturnCode.USER_FAIL, t.getMessage(), SaturnSystemErrorGroup.FAIL);
}
return saturnJobReturn;
}
use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class ShardingItemFutureTask method call.
@Override
public SaturnJobReturn call() throws Exception {
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
if (e instanceof IllegalMonitorStateException || e instanceof ThreadDeath) {
LogUtils.warn(log, callable.getJobName(), "business thread pool maybe crashed", e);
if (callFuture != null) {
callFuture.cancel(false);
}
LogUtils.warn(log, callable.getJobName(), "close the old business thread pool, and re-create new one");
callable.getSaturnJob().getJobScheduler().reCreateExecutorService();
}
}
});
try {
SaturnJobReturn ret = callable.call();
return ret;
} finally {
done();
LogUtils.debug(log, callable.getJobName(), "job:[{}] item:[{}] finish execution, which takes {}ms", callable.getJobName(), callable.getItem(), callable.getExecutionTime());
}
}
use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class ScriptJobRunner method readSaturnJobReturn.
private SaturnJobReturn readSaturnJobReturn() {
SaturnJobReturn tmp = null;
if (saturnOutputFile != null && saturnOutputFile.exists()) {
try {
String fileContents = FileUtils.readFileToString(saturnOutputFile);
if (StringUtils.isNotBlank(fileContents)) {
tmp = JsonUtils.getGson().fromJson(fileContents.trim(), SaturnJobReturn.class);
// 脚本成功返回数据
businessReturned = true;
}
} catch (Throwable t) {
LogUtils.error(log, jobName, "{} - {} read SaturnJobReturn from {} error", jobName, item, saturnOutputFile.getAbsolutePath(), t);
tmp = new SaturnJobReturn(SaturnSystemReturnCode.USER_FAIL, "Exception: " + t, SaturnSystemErrorGroup.FAIL);
}
}
return tmp;
}
use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class ScriptJobRunner method runJob.
public SaturnJobReturn runJob() {
SaturnJobReturn saturnJobReturn = null;
long timeoutSeconds = saturnExecutionContext.getTimetoutSeconds();
try {
createSaturnJobReturnFile();
saturnJobReturn = execute(timeoutSeconds);
} catch (Throwable t) {
LogUtils.error(log, jobName, "{} - {} Exception", jobName, item, t);
saturnJobReturn = new SaturnJobReturn(SaturnSystemReturnCode.SYSTEM_FAIL, "Exception: " + t, SaturnSystemErrorGroup.FAIL);
} finally {
FileUtils.deleteQuietly(saturnOutputFile.getParentFile());
}
if (saturnJobReturn.getProp() == null) {
saturnJobReturn.setProp(new HashMap());
}
return saturnJobReturn;
}
use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class ScriptJobRunner method execute.
private SaturnJobReturn execute(long timeoutSeconds) {
SaturnJobReturn saturnJobReturn;
ProcessOutputStream processOutputStream = new ProcessOutputStream(1);
DefaultExecutor executor = new DefaultExecutor();
PumpStreamHandler streamHandler = new PumpStreamHandler(processOutputStream);
// 关闭线程等待时间, (注意commons-exec会固定增加2秒的addition)
streamHandler.setStopTimeout(timeoutSeconds * 1000);
executor.setExitValue(0);
executor.setStreamHandler(streamHandler);
executor.setWatchdog(getWatchdog());
// filter env key in execParameter. like cd ${mypath} -> cd /root/my.
Map<String, String> env = ScriptPidUtils.loadEnv();
CommandLine commandLine = createCommandLine(env);
try {
long start = System.currentTimeMillis();
LogUtils.info(log, jobName, "Begin executing {}-{} {}", jobName, item, commandLine);
int exitValue = executor.execute(commandLine, env);
long end = System.currentTimeMillis();
LogUtils.info(log, jobName, "Finish executing {}-{} {}, the exit value is {}, cost={}ms", jobName, item, commandLine, exitValue, (end - start));
SaturnJobReturn tmp = readSaturnJobReturn();
if (tmp == null) {
tmp = new SaturnJobReturn("the exit value is " + exitValue);
}
saturnJobReturn = tmp;
} catch (Exception e) {
saturnJobReturn = handleException(timeoutSeconds, e);
} finally {
try {
// 将日志set进jobLog, 写不写zk再由ExecutionService控制
handleJobLog(processOutputStream.getJobLog());
processOutputStream.close();
} catch (Exception ex) {
LogUtils.error(log, jobName, "{}-{} Error at closing output stream. Should not be concern: {}", jobName, item, ex.getMessage(), ex);
}
stopStreamHandler(streamHandler);
ScriptPidUtils.removePidFile(job.getExecutorName(), jobName, "" + item, watchdog.getPid());
}
return saturnJobReturn;
}
Aggregations