Search in sources :

Example 36 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class EmailTemplateServiceImpl method applyTemplate.

/**
 * Apply the template to the workflow instance.
 *
 * @param templateName
 *          template name
 * @param templateContent
 *          template content
 * @param workflowInstance
 *          workflow
 * @return text with applied template
 */
@Override
public String applyTemplate(String templateName, String templateContent, WorkflowInstance workflowInstance) {
    if (templateContent == null && templateScannerRef.get() != null) {
        templateContent = templateScannerRef.get().getTemplate(templateName);
    }
    if (templateContent == null) {
        logger.warn("E-mail template not found: {}", templateName);
        // it's probably missing
        return "TEMPLATE NOT FOUND: " + templateName;
    }
    // Build email data structure and apply the template
    HashMap<String, HashMap<String, String>> catalogs = initCatalogs(workflowInstance.getMediaPackage());
    WorkflowOperationInstance failed = findFailedOperation(workflowInstance);
    List<Incident> incidentList = null;
    if (failed != null) {
        try {
            IncidentTree incidents = incidentService.getIncidentsOfJob(failed.getId(), true);
            incidentList = generateIncidentList(incidents);
        } catch (Exception e) {
            logger.error("Error when populating template with incidents", e);
        // Incidents in email will be empty
        }
    }
    return DocUtil.generate(new EmailData(templateName, workflowInstance, catalogs, failed, incidentList), templateContent);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) HashMap(java.util.HashMap) Incident(org.opencastproject.job.api.Incident) IncidentTree(org.opencastproject.job.api.IncidentTree)

Example 37 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class WorkflowServiceImpl method runWorkflow.

/**
 * Executes the workflow.
 *
 * @param workflow
 *          the workflow instance
 * @throws WorkflowException
 *           if there is a problem processing the workflow
 */
protected Job runWorkflow(WorkflowInstance workflow) throws WorkflowException, UnauthorizedException {
    if (!INSTANTIATED.equals(workflow.getState())) {
        // updated accordingly.
        if (RUNNING.equals(workflow.getState())) {
            WorkflowOperationInstance currentOperation = workflow.getCurrentOperation();
            if (currentOperation != null) {
                if (currentOperation.getId() != null) {
                    try {
                        Job operationJob = serviceRegistry.getJob(currentOperation.getId());
                        if (Job.Status.RUNNING.equals(operationJob.getStatus())) {
                            logger.debug("Not starting workflow %s, it is already in running state", workflow);
                            return null;
                        } else {
                            logger.info("Scheduling next operation of workflow %s", workflow);
                            operationJob.setStatus(Status.QUEUED);
                            operationJob.setDispatchable(true);
                            return serviceRegistry.updateJob(operationJob);
                        }
                    } catch (Exception e) {
                        logger.warn("Error determining status of current workflow operation in {}: {}", workflow, e.getMessage());
                        return null;
                    }
                }
            } else {
                throw new IllegalStateException("Cannot start a workflow '" + workflow + "' with no current operation");
            }
        } else {
            throw new IllegalStateException("Cannot start a workflow in state '" + workflow.getState() + "'");
        }
    }
    // If this is a new workflow, move to the first operation
    workflow.setState(RUNNING);
    update(workflow);
    WorkflowOperationInstance operation = workflow.getCurrentOperation();
    if (operation == null)
        throw new IllegalStateException("Cannot start a workflow without a current operation");
    if (operation.getPosition() != 0)
        throw new IllegalStateException("Current operation expected to be first");
    try {
        logger.info("Scheduling workflow %s for execution", workflow.getId());
        Job job = serviceRegistry.createJob(JOB_TYPE, Operation.START_OPERATION.toString(), Arrays.asList(Long.toString(workflow.getId())), null, false, null, WORKFLOW_JOB_LOAD);
        operation.setId(job.getId());
        update(workflow);
        job.setStatus(Status.QUEUED);
        job.setDispatchable(true);
        return serviceRegistry.updateJob(job);
    } catch (ServiceRegistryException e) {
        throw new WorkflowDatabaseException(e);
    } catch (NotFoundException e) {
        // this should be impossible
        throw new IllegalStateException("Unable to find a job that was just created");
    }
}
Also used : WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) NotFoundException(org.opencastproject.util.NotFoundException) Job(org.opencastproject.job.api.Job) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) UndispatchableJobException(org.opencastproject.serviceregistry.api.UndispatchableJobException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SeriesException(org.opencastproject.series.api.SeriesException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowStateException(org.opencastproject.workflow.api.WorkflowStateException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 38 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class WorkflowServiceImpl method process.

/**
 * Processes the workflow job.
 *
 * @param job
 *          the job
 * @return the job payload
 * @throws Exception
 *           if job processing fails
 */
protected String process(Job job) throws Exception {
    List<String> arguments = job.getArguments();
    Operation op = null;
    WorkflowInstance workflowInstance = null;
    WorkflowOperationInstance wfo = null;
    String operation = job.getOperation();
    try {
        try {
            op = Operation.valueOf(operation);
            switch(op) {
                case START_WORKFLOW:
                    workflowInstance = WorkflowParser.parseWorkflowInstance(job.getPayload());
                    logger.debug("Starting new workflow %s", workflowInstance);
                    runWorkflow(workflowInstance);
                    break;
                case RESUME:
                    workflowInstance = getWorkflowById(Long.parseLong(arguments.get(0)));
                    wfo = workflowInstance.getCurrentOperation();
                    Map<String, String> properties = null;
                    if (arguments.size() > 1) {
                        Properties props = new Properties();
                        props.load(IOUtils.toInputStream(arguments.get(arguments.size() - 1)));
                        properties = new HashMap<String, String>();
                        for (Entry<Object, Object> entry : props.entrySet()) {
                            properties.put(entry.getKey().toString(), entry.getValue().toString());
                        }
                    }
                    logger.debug("Resuming %s at %s", workflowInstance, workflowInstance.getCurrentOperation());
                    workflowInstance.setState(RUNNING);
                    update(workflowInstance);
                    wfo = runWorkflowOperation(workflowInstance, properties);
                    break;
                case START_OPERATION:
                    workflowInstance = getWorkflowById(Long.parseLong(arguments.get(0)));
                    wfo = workflowInstance.getCurrentOperation();
                    if (OperationState.RUNNING.equals(wfo.getState()) || OperationState.PAUSED.equals(wfo.getState())) {
                        logger.info("Reset operation state %s %s to INSTANTIATED due to job restart", workflowInstance, wfo);
                        wfo.setState(OperationState.INSTANTIATED);
                    }
                    wfo.setExecutionHost(job.getProcessingHost());
                    logger.debug("Running %s %s", workflowInstance, wfo);
                    wfo = runWorkflowOperation(workflowInstance, null);
                    updateOperationJob(job.getId(), wfo.getState());
                    break;
                default:
                    throw new IllegalStateException("Don't know how to handle operation '" + operation + "'");
            }
        } catch (IllegalArgumentException e) {
            throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'", e);
        } catch (IndexOutOfBoundsException e) {
            throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e);
        } catch (NotFoundException e) {
            logger.warn(e.getMessage());
            updateOperationJob(job.getId(), OperationState.FAILED);
        }
        return null;
    } catch (Exception e) {
        logger.warn(e, "Exception while accepting job " + job);
        try {
            if (workflowInstance != null) {
                logger.warn("Marking job {} and workflow instance {} as failed", job, workflowInstance);
                updateOperationJob(job.getId(), OperationState.FAILED);
                workflowInstance.setState(FAILED);
                update(workflowInstance);
            } else {
                logger.warn(e, "Unable to parse workflow instance");
            }
        } catch (WorkflowDatabaseException e1) {
            throw new ServiceRegistryException(e1);
        }
        if (e instanceof ServiceRegistryException)
            throw e;
        throw new ServiceRegistryException("Error handling operation '" + op + "'", e);
    }
}
Also used : NotFoundException(org.opencastproject.util.NotFoundException) Collections.mkString(org.opencastproject.util.data.Collections.mkString) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Properties(java.util.Properties) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) UndispatchableJobException(org.opencastproject.serviceregistry.api.UndispatchableJobException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SeriesException(org.opencastproject.series.api.SeriesException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowStateException(org.opencastproject.workflow.api.WorkflowStateException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) IndexRecreateObject(org.opencastproject.message.broker.api.index.IndexRecreateObject)

Example 39 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class WorkflowServiceImplTest method testRetryStrategyHold.

@Test
public void testRetryStrategyHold() throws Exception {
    WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
    def.setId("workflow-definition-1");
    def.setTitle("workflow-definition-1");
    def.setDescription("workflow-definition-1");
    def.setPublished(true);
    service.registerWorkflowDefinition(def);
    WorkflowOperationDefinitionImpl opDef = new WorkflowOperationDefinitionImpl("failOneTime", "fails once", null, true);
    opDef.setRetryStrategy(RetryStrategy.HOLD);
    opDef.setMaxAttempts(2);
    def.add(opDef);
    MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    WorkflowInstance workflow = startAndWait(def, mp, WorkflowState.PAUSED);
    WorkflowOperationInstance errorResolutionOperation = service.getWorkflowById(workflow.getId()).getOperations().get(0);
    WorkflowOperationInstance failOneTimeOperation = service.getWorkflowById(workflow.getId()).getOperations().get(1);
    Assert.assertTrue(errorResolutionOperation.getTemplate().equals(WorkflowServiceImpl.ERROR_RESOLUTION_HANDLER_ID));
    Assert.assertTrue(errorResolutionOperation.getState() == OperationState.PAUSED);
    Assert.assertTrue(errorResolutionOperation.getFailedAttempts() == 0);
    Assert.assertTrue(failOneTimeOperation.getState() == OperationState.RETRY);
    Assert.assertTrue(failOneTimeOperation.getMaxAttempts() == 2);
    Assert.assertTrue(failOneTimeOperation.getFailedAttempts() == 1);
}
Also used : WorkflowOperationDefinitionImpl(org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Example 40 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class WorkflowServiceImplTest method testRemove.

/**
 * Test for {@link WorkflowServiceImpl#remove(long)}
 *
 * @throws Exception
 *           if anything fails
 */
@Test
public void testRemove() throws Exception {
    WorkflowInstance wi1 = startAndWait(workingDefinition, mediapackage1, WorkflowState.SUCCEEDED);
    WorkflowInstance wi2 = startAndWait(workingDefinition, mediapackage2, WorkflowState.SUCCEEDED);
    // reload instances, because operations have no id before
    wi1 = service.getWorkflowById(wi1.getId());
    wi2 = service.getWorkflowById(wi2.getId());
    service.remove(wi1.getId());
    assertEquals(1, service.getWorkflowInstances(new WorkflowQuery()).size());
    for (WorkflowOperationInstance op : wi1.getOperations()) {
        assertEquals(0, serviceRegistry.getChildJobs(op.getId()).size());
    }
    service.remove(wi2.getId());
    assertEquals(0, service.getWorkflowInstances(new WorkflowQuery()).size());
}
Also used : WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Aggregations

WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)104 ArrayList (java.util.ArrayList)51 MediaPackage (org.opencastproject.mediapackage.MediaPackage)48 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)37 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)33 Test (org.junit.Test)32 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)31 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)28 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)24 Job (org.opencastproject.job.api.Job)23 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)19 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)19 URI (java.net.URI)18 NotFoundException (org.opencastproject.util.NotFoundException)16 Track (org.opencastproject.mediapackage.Track)14 IOException (java.io.IOException)13 File (java.io.File)12 HashMap (java.util.HashMap)12 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)11 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)10