Search in sources :

Example 91 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class ServiceRegistryJpaImpl method getDispatchableJobsWithIdFilter.

/**
 * Return dispatchable job ids, where the job status is RESTART or QUEUED and the job id is listed in the given set.
 *
 * @param em the entity manager
 * @param jobIds set with job id's interested in
 * @return list with dispatchable job id's from the given set, with job status RESTART or QUEUED
 * @throws ServiceRegistryException if there is a problem communicating with the jobs database
 */
protected List<Long> getDispatchableJobsWithIdFilter(EntityManager em, Set<Long> jobIds) throws ServiceRegistryException {
    if (jobIds == null || jobIds.isEmpty())
        return Collections.EMPTY_LIST;
    Query query = null;
    try {
        query = em.createNamedQuery("Job.dispatchable.status.idfilter");
        query.setParameter("jobids", dispatchPriorityList.keySet());
        query.setParameter("statuses", Arrays.asList(Status.RESTART.ordinal(), Status.QUEUED.ordinal()));
        return query.getResultList();
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    }
}
Also used : Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 92 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class WorkflowServiceImpl method remove.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#remove(long)
 */
@Override
public void remove(long workflowInstanceId) throws WorkflowDatabaseException, NotFoundException, UnauthorizedException, WorkflowParsingException, WorkflowStateException {
    final Lock lock = this.lock.get(workflowInstanceId);
    lock.lock();
    try {
        WorkflowQuery query = new WorkflowQuery();
        query.withId(Long.toString(workflowInstanceId));
        WorkflowSet workflows = index.getWorkflowInstances(query, Permissions.Action.READ.toString(), false);
        if (workflows.size() == 1) {
            WorkflowInstance instance = workflows.getItems()[0];
            WorkflowInstance.WorkflowState state = instance.getState();
            if (state != WorkflowState.SUCCEEDED && state != WorkflowState.FAILED && state != WorkflowState.STOPPED)
                throw new WorkflowStateException("Workflow instance with state '" + state + "' cannot be removed. Only states SUCCEEDED, FAILED & STOPPED are allowed");
            try {
                assertPermission(instance, Permissions.Action.WRITE.toString());
            } catch (MediaPackageException e) {
                throw new WorkflowParsingException(e);
            }
            // First, remove temporary files DO THIS BEFORE REMOVING FROM INDEX
            try {
                removeTempFiles(instance);
            } catch (NotFoundException e) {
                // If the files aren't their anymore, we don't have to cleanup up them :-)
                logger.debug("Temporary files of workflow instance %d seem to be gone already...", workflowInstanceId);
            }
            // Second, remove jobs related to a operation which belongs to the workflow instance
            List<WorkflowOperationInstance> operations = instance.getOperations();
            List<Long> jobsToDelete = new ArrayList<>();
            for (WorkflowOperationInstance op : operations) {
                if (op.getId() != null) {
                    long workflowOpId = op.getId();
                    if (workflowOpId != workflowInstanceId) {
                        jobsToDelete.add(workflowOpId);
                    }
                }
            }
            try {
                serviceRegistry.removeJobs(jobsToDelete);
            } catch (ServiceRegistryException e) {
                logger.warn("Problems while removing jobs related to workflow operations '%s': %s", jobsToDelete, e.getMessage());
            } catch (NotFoundException e) {
                logger.debug("No jobs related to one of the workflow operations '%s' found in the service registry", jobsToDelete);
            }
            // Third, remove workflow instance job itself
            try {
                serviceRegistry.removeJobs(Collections.singletonList(workflowInstanceId));
                messageSender.sendObjectMessage(WorkflowItem.WORKFLOW_QUEUE, MessageSender.DestinationType.Queue, WorkflowItem.deleteInstance(workflowInstanceId, instance));
            } catch (ServiceRegistryException e) {
                logger.warn("Problems while removing workflow instance job '%d': %s", workflowInstanceId, ExceptionUtils.getStackTrace(e));
            } catch (NotFoundException e) {
                logger.info("No workflow instance job '%d' found in the service registry", workflowInstanceId);
            }
            // At last, remove workflow instance from the index
            try {
                index.remove(workflowInstanceId);
            } catch (NotFoundException e) {
                // This should never happen, because we got workflow instance by querying the index...
                logger.warn("Workflow instance could not be removed from index: %s", ExceptionUtils.getStackTrace(e));
            }
        } else if (workflows.size() == 0) {
            throw new NotFoundException("Workflow instance with id '" + Long.toString(workflowInstanceId) + "' could not be found");
        } else {
            throw new WorkflowDatabaseException("More than one workflow found with id: " + Long.toString(workflowInstanceId));
        }
    } finally {
        lock.unlock();
    }
}
Also used : WorkflowSet(org.opencastproject.workflow.api.WorkflowSet) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) WorkflowState(org.opencastproject.workflow.api.WorkflowInstance.WorkflowState) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) Lock(java.util.concurrent.locks.Lock) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowStateException(org.opencastproject.workflow.api.WorkflowStateException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Example 93 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class WorkflowServiceImpl method runWorkflowOperation.

/**
 * Executes the workflow's current operation.
 *
 * @param workflow
 *          the workflow
 * @param properties
 *          the properties that are passed in on resume
 * @return the processed workflow operation
 * @throws WorkflowException
 *           if there is a problem processing the workflow
 */
protected WorkflowOperationInstance runWorkflowOperation(WorkflowInstance workflow, Map<String, String> properties) throws WorkflowException, UnauthorizedException {
    WorkflowOperationInstance processingOperation = workflow.getCurrentOperation();
    if (processingOperation == null)
        throw new IllegalStateException("Workflow '" + workflow + "' has no operation to run");
    // Keep the current state for later reference, it might have been changed from the outside
    WorkflowState initialState = workflow.getState();
    // Execute the operation handler
    WorkflowOperationHandler operationHandler = selectOperationHandler(processingOperation);
    WorkflowOperationWorker worker = new WorkflowOperationWorker(operationHandler, workflow, properties, this);
    workflow = worker.execute();
    // The workflow has been serialized/deserialized in between, so we need to refresh the reference
    int currentOperationPosition = processingOperation.getPosition();
    processingOperation = workflow.getOperations().get(currentOperationPosition);
    Long currentOperationJobId = processingOperation.getId();
    try {
        updateOperationJob(currentOperationJobId, processingOperation.getState());
    } catch (NotFoundException e) {
        throw new IllegalStateException("Unable to find a job that has already been running");
    } catch (ServiceRegistryException e) {
        throw new WorkflowDatabaseException(e);
    }
    // Move on to the next workflow operation
    WorkflowOperationInstance currentOperation = workflow.getCurrentOperation();
    // Is the workflow done?
    if (currentOperation == null) {
        // If we are in failing mode, we were simply working off an error handling workflow
        if (FAILING.equals(workflow.getState())) {
            workflow.setState(FAILED);
        } else // switched to paused while processing the error handling workflow extension
        if (!FAILED.equals(workflow.getState())) {
            workflow.setState(SUCCEEDED);
            for (WorkflowOperationInstance op : workflow.getOperations()) {
                if (op.getState().equals(WorkflowOperationInstance.OperationState.FAILED)) {
                    if (op.isFailWorkflowOnException()) {
                        workflow.setState(FAILED);
                        break;
                    }
                }
            }
        }
        // Save the updated workflow to the database
        logger.debug("%s has %s", workflow, workflow.getState());
        update(workflow);
    } else {
        // Somebody might have set the workflow to "paused" from the outside, so take a look a the database first
        WorkflowState dbWorkflowState = null;
        try {
            dbWorkflowState = getWorkflowById(workflow.getId()).getState();
        } catch (WorkflowDatabaseException e) {
            throw new IllegalStateException("The workflow with ID " + workflow.getId() + " can not be accessed in the database", e);
        } catch (NotFoundException e) {
            throw new IllegalStateException("The workflow with ID " + workflow.getId() + " can not be found in the database", e);
        } catch (UnauthorizedException e) {
            throw new IllegalStateException("The workflow with ID " + workflow.getId() + " can not be read", e);
        }
        // If somebody changed the workflow state from the outside, that state should take precedence
        if (!dbWorkflowState.equals(initialState)) {
            logger.info("Workflow state for %s was changed to '%s' from the outside", workflow, dbWorkflowState);
            workflow.setState(dbWorkflowState);
        }
        // Save the updated workflow to the database
        Job job = null;
        switch(workflow.getState()) {
            case FAILED:
                update(workflow);
                break;
            case FAILING:
            case RUNNING:
                try {
                    job = serviceRegistry.createJob(JOB_TYPE, Operation.START_OPERATION.toString(), Arrays.asList(Long.toString(workflow.getId())), null, false, null, WORKFLOW_JOB_LOAD);
                    currentOperation.setId(job.getId());
                    update(workflow);
                    job.setStatus(Status.QUEUED);
                    job.setDispatchable(true);
                    job = 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");
                }
                break;
            case PAUSED:
            case STOPPED:
            case SUCCEEDED:
                update(workflow);
                break;
            case INSTANTIATED:
                update(workflow);
                throw new IllegalStateException("Impossible workflow state found during processing");
            default:
                throw new IllegalStateException("Unkown workflow state found during processing");
        }
    }
    return processingOperation;
}
Also used : WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowState(org.opencastproject.workflow.api.WorkflowInstance.WorkflowState) ResumableWorkflowOperationHandler(org.opencastproject.workflow.api.ResumableWorkflowOperationHandler) WorkflowOperationHandler(org.opencastproject.workflow.api.WorkflowOperationHandler) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) Job(org.opencastproject.job.api.Job) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 94 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class WorkflowServiceImpl method getWorkflowById.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#getWorkflowById(long)
 */
@Override
public WorkflowInstanceImpl getWorkflowById(long id) throws WorkflowDatabaseException, NotFoundException, UnauthorizedException {
    try {
        Job job = serviceRegistry.getJob(id);
        if (Status.DELETED.equals(job.getStatus())) {
            throw new NotFoundException("Workflow '" + id + "' has been deleted");
        }
        if (JOB_TYPE.equals(job.getJobType()) && Operation.START_WORKFLOW.toString().equals(job.getOperation())) {
            WorkflowInstanceImpl workflow = WorkflowParser.parseWorkflowInstance(job.getPayload());
            assertPermission(workflow, Permissions.Action.READ.toString());
            return workflow;
        } else {
            throw new NotFoundException("'" + id + "' is a job identifier, but it is not a workflow identifier");
        }
    } catch (WorkflowParsingException e) {
        throw new IllegalStateException("The workflow job payload is malformed");
    } catch (ServiceRegistryException e) {
        throw new IllegalStateException("Error loading workflow job from the service registry");
    } catch (MediaPackageException e) {
        throw new IllegalStateException("Unable to read mediapackage from workflow " + id, e);
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) NotFoundException(org.opencastproject.util.NotFoundException) Job(org.opencastproject.job.api.Job) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Example 95 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class WorkflowServiceImpl method resume.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#resume(long, Map)
 */
@Override
public WorkflowInstance resume(long workflowInstanceId, Map<String, String> properties) throws WorkflowException, NotFoundException, IllegalStateException, UnauthorizedException {
    WorkflowInstance workflowInstance = getWorkflowById(workflowInstanceId);
    if (!WorkflowState.PAUSED.equals(workflowInstance.getState()))
        throw new IllegalStateException("Can not resume a workflow where the current state is not in paused");
    workflowInstance = updateConfiguration(workflowInstance, properties);
    update(workflowInstance);
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    // Is the workflow done?
    if (currentOperation == null) {
        // Let's make sure we didn't miss any failed operation, since the workflow state could have been
        // switched to paused while processing the error handling workflow extension
        workflowInstance.setState(SUCCEEDED);
        for (WorkflowOperationInstance op : workflowInstance.getOperations()) {
            if (op.getState().equals(WorkflowOperationInstance.OperationState.FAILED)) {
                if (op.isFailWorkflowOnException()) {
                    workflowInstance.setState(FAILED);
                    break;
                }
            }
        }
        // Save the resumed workflow to the database
        logger.debug("%s has %s", workflowInstance, workflowInstance.getState());
        update(workflowInstance);
        return workflowInstance;
    }
    // certain operations. In the latter case, there is no current paused operation.
    if (OperationState.INSTANTIATED.equals(currentOperation.getState())) {
        try {
            // the operation has its own job. Update that too.
            Job operationJob = serviceRegistry.createJob(JOB_TYPE, Operation.START_OPERATION.toString(), Arrays.asList(Long.toString(workflowInstanceId)), null, false, null, WORKFLOW_JOB_LOAD);
            // this method call is publicly visible, so it doesn't necessarily go through the accept method. Set the
            // workflow state manually.
            workflowInstance.setState(RUNNING);
            currentOperation.setId(operationJob.getId());
            // update the workflow and its associated job
            update(workflowInstance);
            // Now set this job to be queued so it can be dispatched
            operationJob.setStatus(Status.QUEUED);
            operationJob.setDispatchable(true);
            operationJob = serviceRegistry.updateJob(operationJob);
            return workflowInstance;
        } catch (ServiceRegistryException e) {
            throw new WorkflowDatabaseException(e);
        }
    }
    Long operationJobId = workflowInstance.getCurrentOperation().getId();
    if (operationJobId == null)
        throw new IllegalStateException("Can not resume a workflow where the current operation has no associated id");
    // Set the current operation's job to queued, so it gets picked up again
    Job workflowJob;
    try {
        workflowJob = serviceRegistry.getJob(workflowInstanceId);
        workflowJob.setStatus(Status.RUNNING);
        workflowJob.setPayload(WorkflowParser.toXml(workflowInstance));
        workflowJob = serviceRegistry.updateJob(workflowJob);
        Job operationJob = serviceRegistry.getJob(operationJobId);
        operationJob.setStatus(Status.QUEUED);
        operationJob.setDispatchable(true);
        if (properties != null) {
            Properties props = new Properties();
            props.putAll(properties);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            props.store(out, null);
            List<String> newArguments = new ArrayList<String>(operationJob.getArguments());
            newArguments.add(new String(out.toByteArray(), "UTF-8"));
            operationJob.setArguments(newArguments);
        }
        operationJob = serviceRegistry.updateJob(operationJob);
    } catch (ServiceRegistryException e) {
        throw new WorkflowDatabaseException(e);
    } catch (IOException e) {
        throw new WorkflowParsingException("Unable to parse workflow and/or workflow properties");
    }
    return workflowInstance;
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Collections.mkString(org.opencastproject.util.data.Collections.mkString) IOException(java.io.IOException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Properties(java.util.Properties) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) Job(org.opencastproject.job.api.Job) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Aggregations

ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)99 NotFoundException (org.opencastproject.util.NotFoundException)61 ConfigurationException (org.osgi.service.cm.ConfigurationException)41 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)30 URISyntaxException (java.net.URISyntaxException)29 Job (org.opencastproject.job.api.Job)29 PersistenceException (javax.persistence.PersistenceException)26 RollbackException (javax.persistence.RollbackException)26 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)26 NoResultException (javax.persistence.NoResultException)25 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)25 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)24 EntityManager (javax.persistence.EntityManager)22 MediaPackage (org.opencastproject.mediapackage.MediaPackage)20 URI (java.net.URI)16 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)14 DistributionException (org.opencastproject.distribution.api.DistributionException)13 Attachment (org.opencastproject.mediapackage.Attachment)12 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)12