Search in sources :

Example 1 with Predictor

use of it.sauronsoftware.cron4j.Predictor in project adempiere by adempiere.

the class Scheduler method run.

//	getServerInfo
@Override
public void run() {
    String cronPattern = (String) m_model.getCronPattern();
    if (cronPattern != null && cronPattern.trim().length() > 0 && SchedulingPattern.validate(cronPattern)) {
        cronScheduler = new it.sauronsoftware.cron4j.Scheduler();
        cronScheduler.schedule(cronPattern, new Runnable() {

            public void run() {
                runNow();
                long next = predictor.nextMatchingTime();
                p_model.setDateNextRun(new Timestamp(next));
                p_model.saveEx();
            }
        });
        predictor = new Predictor(cronPattern);
        long next = predictor.nextMatchingTime();
        p_model.setDateNextRun(new Timestamp(next));
        p_model.saveEx();
        cronScheduler.start();
        while (true) {
            if (!sleep()) {
                cronScheduler.stop();
                break;
            } else if (!cronScheduler.isStarted()) {
                break;
            }
        }
    } else {
        super.run();
    }
}
Also used : Predictor(it.sauronsoftware.cron4j.Predictor) Timestamp(java.sql.Timestamp)

Example 2 with Predictor

use of it.sauronsoftware.cron4j.Predictor in project ignite by apache.

the class ScheduleFutureImpl method nextExecutionTimes.

/**
 * {@inheritDoc}
 */
@Override
public long[] nextExecutionTimes(int cnt, long start) {
    assert cnt > 0;
    assert start > 0;
    if (isDone() || isCancelled())
        return EMPTY_TIMES;
    synchronized (mux) {
        if (maxCalls > 0)
            cnt = Math.min(cnt, maxCalls);
    }
    long[] times = new long[cnt];
    if (start < createTime() + delay * 1000)
        start = createTime() + delay * 1000;
    SchedulingPattern ptrn = new SchedulingPattern(cron);
    Predictor p = new Predictor(ptrn, start);
    for (int i = 0; i < cnt; i++) times[i] = p.nextMatchingTime();
    return times;
}
Also used : Predictor(it.sauronsoftware.cron4j.Predictor) SchedulingPattern(it.sauronsoftware.cron4j.SchedulingPattern)

Example 3 with Predictor

use of it.sauronsoftware.cron4j.Predictor in project scheduling by ow2-proactive.

the class InternalJob method assignReplicationTag.

/**
 * Assign a tag to new duplicated task because of a REPLICATE or LOOP.
 *
 * @param replicatedTask the new duplicated task.
 * @param initiator      the initiator of the duplication.
 * @param loopAction     true if the duplication if after a loop or, false if it is a
 *                       replicate.
 * @param action         the duplication action.
 */
private void assignReplicationTag(InternalTask replicatedTask, InternalTask initiator, boolean loopAction, FlowAction action) {
    StringBuilder buf = new StringBuilder();
    if (loopAction) {
        buf.append("LOOP-");
        buf.append(InternalTask.getInitialName(initiator.getName()));
        if (initiator.getReplicationIndex() > 0) {
            buf.append("*");
            buf.append(initiator.getReplicationIndex());
        }
    } else {
        buf.append("REPLICATE-");
        buf.append(initiator.getName());
    }
    buf.append("-");
    if (loopAction) {
        String cronExpr = action.getCronExpr();
        if (cronExpr.isEmpty()) {
            buf.append(replicatedTask.getIterationIndex());
        } else {
            // cron task: the replication index is the next date that
            // matches the cron expression
            Date resolvedCron = new Predictor(cronExpr).nextMatchingDate();
            SimpleDateFormat dt = new SimpleDateFormat("dd_MM_YY_HH_mm");
            buf.append(dt.format(resolvedCron));
        }
    } else {
        buf.append(replicatedTask.getReplicationIndex());
    }
    replicatedTask.setTag(buf.toString());
}
Also used : Predictor(it.sauronsoftware.cron4j.Predictor) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with Predictor

use of it.sauronsoftware.cron4j.Predictor in project scheduling by ow2-proactive.

the class TerminateLoopHandler method terminateLoopTask.

public boolean terminateLoopTask(FlowAction action, InternalTask initiator, ChangedTasksInfo changesInfo, SchedulerStateUpdate frontend) {
    // find the target of the loop
    InternalTask target = null;
    if (action.getTarget().equals(initiator.getName())) {
        target = initiator;
    } else {
        target = internalJob.findTaskUp(action.getTarget(), initiator);
    }
    boolean replicateForNextLoopIteration = internalJob.replicateForNextLoopIteration(initiator, target, changesInfo, frontend, action);
    if (replicateForNextLoopIteration && action.getCronExpr() != null) {
        for (TaskId tid : changesInfo.getNewTasks()) {
            InternalTask newTask = internalJob.getIHMTasks().get(tid);
            try {
                Date startAt = (new Predictor(action.getCronExpr())).nextMatchingDate();
                newTask.addGenericInformation(InternalJob.GENERIC_INFO_START_AT_KEY, ISO8601DateUtil.parse(startAt));
                newTask.setScheduledTime(startAt.getTime());
            } catch (InvalidPatternException e) {
                // this will not happen as the cron expression is
                // already being validated in FlowScript class.
                LOGGER.debug(e.getMessage());
            }
        }
    }
    return replicateForNextLoopIteration;
}
Also used : TaskId(org.ow2.proactive.scheduler.common.task.TaskId) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) Predictor(it.sauronsoftware.cron4j.Predictor) Date(java.util.Date)

Example 5 with Predictor

use of it.sauronsoftware.cron4j.Predictor in project scheduling by ow2-proactive.

the class FlowScript method getResult.

@Override
protected ScriptResult<FlowAction> getResult(Object evalResult, Bindings bindings) {
    try {
        FlowAction act = new FlowAction();
        /*
             * no action defined
             */
        if (this.actionType == null || this.actionType.equals(FlowActionType.CONTINUE.toString())) {
            act.setType(FlowActionType.CONTINUE);
        } else /*
             * loop
             */
        if (this.actionType.equals(FlowActionType.LOOP.toString())) {
            if (this.target == null) {
                String msg = "LOOP control flow action requires a target";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else {
                if (bindings.containsKey(loopVariable)) {
                    Boolean enabled;
                    String loopValue = bindings.get(loopVariable).toString();
                    if ("true".equalsIgnoreCase(loopValue)) {
                        enabled = Boolean.TRUE;
                    } else if ("false".equalsIgnoreCase(loopValue)) {
                        enabled = Boolean.FALSE;
                    } else {
                        try {
                            (new Predictor(loopValue)).nextMatchingDate();
                            enabled = Boolean.TRUE;
                            act.setCronExpr(loopValue);
                        } catch (InvalidPatternException e) {
                            enabled = Boolean.FALSE;
                        }
                    }
                    if (enabled) {
                        act.setType(FlowActionType.LOOP);
                        act.setTarget(this.target);
                    } else {
                        act.setType(FlowActionType.CONTINUE);
                    }
                } else {
                    String msg = "Script environment for LOOP action needs to define variable " + loopVariable;
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
            }
        } else /*
             * replicate
             */
        if (this.actionType.equals(FlowActionType.REPLICATE.toString())) {
            if (bindings.containsKey(replicateRunsVariable)) {
                act.setType(FlowActionType.REPLICATE);
                int args = 1;
                Object o = bindings.get(replicateRunsVariable);
                try {
                    args = Integer.parseInt("" + o);
                } catch (NumberFormatException e) {
                    try {
                        args = (int) Math.floor(Double.parseDouble("" + o));
                    } catch (Exception e2) {
                        String msg = "REPLICATE action: could not parse value for variable " + replicateRunsVariable;
                        logger.error(msg);
                        return new ScriptResult<FlowAction>(new Exception(msg, e2));
                    }
                }
                if (args < 0) {
                    String msg = "REPLICATE action: value of variable " + replicateRunsVariable + " cannot be negative";
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
                act.setDupNumber(args);
            } else {
                String msg = "Script environment for REPLICATE action needs to define variable " + replicateRunsVariable;
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            }
        } else /*
             * if
             */
        if (this.actionType.equals(FlowActionType.IF.toString())) {
            if (this.target == null) {
                String msg = "IF action requires a target ";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else if (this.targetElse == null) {
                String msg = "IF action requires an ELSE target ";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else {
                act.setType(FlowActionType.IF);
                if (bindings.containsKey(branchSelectionVariable)) {
                    String val = new String((String) bindings.get(branchSelectionVariable));
                    if (val.toLowerCase().equals(ifBranchSelectedVariable)) {
                        act.setTarget(this.target);
                        act.setTargetElse(this.targetElse);
                    } else if (val.toLowerCase().equals(elseBranchSelectedVariable)) {
                        act.setTarget(this.targetElse);
                        act.setTargetElse(this.target);
                    } else {
                        String msg = "IF action: value for " + branchSelectionVariable + " needs to be one of " + ifBranchSelectedVariable + " or " + elseBranchSelectedVariable;
                        logger.error(msg);
                        return new ScriptResult<FlowAction>(new Exception(msg));
                    }
                } else {
                    String msg = "Environment for IF action needs to define variable " + branchSelectionVariable;
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
                if (this.targetContinuation != null) {
                    act.setTargetContinuation(this.targetContinuation);
                }
            }
        } else /*
             * unknown action
             */
        {
            String msg = actionType + " action type unknown";
            logger.error(msg);
            return new ScriptResult<FlowAction>(new Exception(msg));
        }
        return new ScriptResult<FlowAction>(act);
    } catch (Throwable th) {
        return new ScriptResult<FlowAction>(th);
    }
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException) Predictor(it.sauronsoftware.cron4j.Predictor) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException)

Aggregations

Predictor (it.sauronsoftware.cron4j.Predictor)6 Date (java.util.Date)3 InvalidPatternException (it.sauronsoftware.cron4j.InvalidPatternException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 SchedulingPattern (it.sauronsoftware.cron4j.SchedulingPattern)1 Timestamp (java.sql.Timestamp)1 TaskId (org.ow2.proactive.scheduler.common.task.TaskId)1 InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)1 InvalidScriptException (org.ow2.proactive.scripting.InvalidScriptException)1 ScriptResult (org.ow2.proactive.scripting.ScriptResult)1