Search in sources :

Example 1 with WorkflowStateException

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

the class AbstractEventEndpoint method deleteWorkflow.

@DELETE
@Path("{eventId}/workflows/{workflowId}")
@RestQuery(name = "deleteWorkflow", description = "Deletes a workflow", returnDescription = "The method doesn't return any content", pathParameters = { @RestParameter(name = "eventId", isRequired = true, description = "The event identifier", type = RestParameter.Type.STRING), @RestParameter(name = "workflowId", isRequired = true, description = "The workflow identifier", type = RestParameter.Type.INTEGER) }, reponses = { @RestResponse(responseCode = SC_BAD_REQUEST, description = "When trying to delete the latest workflow of the event."), @RestResponse(responseCode = SC_NOT_FOUND, description = "If the event or the workflow has not been found."), @RestResponse(responseCode = SC_NO_CONTENT, description = "The method does not return any content") })
public Response deleteWorkflow(@PathParam("eventId") String id, @PathParam("workflowId") long wfId) throws SearchIndexException {
    final Opt<Event> optEvent = getIndexService().getEvent(id, getIndex());
    try {
        if (optEvent.isNone()) {
            return notFound("Cannot find an event with id '%s'.", id);
        }
        final WorkflowInstance wfInstance = getWorkflowService().getWorkflowById(wfId);
        if (!wfInstance.getMediaPackage().getIdentifier().toString().equals(id)) {
            return badRequest(String.format("Workflow %s is not associated to event %s", wfId, id));
        }
        if (wfId == optEvent.get().getWorkflowId()) {
            return badRequest(String.format("Cannot delete current workflow %s from event %s." + " Only older workflows can be deleted.", wfId, id));
        }
        getWorkflowService().remove(wfId);
        return Response.noContent().build();
    } catch (WorkflowStateException e) {
        return badRequest("Deleting is not allowed for current workflow state. EventId: " + id);
    } catch (NotFoundException e) {
        return notFound("Workflow not found: '%d'.", wfId);
    } catch (UnauthorizedException e) {
        return forbidden();
    } catch (Exception e) {
        return serverError();
    }
}
Also used : WorkflowStateException(org.opencastproject.workflow.api.WorkflowStateException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) Event(org.opencastproject.index.service.impl.index.event.Event) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WebApplicationException(javax.ws.rs.WebApplicationException) EventCommentException(org.opencastproject.event.comment.EventCommentException) JSONException(org.codehaus.jettison.json.JSONException) JobEndpointException(org.opencastproject.adminui.exception.JobEndpointException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UrlSigningException(org.opencastproject.security.urlsigning.exception.UrlSigningException) AclServiceException(org.opencastproject.authorization.xacml.manager.api.AclServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowStateException(org.opencastproject.workflow.api.WorkflowStateException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 2 with WorkflowStateException

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

the class WorkflowServiceImpl method cleanupWorkflowInstances.

@Override
public synchronized void cleanupWorkflowInstances(int buffer, WorkflowState state) throws UnauthorizedException, WorkflowDatabaseException {
    logger.info("Start cleaning up workflow instances older than {} days with status '{}'", buffer, state);
    int instancesCleaned = 0;
    int cleaningFailed = 0;
    WorkflowQuery query = new WorkflowQuery().withState(state).withDateBefore(DateUtils.addDays(new Date(), -buffer)).withCount(Integer.MAX_VALUE);
    for (WorkflowInstance workflowInstance : getWorkflowInstances(query).getItems()) {
        try {
            remove(workflowInstance.getId());
            instancesCleaned++;
        } catch (WorkflowDatabaseException e) {
            throw e;
        } catch (UnauthorizedException e) {
            throw e;
        } catch (NotFoundException e) {
            // Since we are in a cleanup operation, we don't have to care about NotFoundExceptions
            logger.debug("Workflow instance '{}' could not be removed: {}", workflowInstance.getId(), ExceptionUtils.getStackTrace(e));
        } catch (WorkflowParsingException e) {
            logger.warn("Workflow instance '{}' could not be removed: {}", workflowInstance.getId(), ExceptionUtils.getStackTrace(e));
            cleaningFailed++;
        } catch (WorkflowStateException e) {
            logger.warn("Workflow instance '{}' could not be removed: {}", workflowInstance.getId(), ExceptionUtils.getStackTrace(e));
            cleaningFailed++;
        }
    }
    if (instancesCleaned == 0 && cleaningFailed == 0) {
        logger.info("No workflow instances found to clean up");
        return;
    }
    if (instancesCleaned > 0)
        logger.info("Cleaned up '%d' workflow instances", instancesCleaned);
    if (cleaningFailed > 0) {
        logger.warn("Cleaning failed for '%d' workflow instances", cleaningFailed);
        throw new WorkflowDatabaseException("Unable to clean all workflow instances, see logs!");
    }
}
Also used : WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowQuery(org.opencastproject.workflow.api.WorkflowQuery) WorkflowStateException(org.opencastproject.workflow.api.WorkflowStateException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Date(java.util.Date) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException)

Example 3 with WorkflowStateException

use of org.opencastproject.workflow.api.WorkflowStateException 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)

Aggregations

NotFoundException (org.opencastproject.util.NotFoundException)3 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)3 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)3 WorkflowStateException (org.opencastproject.workflow.api.WorkflowStateException)3 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)2 WorkflowParsingException (org.opencastproject.workflow.api.WorkflowParsingException)2 WorkflowQuery (org.opencastproject.workflow.api.WorkflowQuery)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Lock (java.util.concurrent.locks.Lock)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JSONException (org.codehaus.jettison.json.JSONException)1 JobEndpointException (org.opencastproject.adminui.exception.JobEndpointException)1 AclServiceException (org.opencastproject.authorization.xacml.manager.api.AclServiceException)1 EventCommentException (org.opencastproject.event.comment.EventCommentException)1 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)1