Search in sources :

Example 26 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class AbstractSaturnJob method mayRunDownStream.

@Override
protected boolean mayRunDownStream(JobExecutionMultipleShardingContext shardingContext) {
    if (!super.mayRunDownStream(shardingContext)) {
        return false;
    }
    // 只要有一个失败,就不触发下游
    if (shardingContext instanceof SaturnExecutionContext) {
        SaturnExecutionContext saturnContext = (SaturnExecutionContext) shardingContext;
        Map<Integer, SaturnJobReturn> shardingItemResults = saturnContext.getShardingItemResults();
        if (shardingItemResults != null && !shardingItemResults.isEmpty()) {
            Iterator<Map.Entry<Integer, SaturnJobReturn>> iterator = shardingItemResults.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, SaturnJobReturn> next = iterator.next();
                Integer item = next.getKey();
                SaturnJobReturn saturnJobReturn = next.getValue();
                if (saturnJobReturn.getErrorGroup() != SaturnSystemErrorGroup.SUCCESS) {
                    LogUtils.warn(log, jobName, "item {} ran unsuccessfully, SaturnJobReturn is {}, wont run downStream", item, saturnJobReturn, item);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn)

Example 27 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class LongtimeJavaJob method handleJavaJob.

@Override
public SaturnJobReturn handleJavaJob(String jobName, Integer shardItem, String shardParam, SaturnJobExecutionContext shardingContext) {
    String key = jobName + "_" + shardItem;
    JobStatus status = statusMap.get(key);
    status.running = true;
    System.out.println(new Date() + " running:" + jobName + "; " + shardItem + ";" + shardParam + ";finished:" + status.finished);
    try {
        Thread.sleep(status.sleepSeconds * 1000);
        status.runningCount++;
    } catch (InterruptedException e) {
        status.interrupted = true;
        System.out.println("i am terminating..");
    } finally {
        status.finished = true;
    }
    return new SaturnJobReturn();
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) Date(java.util.Date)

Example 28 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class SaturnJavaJob method doExecution.

public SaturnJobReturn doExecution(final String jobName, final Integer key, final String value, SaturnExecutionContext shardingContext, final JavaShardingItemCallable callable) throws Throwable {
    String jobClass = shardingContext.getJobConfiguration().getJobClass();
    LogUtils.info(log, jobName, "Running SaturnJavaJob,  jobClass [{}], item [{}]", jobClass, key);
    try {
        Object ret = new JobBusinessClassMethodCaller() {

            @Override
            protected Object internalCall(ClassLoader jobClassLoader, Class<?> saturnJobExecutionContextClazz) throws Exception {
                return jobBusinessInstance.getClass().getMethod("handleJavaJob", String.class, Integer.class, String.class, saturnJobExecutionContextClazz).invoke(jobBusinessInstance, jobName, key, value, callable.getContextForJob(jobClassLoader));
            }
        }.call(jobBusinessInstance, saturnExecutorService);
        SaturnJobReturn saturnJobReturn = (SaturnJobReturn) JavaShardingItemCallable.cloneObject(ret, saturnExecutorService.getExecutorClassLoader());
        if (saturnJobReturn != null) {
            callable.setBusinessReturned(true);
        }
        return saturnJobReturn;
    } catch (Exception e) {
        if (e.getCause() instanceof ThreadDeath) {
            throw e.getCause();
        }
        String message = logBusinessExceptionIfNecessary(jobName, e);
        return new SaturnJobReturn(SaturnSystemReturnCode.USER_FAIL, message, SaturnSystemErrorGroup.FAIL);
    }
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) JobInitAlarmException(com.vip.saturn.job.exception.JobInitAlarmException)

Example 29 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class SaturnJavaJob method handleJob.

@Override
protected Map<Integer, SaturnJobReturn> handleJob(final SaturnExecutionContext shardingContext) {
    final Map<Integer, SaturnJobReturn> retMap = new HashMap<Integer, SaturnJobReturn>();
    synchronized (futureTaskMap) {
        futureTaskMap.clear();
        final String jobName = shardingContext.getJobName();
        final int timeoutSeconds = getTimeoutSeconds();
        ExecutorService executorService = getExecutorService();
        // 处理自定义参数
        String jobParameter = shardingContext.getJobParameter();
        // shardingItemParameters为参数表解析出来的Key/Value值
        Map<Integer, String> shardingItemParameters = shardingContext.getShardingItemParameters();
        for (final Entry<Integer, String> shardingItem : shardingItemParameters.entrySet()) {
            final Integer key = shardingItem.getKey();
            try {
                String jobValue = shardingItem.getValue();
                // 作业分片的对应值
                final String itemVal = getRealItemValue(jobParameter, jobValue);
                ShardingItemFutureTask shardingItemFutureTask = new ShardingItemFutureTask(createCallable(jobName, key, itemVal, timeoutSeconds, shardingContext, this), null);
                Future<?> callFuture = executorService.submit(shardingItemFutureTask);
                if (timeoutSeconds > 0) {
                    TimeoutSchedulerExecutor.scheduleTimeoutJob(shardingContext.getExecutorName(), timeoutSeconds, shardingItemFutureTask);
                }
                shardingItemFutureTask.setCallFuture(callFuture);
                futureTaskMap.put(key, shardingItemFutureTask);
            } catch (Throwable t) {
                LogUtils.error(log, jobName, t.getMessage(), t);
                retMap.put(key, new SaturnJobReturn(SaturnSystemReturnCode.SYSTEM_FAIL, t.getMessage(), SaturnSystemErrorGroup.FAIL));
            }
        }
    }
    for (Entry<Integer, ShardingItemFutureTask> entry : futureTaskMap.entrySet()) {
        Integer item = entry.getKey();
        ShardingItemFutureTask futureTask = entry.getValue();
        try {
            futureTask.getCallFuture().get();
        } catch (Exception e) {
            LogUtils.error(log, jobName, e.getMessage(), e);
            retMap.put(item, new SaturnJobReturn(SaturnSystemReturnCode.SYSTEM_FAIL, e.getMessage(), SaturnSystemErrorGroup.FAIL));
            continue;
        }
        retMap.put(item, futureTask.getCallable().getSaturnJobReturn());
    }
    synchronized (futureTaskMap) {
        futureTaskMap.clear();
    }
    return retMap;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) HashMap(java.util.HashMap) JobInitAlarmException(com.vip.saturn.job.exception.JobInitAlarmException) ExecutorService(java.util.concurrent.ExecutorService)

Example 30 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class SaturnScriptJob method handleJob.

@Override
public Map<Integer, SaturnJobReturn> handleJob(final SaturnExecutionContext shardingContext) {
    synchronized (watchDogList) {
        watchDogList.clear();
    }
    shardingItemCallableList.clear();
    final Map<Integer, SaturnJobReturn> retMap = new ConcurrentHashMap<Integer, SaturnJobReturn>();
    Map<Integer, String> shardingItemParameters = shardingContext.getShardingItemParameters();
    final String jobName = shardingContext.getJobName();
    ExecutorService executorService = getExecutorService();
    // 处理自定义参数
    String jobParameter = shardingContext.getJobParameter();
    final CountDownLatch latch = new CountDownLatch(shardingItemParameters.size());
    for (final Entry<Integer, String> shardingItem : shardingItemParameters.entrySet()) {
        final Integer key = shardingItem.getKey();
        String jobValue = shardingItem.getValue();
        // 作业分片的对应值
        final String execParameter = getRealItemValue(jobParameter, jobValue);
        LogUtils.debug(log, jobName, "jobname={}, key= {}, jobParameter={}", jobName, key, execParameter);
        executorService.submit(new Runnable() {

            @Override
            public void run() {
                SaturnJobReturn jobReturn = null;
                try {
                    jobReturn = innerHandleWithListener(jobName, key, execParameter, shardingContext);
                } catch (Throwable e) {
                    LogUtils.error(log, jobName, e.getMessage(), e);
                    jobReturn = new SaturnJobReturn(SaturnSystemReturnCode.USER_FAIL, "Error: " + e.getMessage(), SaturnSystemErrorGroup.FAIL);
                } finally {
                    retMap.put(key, jobReturn);
                    latch.countDown();
                }
            }
        });
    }
    try {
        latch.await();
    } catch (final InterruptedException ex) {
        LogUtils.error(log, jobName, "SaturnScriptJob: Job {} is interrupted", jobName);
        Thread.currentThread().interrupt();
    }
    return retMap;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

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