Search in sources :

Example 31 with SaturnJobReturn

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;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn)

Example 32 with 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());
    }
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 33 with SaturnJobReturn

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;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn)

Example 34 with SaturnJobReturn

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;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) HashMap(java.util.HashMap)

Example 35 with 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;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException)

Aggregations

SaturnJobReturn (com.vip.saturn.job.SaturnJobReturn)35 LogbackListAppender (com.vip.saturn.job.executor.utils.LogbackListAppender)9 HashMap (java.util.HashMap)9 Test (org.junit.Test)9 Date (java.util.Date)4 JobInitAlarmException (com.vip.saturn.job.exception.JobInitAlarmException)2 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)2 ExecutorService (java.util.concurrent.ExecutorService)2 DemoService (com.vip.saturn.demo.service.DemoService)1 SaturnExecutionContext (com.vip.saturn.job.basic.SaturnExecutionContext)1 ShardingItemCallable (com.vip.saturn.job.basic.ShardingItemCallable)1 ExecutionInfo (com.vip.saturn.job.internal.control.ExecutionInfo)1 RegException (com.vip.saturn.job.reg.exception.RegException)1 ScriptJobRunner (com.vip.saturn.job.shell.ScriptJobRunner)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CommandLine (org.apache.commons.exec.CommandLine)1