/** * Create an internal native Task with the given native task (user) * * @param task the user native task that will be used to create the internal native task. * @return the created internal task. * @throws JobCreationException an exception if the factory cannot create the given task. */ private static InternalTask createTask(Job userJob, NativeTask task) throws JobCreationException { if (((task.getCommandLine() == null) || (task.getCommandLine().length == 0))) { String msg = "The command line is null or empty and not generated !"; logger.info(msg); throw new JobCreationException(msg); } try { String commandAndArguments = "\"" + Joiner.on("\" \"").join(task.getCommandLine()) + "\""; InternalTask scriptTask; if (isForkingTask()) { scriptTask = new InternalForkedScriptTask( new ScriptExecutableContainer( new TaskScript(new SimpleScript(commandAndArguments, "native")))); configureRunAsMe(task); } else { scriptTask = new InternalScriptTask( new ScriptExecutableContainer( new TaskScript(new SimpleScript(commandAndArguments, "native")))); } ForkEnvironment forkEnvironment = new ForkEnvironment(); scriptTask.setForkEnvironment(forkEnvironment); // set task common properties setTaskCommonProperties(userJob, task, scriptTask); return scriptTask; } catch (Exception e) { throw new JobCreationException(e); } }
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; }
/** * Instantiate a new JobResult with a jobId and a result * * @param id the jobId associated with this result */ public JobResultImpl(InternalJob job) { this.jobInfo = job.getJobInfo(); this.allResults = new HashMap<String, TaskResult>(job.getTasks().size()); this.preciousResults = new HashMap<String, TaskResult>(); for (InternalTask it : job.getIHMTasks().values()) { this.allResults.put(it.getName(), null); } }
void initTaskType(InternalTask task) { if (task.getClass().equals(InternalForkedScriptTask.class)) { taskType = FORKED_SCRIPT_TASK; } else if (task.getClass().equals(InternalScriptTask.class)) { taskType = SCRIPT_TASK; } else { throw new IllegalArgumentException("Unexpected task type: " + task.getClass()); } }
/** * Create an internal java Task with the given java task (user) * * @param task the user java task that will be used to create the internal java task. * @return the created internal task. * @throws JobCreationException an exception if the factory cannot create the given task. */ @SuppressWarnings("unchecked") private static InternalTask createTask(Job userJob, JavaTask task) throws JobCreationException { InternalTask javaTask; if (task.getExecutableClassName() != null) { HashMap<String, byte[]> args = task.getSerializedArguments(); try { if (isForkingTask()) { javaTask = new InternalForkedScriptTask( new ScriptExecutableContainer( new TaskScript( new SimpleScript( task.getExecutableClassName(), JavaClassScriptEngineFactory.JAVA_CLASS_SCRIPT_ENGINE_NAME, new Serializable[] {args})))); javaTask.setForkEnvironment(task.getForkEnvironment()); configureRunAsMe(task); } else { javaTask = new InternalScriptTask( new ScriptExecutableContainer( new TaskScript( new SimpleScript( task.getExecutableClassName(), JavaClassScriptEngineFactory.JAVA_CLASS_SCRIPT_ENGINE_NAME, new Serializable[] {args})))); } } catch (InvalidScriptException e) { throw new JobCreationException(e); } } else { String msg = "You must specify your own executable task class to be launched (in every task)!"; logger.info(msg); throw new JobCreationException(msg); } // set task common properties try { setTaskCommonProperties(userJob, task, javaTask); } catch (Exception e) { throw new JobCreationException(e); } return javaTask; }
void updateMutableAttributes(InternalTask task) { setTaskName(task.getName()); setStartTime(task.getStartTime()); setFinishedTime(task.getFinishedTime()); setIteration(task.getIterationIndex()); setReplication(task.getReplicationIndex()); setMatchingBlock(task.getMatchingBlock()); setTaskStatus(task.getStatus()); setExecutionDuration(task.getExecutionDuration()); }
/** * Set some properties between the user task and internal task. * * @param task the user task. * @param taskToSet the internal task to set. * @throws IllegalAccessException * @throws IllegalArgumentException */ private static void setTaskCommonProperties(Job userJob, Task task, InternalTask taskToSet) throws IllegalArgumentException, IllegalAccessException { autoCopyfields(CommonAttribute.class, task, taskToSet); autoCopyfields(Task.class, task, taskToSet); // special behavior if (onErrorPolicyInterpreter.notSetOrNone(task)) { taskToSet.setOnTaskError(userJob.getOnTaskErrorProperty().getValue()); } else { taskToSet.setOnTaskError(task.getOnTaskErrorProperty().getValue()); } if (task.getRestartTaskOnErrorProperty().isSet()) { taskToSet.setRestartTaskOnError(task.getRestartTaskOnError()); } else { taskToSet.setRestartTaskOnError(userJob.getRestartTaskOnError()); } if (task.getMaxNumberOfExecutionProperty().isSet()) { taskToSet.setMaxNumberOfExecution(task.getMaxNumberOfExecution()); } else { taskToSet.setMaxNumberOfExecution(userJob.getMaxNumberOfExecution()); } }
InternalTask toInternalTask(InternalJob internalJob) throws InvalidScriptException { TaskId taskId = createTaskId(internalJob); InternalTask internalTask; if (taskType.equals(SCRIPT_TASK)) { internalTask = new InternalScriptTask(); } else if (taskType.equals(FORKED_SCRIPT_TASK)) { internalTask = new InternalForkedScriptTask(); } else { throw new IllegalStateException("Unexpected stored task type: " + taskType); } internalTask.setId(taskId); internalTask.setDescription(getDescription()); internalTask.setStatus(getTaskStatus()); internalTask.setJobInfo(internalJob.getJobInfo()); internalTask.setName(getTaskName()); internalTask.setExecutionDuration(getExecutionDuration()); internalTask.setFinishedTime(getFinishedTime()); internalTask.setStartTime(getStartTime()); internalTask.setExecutionHostName(getExecutionHostName()); internalTask.setCancelJobOnError(isCancelJobOnError()); internalTask.setPreciousLogs(isPreciousLogs()); internalTask.setPreciousResult(isPreciousResult()); internalTask.setRunAsMe(isRunAsMe()); internalTask.setWallTime(getWallTime()); internalTask.setMaxNumberOfExecution(getMaxNumberOfExecution()); internalTask.setRestartTaskOnError(getRestartMode()); internalTask.setFlowBlock(getFlowBlock()); internalTask.setIterationIndex(getIteration()); internalTask.setReplicationIndex(getReplication()); internalTask.setMatchingBlock(getMatchingBlock()); ForkEnvironment forkEnv = new ForkEnvironment(); forkEnv.setJavaHome(javaHome); List<String> additionalClasspath = getAdditionalClasspath(); if (additionalClasspath != null) { for (String classpath : additionalClasspath) { forkEnv.addAdditionalClasspath(classpath); } } List<String> jvmArguments = getJvmArguments(); if (jvmArguments != null) { for (String jvmArg : jvmArguments) { forkEnv.addJVMArgument(jvmArg); } } List<EnvironmentModifierData> envModifiers = getEnvModifiers(); if (envModifiers != null) { for (EnvironmentModifierData envModifier : envModifiers) { forkEnv.addSystemEnvironmentVariable(envModifier.getName(), envModifier.getValue()); } } if (envScript != null) { forkEnv.setEnvScript(envScript.createSimpleScript()); } internalTask.setForkEnvironment(forkEnv); return internalTask; }
/** * Create an internalTaskFlow job with the given task flow job (user) * * @param userJob the user job that will be used to create the internal job. * @return the created internal job. * @throws JobCreationException an exception if the factory cannot create the given job. */ private static InternalJob createJob(TaskFlowJob userJob) throws JobCreationException { if (userJob.getTasks().size() == 0) { logger.info("Job '" + userJob.getName() + "' must contain tasks !"); throw new JobCreationException("This job must contains tasks !"); } // validate taskflow List<FlowChecker.Block> blocks = new ArrayList<>(); FlowError err = FlowChecker.validate(userJob, blocks); if (err != null) { String e = ""; e += "Invalid taskflow: " + err.getMessage() + "; context: " + err.getTask(); logger.error(e); throw new JobCreationException(e, err); } InternalJob job = new InternalTaskFlowJob(); // keep an initial job content job.setTaskFlowJob(userJob); Map<Task, InternalTask> tasksList = new LinkedHashMap<>(); boolean hasPreciousResult = false; for (Task t : userJob.getTasks()) { tasksList.put(t, createTask(userJob, t)); if (!hasPreciousResult) { hasPreciousResult = t.isPreciousResult(); } } for (Entry<Task, InternalTask> entry : tasksList.entrySet()) { if (entry.getKey().getDependencesList() != null) { for (Task t : entry.getKey().getDependencesList()) { entry.getValue().addDependence(tasksList.get(t)); } } job.addTask(entry.getValue()); } // tag matching blocks in InternalTasks for (InternalTask it : tasksList.values()) { for (FlowChecker.Block block : blocks) { if (it.getName().equals(block.start.element.getName())) { it.setMatchingBlock(block.end.element.getName()); } if (it.getName().equals(block.end.element.getName())) { it.setMatchingBlock(block.start.element.getName()); } } } // create if/else/join weak dependencies for (InternalTask it : tasksList.values()) { // it performs an IF action if (it.getFlowScript() != null && it.getFlowScript().getActionType().equals(FlowActionType.IF.toString())) { String ifBranch = it.getFlowScript().getActionTarget(); String elseBranch = it.getFlowScript().getActionTargetElse(); String join = it.getFlowScript().getActionContinuation(); List<InternalTask> joinedBranches = new ArrayList<>(); // find the ifBranch task for (InternalTask it2 : tasksList.values()) { if (it2.getName().equals(ifBranch)) { it2.setIfBranch(it); String match = it2.getMatchingBlock(); // find its matching block task if (match == null) { // no match: single task joinedBranches.add(it2); } else { for (InternalTask it3 : tasksList.values()) { if (it3.getName().equals(match)) { joinedBranches.add(it3); break; } } } break; } } // find the elseBranch task for (InternalTask it2 : tasksList.values()) { if (it2.getName().equals(elseBranch)) { it2.setIfBranch(it); String match = it2.getMatchingBlock(); // find its matching block task if (match == null) { // no match: single task joinedBranches.add(it2); } else { for (InternalTask it3 : tasksList.values()) { if (it3.getName().equals(match)) { joinedBranches.add(it3); break; } } } break; } } // find the joinBranch task for (InternalTask it2 : tasksList.values()) { if (it2.getName().equals(join)) { it2.setJoinedBranches(joinedBranches); } } } } return job; }