Search in sources :

Example 81 with WorkflowInstance

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

the class WorkflowServiceImpl method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#start(org.opencastproject.workflow.api.WorkflowDefinition,
 *      org.opencastproject.mediapackage.MediaPackage, Long, java.util.Map)
 */
@Override
public WorkflowInstance start(WorkflowDefinition workflowDefinition, MediaPackage sourceMediaPackage, Long parentWorkflowId, Map<String, String> properties) throws WorkflowDatabaseException, WorkflowParsingException, NotFoundException {
    // We have to synchronize per media package to avoid starting multiple simultaneous workflows for one media package.
    final Lock lock = mediaPackageLocks.get(sourceMediaPackage.getIdentifier().toString());
    lock.lock();
    try {
        logger.startUnitOfWork();
        if (workflowDefinition == null)
            throw new IllegalArgumentException("workflow definition must not be null");
        if (sourceMediaPackage == null)
            throw new IllegalArgumentException("mediapackage must not be null");
        for (List<String> errors : MediaPackageSupport.sanityCheck(sourceMediaPackage)) {
            throw new IllegalArgumentException("Insane media package cannot be processed: " + mkString(errors, "; "));
        }
        if (parentWorkflowId != null) {
            try {
                // Let NotFoundException bubble up
                getWorkflowById(parentWorkflowId);
            } catch (UnauthorizedException e) {
                throw new IllegalArgumentException("Parent workflow " + parentWorkflowId + " not visible to this user");
            }
        } else {
            WorkflowQuery wfq = new WorkflowQuery().withMediaPackage(sourceMediaPackage.getIdentifier().compact());
            WorkflowSet mpWorkflowInstances = getWorkflowInstances(wfq);
            if (mpWorkflowInstances.size() > 0) {
                for (WorkflowInstance wfInstance : mpWorkflowInstances.getItems()) {
                    if (wfInstance.isActive())
                        throw new IllegalStateException(String.format("Can't start workflow '%s' for media package '%s' because another workflow is currently active.", workflowDefinition.getTitle(), sourceMediaPackage.getIdentifier().compact()));
                }
            }
        }
        // Get the current user
        User currentUser = securityService.getUser();
        if (currentUser == null)
            throw new SecurityException("Current user is unknown");
        // Get the current organization
        Organization organization = securityService.getOrganization();
        if (organization == null)
            throw new SecurityException("Current organization is unknown");
        WorkflowInstance workflowInstance = new WorkflowInstanceImpl(workflowDefinition, sourceMediaPackage, parentWorkflowId, currentUser, organization, properties);
        workflowInstance = updateConfiguration(workflowInstance, properties);
        // Create and configure the workflow instance
        try {
            // Create a new job for this workflow instance
            String workflowDefinitionXml = WorkflowParser.toXml(workflowDefinition);
            String workflowInstanceXml = WorkflowParser.toXml(workflowInstance);
            String mediaPackageXml = MediaPackageParser.getAsXml(sourceMediaPackage);
            List<String> arguments = new ArrayList<String>();
            arguments.add(workflowDefinitionXml);
            arguments.add(mediaPackageXml);
            if (parentWorkflowId != null || properties != null) {
                String parentWorkflowIdString = (parentWorkflowId != null) ? parentWorkflowId.toString() : NULL_PARENT_ID;
                arguments.add(parentWorkflowIdString);
            }
            if (properties != null) {
                arguments.add(mapToString(properties));
            }
            Job job = serviceRegistry.createJob(JOB_TYPE, Operation.START_WORKFLOW.toString(), arguments, workflowInstanceXml, false, null, WORKFLOW_JOB_LOAD);
            // Have the workflow take on the job's identity
            workflowInstance.setId(job.getId());
            // Add the workflow to the search index and have the job enqueued for dispatch.
            // Update also sets ACL and mediapackage metadata
            update(workflowInstance);
            return workflowInstance;
        } catch (Throwable t) {
            try {
                workflowInstance.setState(FAILED);
                update(workflowInstance);
            } catch (Exception failureToFail) {
                logger.warn(failureToFail, "Unable to update workflow to failed state");
            }
            throw new WorkflowDatabaseException(t);
        }
    } finally {
        logger.endUnitOfWork();
        lock.unlock();
    }
}
Also used : WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Collections.mkString(org.opencastproject.util.data.Collections.mkString) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) 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) Lock(java.util.concurrent.locks.Lock) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) Job(org.opencastproject.job.api.Job)

Example 82 with WorkflowInstance

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

the class WorkflowServiceImpl method repopulate.

@Override
public void repopulate(final String indexName) throws Exception {
    List<String> workflows = serviceRegistry.getJobPayloads(Operation.START_WORKFLOW.toString());
    final String destinationId = WorkflowItem.WORKFLOW_QUEUE_PREFIX + indexName.substring(0, 1).toUpperCase() + indexName.substring(1);
    if (workflows.size() > 0) {
        final int total = workflows.size();
        logger.info("Populating index '{}' with {} workflows", indexName, total);
        final int responseInterval = (total < 100) ? 1 : (total / 100);
        int current = 0;
        for (final String workflow : workflows) {
            current += 1;
            if (StringUtils.isEmpty(workflow)) {
                logger.warn("Skipping restoring of workflow no {}: Payload is empty", current);
                continue;
            }
            WorkflowInstance instance;
            try {
                instance = WorkflowParser.parseWorkflowInstance(workflow);
            } catch (WorkflowParsingException e) {
                logger.warn("Skipping restoring of workflow. Error parsing: {}", workflow, e);
                continue;
            }
            Organization organization = instance.getOrganization();
            SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(componentContext, organization), new Effect0() {

                @Override
                public void run() {
                    // Send message to update index item
                    messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, WorkflowItem.updateInstance(instance));
                }
            });
            if ((current % responseInterval == 0) || (current == total)) {
                logger.info("Updating {} workflow index {}/{}: {} percent complete.", indexName, current, total, current * 100 / total);
            }
        }
    }
    logger.info("Finished populating {} index with workflows", indexName);
    Organization organization = new DefaultOrganization();
    SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(componentContext, organization), new Effect0() {

        @Override
        protected void run() {
            messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.end(indexName, IndexRecreateObject.Service.Workflow));
        }
    });
}
Also used : Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Effect0(org.opencastproject.util.data.Effect0) Collections.mkString(org.opencastproject.util.data.Collections.mkString) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization)

Example 83 with WorkflowInstance

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

the class WorkflowServiceImpl method isReadyToAccept.

/**
 * {@inheritDoc}
 *
 * If we are already running the maximum number of workflows, don't accept another START_WORKFLOW job
 *
 * @see org.opencastproject.job.api.AbstractJobProducer#isReadyToAccept(org.opencastproject.job.api.Job)
 */
@Override
public boolean isReadyToAccept(Job job) throws ServiceRegistryException, UndispatchableJobException {
    String operation = job.getOperation();
    // Only restrict execution of new jobs
    if (!Operation.START_WORKFLOW.toString().equals(operation))
        return true;
    // If the first operation is guaranteed to pause, run the job.
    if (job.getArguments().size() > 1 && job.getArguments().get(0) != null) {
        try {
            WorkflowDefinition workflowDef = WorkflowParser.parseWorkflowDefinition(job.getArguments().get(0));
            if (workflowDef.getOperations().size() > 0) {
                String firstOperationId = workflowDef.getOperations().get(0).getId();
                WorkflowOperationHandler handler = getWorkflowOperationHandler(firstOperationId);
                if (handler instanceof ResumableWorkflowOperationHandler) {
                    if (((ResumableWorkflowOperationHandler) handler).isAlwaysPause()) {
                        return true;
                    }
                }
            }
        } catch (WorkflowParsingException e) {
            throw new UndispatchableJobException(job + " is not a proper job to start a workflow", e);
        }
    }
    WorkflowInstance workflow = null;
    WorkflowSet workflowInstances = null;
    String mediaPackageId = null;
    // Fetch all workflows that are running with the current mediapackage
    try {
        workflow = getWorkflowById(job.getId());
        mediaPackageId = workflow.getMediaPackage().getIdentifier().toString();
        workflowInstances = getWorkflowInstances(new WorkflowQuery().withMediaPackage(workflow.getMediaPackage().getIdentifier().toString()).withState(RUNNING).withState(PAUSED).withState(FAILING));
    } catch (NotFoundException e) {
        logger.error("Trying to start workflow with id %s but no corresponding instance is available from the workflow service", job.getId());
        throw new UndispatchableJobException(e);
    } catch (UnauthorizedException e) {
        logger.error("Authorization denied while requesting to loading workflow instance %s: %s", job.getId(), e.getMessage());
        throw new UndispatchableJobException(e);
    } catch (WorkflowDatabaseException e) {
        logger.error("Error loading workflow instance %s: %s", job.getId(), e.getMessage());
        return false;
    }
    // If more than one workflow is running working on this mediapackage, then we don't start this one
    boolean toomany = workflowInstances.size() > 1;
    // Make sure we are not excluding ourselves
    toomany |= workflowInstances.size() == 1 && workflow.getId() != workflowInstances.getItems()[0].getId();
    // Avoid running multiple workflows with same media package id at the same time
    if (toomany) {
        if (!delayedWorkflows.contains(workflow.getId())) {
            logger.info("Delaying start of workflow %s, another workflow on media package %s is still running", workflow.getId(), mediaPackageId);
            delayedWorkflows.add(workflow.getId());
        }
        return false;
    }
    return true;
}
Also used : WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) UndispatchableJobException(org.opencastproject.serviceregistry.api.UndispatchableJobException) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) NotFoundException(org.opencastproject.util.NotFoundException) Collections.mkString(org.opencastproject.util.data.Collections.mkString) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) WorkflowOperationHandler(org.opencastproject.workflow.api.WorkflowOperationHandler) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Example 84 with WorkflowInstance

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

the class WorkflowServiceSolrIndex method createDocument.

/**
 * Adds the workflow instance to the search index.
 *
 * @param instance
 *          the instance
 * @return the solr input document
 * @throws Exception
 */
protected SolrInputDocument createDocument(WorkflowInstance instance) throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField(ID_KEY, instance.getId());
    doc.addField(WORKFLOW_DEFINITION_KEY, instance.getTemplate());
    doc.addField(STATE_KEY, instance.getState().toString());
    String xml = WorkflowParser.toXml(instance);
    doc.addField(XML_KEY, xml);
    // index the current operation if there is one. If the workflow is finished, there is no current operation, so use a
    // constant
    WorkflowOperationInstance op = instance.getCurrentOperation();
    if (op == null) {
        doc.addField(OPERATION_KEY, NO_OPERATION_KEY);
    } else {
        doc.addField(OPERATION_KEY, op.getTemplate());
    }
    MediaPackage mp = instance.getMediaPackage();
    doc.addField(MEDIAPACKAGE_KEY, mp.getIdentifier().toString());
    if (mp.getSeries() != null) {
        doc.addField(SERIES_ID_KEY, mp.getSeries());
    }
    if (mp.getSeriesTitle() != null) {
        doc.addField(SERIES_TITLE_KEY, mp.getSeriesTitle());
    }
    if (mp.getDate() != null) {
        doc.addField(CREATED_KEY, mp.getDate());
    }
    if (mp.getTitle() != null) {
        doc.addField(TITLE_KEY, mp.getTitle());
    }
    if (mp.getLicense() != null) {
        doc.addField(LICENSE_KEY, mp.getLicense());
    }
    if (mp.getLanguage() != null) {
        doc.addField(LANGUAGE_KEY, mp.getLanguage());
    }
    if (mp.getContributors() != null && mp.getContributors().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String contributor : mp.getContributors()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(contributor);
        }
        doc.addField(CONTRIBUTOR_KEY, buf.toString());
    }
    if (mp.getCreators() != null && mp.getCreators().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String creator : mp.getCreators()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(creator);
        }
        doc.addField(CREATOR_KEY, buf.toString());
    }
    if (mp.getSubjects() != null && mp.getSubjects().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String subject : mp.getSubjects()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(subject);
        }
        doc.addField(SUBJECT_KEY, buf.toString());
    }
    User workflowCreator = instance.getCreator();
    doc.addField(WORKFLOW_CREATOR_KEY, workflowCreator.getUsername());
    doc.addField(ORG_KEY, instance.getOrganization().getId());
    WorkflowInstance.WorkflowState state = instance.getState();
    if (!(WorkflowInstance.WorkflowState.SUCCEEDED.equals(state) || WorkflowInstance.WorkflowState.FAILED.equals(state) || WorkflowInstance.WorkflowState.STOPPED.equals(state))) {
        AccessControlList acl;
        try {
            acl = authorizationService.getActiveAcl(mp).getA();
        } catch (Error e) {
            logger.error("No security xacml found on media package {}", mp);
            throw new WorkflowException(e);
        }
        addAuthorization(doc, acl);
    }
    return doc;
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) User(org.opencastproject.security.api.User) WorkflowState(org.opencastproject.workflow.api.WorkflowInstance.WorkflowState) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance)

Example 85 with WorkflowInstance

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

the class WorkflowServiceSolrIndex method activate.

/**
 * Activates the index by configuring solr with the server url that must have been set previously.
 */
public void activate(String systemUserName) {
    // Set up the solr server
    if (solrServerUrl != null) {
        solrServer = SolrServerFactory.newRemoteInstance(solrServerUrl);
    } else {
        try {
            setupSolr(new File(solrRoot));
        } catch (IOException e) {
            throw new IllegalStateException("Unable to connect to solr at " + solrRoot, e);
        } catch (SolrServerException e) {
            throw new IllegalStateException("Unable to connect to solr at " + solrRoot, e);
        }
    }
    // If the solr is empty, add all of the existing workflows
    long instancesInSolr = 0;
    try {
        instancesInSolr = count();
    } catch (WorkflowDatabaseException e) {
        throw new IllegalStateException(e);
    }
    if (instancesInSolr == 0) {
        logger.info("The workflow index is empty, looking for workflows to index");
        // this may be a new index, so get all of the existing workflows and index them
        List<String> workflowPayloads;
        try {
            workflowPayloads = serviceRegistry.getJobPayloads(WorkflowServiceImpl.Operation.START_WORKFLOW.toString());
        } catch (ServiceRegistryException e) {
            logger.error("Unable to load the workflows jobs: {}", e.getMessage());
            throw new ServiceException(e.getMessage());
        }
        final int total = workflowPayloads.size();
        if (total == 0) {
            logger.info("No workflows found. Repopulating index finished.");
            return;
        }
        logger.info("Populating the workflow index with {} workflows", total);
        int current = 0;
        for (String payload : workflowPayloads) {
            current++;
            WorkflowInstance instance = null;
            try {
                instance = WorkflowParser.parseWorkflowInstance(payload);
                Organization organization = instance.getOrganization();
                securityService.setOrganization(organization);
                securityService.setUser(SecurityUtil.createSystemUser(systemUserName, organization));
                index(instance);
            } catch (WorkflowParsingException | WorkflowDatabaseException e) {
                logger.warn("Skipping restoring of workflow {}", payload, e);
            }
            if (current % 100 == 0) {
                logger.info("Indexing workflow {}/{} ({} percent done)", current, total, current * 100 / total);
            }
        }
        logger.info("Finished populating the workflow search index");
    }
}
Also used : Organization(org.opencastproject.security.api.Organization) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) ServiceException(org.osgi.framework.ServiceException) File(java.io.File) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Aggregations

WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)94 Test (org.junit.Test)48 MediaPackage (org.opencastproject.mediapackage.MediaPackage)40 NotFoundException (org.opencastproject.util.NotFoundException)26 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)24 HashMap (java.util.HashMap)22 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)20 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)19 ArrayList (java.util.ArrayList)16 WorkflowQuery (org.opencastproject.workflow.api.WorkflowQuery)16 IOException (java.io.IOException)15 Organization (org.opencastproject.security.api.Organization)15 WorkflowSet (org.opencastproject.workflow.api.WorkflowSet)14 WorkflowException (org.opencastproject.workflow.api.WorkflowException)13 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)12 WorkflowDefinitionImpl (org.opencastproject.workflow.api.WorkflowDefinitionImpl)12 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)11 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)10 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)9 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)9