Search in sources :

Example 1 with WorkflowException

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

the class IndexServiceImpl method updateEventMetadata.

@Override
public MetadataList updateEventMetadata(String id, MetadataList metadataList, AbstractSearchIndex index) throws IndexServiceException, SearchIndexException, NotFoundException, UnauthorizedException {
    Opt<Event> optEvent = getEvent(id, index);
    if (optEvent.isNone())
        throw new NotFoundException("Cannot find an event with id " + id);
    Event event = optEvent.get();
    MediaPackage mediaPackage = getEventMediapackage(event);
    Opt<Set<String>> presenters = Opt.none();
    Opt<MetadataCollection> eventCatalog = metadataList.getMetadataByAdapter(getCommonEventCatalogUIAdapter());
    if (eventCatalog.isSome()) {
        presenters = updatePresenters(eventCatalog.get());
    }
    updateMediaPackageMetadata(mediaPackage, metadataList);
    switch(getEventSource(event)) {
        case WORKFLOW:
            Opt<WorkflowInstance> workflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
            if (workflowInstance.isNone()) {
                logger.error("No workflow instance for event {} found!", event.getIdentifier());
                throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
            }
            try {
                WorkflowInstance instance = workflowInstance.get();
                instance.setMediaPackage(mediaPackage);
                updateWorkflowInstance(instance);
            } catch (WorkflowException e) {
                logger.error("Unable to update workflow event {} with metadata {} because {}", id, RestUtils.getJsonStringSilent(metadataList.toJSON()), getStackTrace(e));
                throw new IndexServiceException("Unable to update workflow event " + id);
            }
            break;
        case ARCHIVE:
            assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
            break;
        case SCHEDULE:
            try {
                schedulerService.updateEvent(id, Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), presenters, Opt.some(mediaPackage), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
            } catch (SchedulerException e) {
                logger.error("Unable to update scheduled event {} with metadata {} because {}", id, RestUtils.getJsonStringSilent(metadataList.toJSON()), getStackTrace(e));
                throw new IndexServiceException("Unable to update scheduled event " + id);
            }
            break;
        default:
            logger.error("Unkown event source!");
    }
    return metadataList;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Event(org.opencastproject.index.service.impl.index.event.Event) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException)

Example 2 with WorkflowException

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

the class IndexServiceImpl method removeCatalogByFlavor.

@Override
public void removeCatalogByFlavor(Event event, MediaPackageElementFlavor flavor) throws IndexServiceException, NotFoundException, UnauthorizedException {
    MediaPackage mediaPackage = getEventMediapackage(event);
    Catalog[] catalogs = mediaPackage.getCatalogs(flavor);
    if (catalogs.length == 0) {
        throw new NotFoundException(String.format("Cannot find a catalog with flavor '%s' for event with id '%s'.", flavor.toString(), event.getIdentifier()));
    }
    for (Catalog catalog : catalogs) {
        mediaPackage.remove(catalog);
    }
    switch(getEventSource(event)) {
        case WORKFLOW:
            Opt<WorkflowInstance> workflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
            if (workflowInstance.isNone()) {
                logger.error("No workflow instance for event {} found!", event.getIdentifier());
                throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
            }
            try {
                WorkflowInstance instance = workflowInstance.get();
                instance.setMediaPackage(mediaPackage);
                updateWorkflowInstance(instance);
            } catch (WorkflowException e) {
                logger.error("Unable to remove catalog with flavor {} by updating workflow event {} because {}", flavor, event.getIdentifier(), getStackTrace(e));
                throw new IndexServiceException("Unable to update workflow event " + event.getIdentifier());
            }
            break;
        case ARCHIVE:
            assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
            break;
        case SCHEDULE:
            try {
                schedulerService.updateEvent(event.getIdentifier(), Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), Opt.<Set<String>>none(), Opt.some(mediaPackage), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
            } catch (SchedulerException e) {
                logger.error("Unable to remove catalog with flavor {} by updating scheduled event {} because {}", flavor, event.getIdentifier(), getStackTrace(e));
                throw new IndexServiceException("Unable to update scheduled event " + event.getIdentifier());
            }
            break;
        default:
            throw new IndexServiceException(String.format("Unable to handle event source type '%s'", getEventSource(event)));
    }
}
Also used : SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Catalog(org.opencastproject.mediapackage.Catalog) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException)

Example 3 with WorkflowException

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

the class WorkflowServiceImpl method update.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#update(org.opencastproject.workflow.api.WorkflowInstance)
 */
@Override
public void update(final WorkflowInstance workflowInstance) throws WorkflowException, UnauthorizedException {
    final Lock lock = updateLock.get(workflowInstance.getId());
    lock.lock();
    try {
        WorkflowInstance originalWorkflowInstance = null;
        try {
            originalWorkflowInstance = getWorkflowById(workflowInstance.getId());
        } catch (NotFoundException e) {
        // That's fine, it's a new workflow instance
        }
        if (originalWorkflowInstance != null) {
            try {
                assertPermission(originalWorkflowInstance, Permissions.Action.WRITE.toString());
            } catch (MediaPackageException e) {
                throw new WorkflowParsingException(e);
            }
        }
        MediaPackage updatedMediaPackage = null;
        try {
            // Before we persist this, extract the metadata
            updatedMediaPackage = workflowInstance.getMediaPackage();
            populateMediaPackageMetadata(updatedMediaPackage);
            String seriesId = updatedMediaPackage.getSeries();
            if (seriesId != null && workflowInstance.getCurrentOperation() != null) {
                // If the mediapackage contains a series, find the series ACLs and add the security information to the
                // mediapackage
                AccessControlList acl = seriesService.getSeriesAccessControl(seriesId);
                Option<AccessControlList> activeSeriesAcl = authorizationService.getAcl(updatedMediaPackage, AclScope.Series);
                if (activeSeriesAcl.isNone() || !AccessControlUtil.equals(activeSeriesAcl.get(), acl))
                    authorizationService.setAcl(updatedMediaPackage, AclScope.Series, acl);
            }
        } catch (SeriesException e) {
            throw new WorkflowDatabaseException(e);
        } catch (NotFoundException e) {
            logger.warn("Metadata for mediapackage {} could not be updated because it wasn't found", updatedMediaPackage, e);
        } catch (Exception e) {
            logger.error("Metadata for mediapackage {} could not be updated", updatedMediaPackage, e);
        }
        // Synchronize the job status with the workflow
        WorkflowState workflowState = workflowInstance.getState();
        String xml;
        try {
            xml = WorkflowParser.toXml(workflowInstance);
        } catch (Exception e) {
            // Can't happen, since we are converting from an in-memory object
            throw new IllegalStateException("In-memory workflow instance could not be serialized", e);
        }
        Job job = null;
        try {
            job = serviceRegistry.getJob(workflowInstance.getId());
            job.setPayload(xml);
            // Synchronize workflow and job state
            switch(workflowState) {
                case FAILED:
                    job.setStatus(Status.FAILED);
                    break;
                case FAILING:
                    break;
                case INSTANTIATED:
                    job.setDispatchable(true);
                    job.setStatus(Status.QUEUED);
                    break;
                case PAUSED:
                    job.setStatus(Status.PAUSED);
                    break;
                case RUNNING:
                    job.setStatus(Status.RUNNING);
                    break;
                case STOPPED:
                    job.setStatus(Status.CANCELED);
                    break;
                case SUCCEEDED:
                    job.setStatus(Status.FINISHED);
                    break;
                default:
                    throw new IllegalStateException("Found a workflow state that is not handled");
            }
        } catch (ServiceRegistryException e) {
            logger.error(e, "Unable to read workflow job %s from service registry", workflowInstance.getId());
            throw new WorkflowDatabaseException(e);
        } catch (NotFoundException e) {
            logger.error("Job for workflow %s not found in service registry", workflowInstance.getId());
            throw new WorkflowDatabaseException(e);
        }
        // Update both workflow and workflow job
        try {
            job = serviceRegistry.updateJob(job);
            messageSender.sendObjectMessage(WorkflowItem.WORKFLOW_QUEUE, MessageSender.DestinationType.Queue, WorkflowItem.updateInstance(workflowInstance));
            index(workflowInstance);
        } catch (ServiceRegistryException e) {
            logger.error("Update of workflow job %s in the service registry failed, service registry and workflow index may be out of sync", workflowInstance.getId());
            throw new WorkflowDatabaseException(e);
        } catch (NotFoundException e) {
            logger.error("Job for workflow %s not found in service registry", workflowInstance.getId());
            throw new WorkflowDatabaseException(e);
        } catch (Exception e) {
            logger.error("Update of workflow job %s in the service registry failed, service registry and workflow index may be out of sync", job.getId());
            throw new WorkflowException(e);
        }
        if (workflowStatsCollect) {
            workflowsStatistics.updateWorkflow(getBeanStatistics(), getHoldWorkflows());
        }
        try {
            WorkflowInstance clone = WorkflowParser.parseWorkflowInstance(WorkflowParser.toXml(workflowInstance));
            fireListeners(originalWorkflowInstance, clone);
        } catch (Exception e) {
            // Can't happen, since we are converting from an in-memory object
            throw new IllegalStateException("In-memory workflow instance could not be serialized", e);
        }
    } finally {
        lock.unlock();
    }
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) NotFoundException(org.opencastproject.util.NotFoundException) Collections.mkString(org.opencastproject.util.data.Collections.mkString) SeriesException(org.opencastproject.series.api.SeriesException) 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) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) Lock(java.util.concurrent.locks.Lock) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowState(org.opencastproject.workflow.api.WorkflowInstance.WorkflowState) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Job(org.opencastproject.job.api.Job) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Example 4 with WorkflowException

use of org.opencastproject.workflow.api.WorkflowException 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 5 with WorkflowException

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

the class WorkflowServiceImpl method stop.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#stop(long)
 */
@Override
public WorkflowInstance stop(long workflowInstanceId) throws WorkflowException, NotFoundException, UnauthorizedException {
    final Lock lock = this.lock.get(workflowInstanceId);
    lock.lock();
    try {
        WorkflowInstanceImpl instance = getWorkflowById(workflowInstanceId);
        if (instance.getState() != STOPPED) {
            // Update the workflow instance
            instance.setState(STOPPED);
            update(instance);
        }
        try {
            removeTempFiles(instance);
        } catch (Exception e) {
            logger.warn("Cannot remove temp files for workflow instance {}: {}", workflowInstanceId, e.getMessage());
        }
        return instance;
    } finally {
        lock.unlock();
    }
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) 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)

Aggregations

WorkflowException (org.opencastproject.workflow.api.WorkflowException)15 NotFoundException (org.opencastproject.util.NotFoundException)10 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)9 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)8 IOException (java.io.IOException)7 MediaPackage (org.opencastproject.mediapackage.MediaPackage)6 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)6 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)5 WorkflowParsingException (org.opencastproject.workflow.api.WorkflowParsingException)5 Lock (java.util.concurrent.locks.Lock)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)3 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)3 SeriesException (org.opencastproject.series.api.SeriesException)3 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)3 UndispatchableJobException (org.opencastproject.serviceregistry.api.UndispatchableJobException)3 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)3 WorkflowSet (org.opencastproject.workflow.api.WorkflowSet)3 WorkflowStateException (org.opencastproject.workflow.api.WorkflowStateException)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3