Search in sources :

Example 1 with WorkflowListener

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

the class WorkflowServiceImpl method fireListeners.

/**
 * Fires the workflow listeners on workflow updates.
 */
protected void fireListeners(final WorkflowInstance oldWorkflowInstance, final WorkflowInstance newWorkflowInstance) {
    final User currentUser = securityService.getUser();
    final Organization currentOrganization = securityService.getOrganization();
    for (final WorkflowListener listener : listeners) {
        if (oldWorkflowInstance == null || !oldWorkflowInstance.getState().equals(newWorkflowInstance.getState())) {
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    try {
                        securityService.setUser(currentUser);
                        securityService.setOrganization(currentOrganization);
                        listener.stateChanged(newWorkflowInstance);
                    } finally {
                        securityService.setUser(null);
                        securityService.setOrganization(null);
                    }
                }
            };
            executorService.execute(runnable);
        } else {
            logger.debug("Not notifying %s because the workflow state has not changed", listener);
        }
        if (newWorkflowInstance.getCurrentOperation() != null) {
            if (oldWorkflowInstance == null || oldWorkflowInstance.getCurrentOperation() == null || !oldWorkflowInstance.getCurrentOperation().equals(newWorkflowInstance.getCurrentOperation())) {
                Runnable runnable = new Runnable() {

                    @Override
                    public void run() {
                        try {
                            securityService.setUser(currentUser);
                            securityService.setOrganization(currentOrganization);
                            listener.operationChanged(newWorkflowInstance);
                        } finally {
                            securityService.setUser(null);
                            securityService.setOrganization(null);
                        }
                    }
                };
                executorService.execute(runnable);
            }
        } else {
            logger.debug("Not notifying %s because the workflow operation has not changed", listener);
        }
    }
}
Also used : User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) WorkflowListener(org.opencastproject.workflow.api.WorkflowListener)

Example 2 with WorkflowListener

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

the class WorkflowStatisticsTest method testStatistics.

/**
 * Tests whether the workflow service statistics are gathered correctly.
 */
@Test
public void testStatistics() throws Exception {
    // Start the workflows and advance them in "random" order. With every definition, an instance is started for every
    // operation that is part of the definition. So we end up with an instance per definition and operation, and there
    // are no two workflows that are in the same operation.
    int total = 0;
    int paused = 0;
    int failed = 0;
    int failing = 0;
    int instantiated = 0;
    int running = 0;
    int stopped = 0;
    int succeeded = 0;
    WorkflowStateListener listener = new WorkflowStateListener(WorkflowState.PAUSED);
    service.addWorkflowListener(listener);
    List<WorkflowInstance> instances = new ArrayList<WorkflowInstance>();
    for (WorkflowDefinition def : workflowDefinitions) {
        for (int j = 0; j < def.getOperations().size(); j++) {
            mediaPackage.setIdentifier(new UUIDIdBuilderImpl().createNew());
            instances.add(service.start(def, mediaPackage));
            total++;
            paused++;
        }
    }
    // Wait for all the workflows to go into "paused" state
    synchronized (listener) {
        while (listener.countStateChanges() < WORKFLOW_DEFINITION_COUNT * OPERATION_COUNT) {
            listener.wait();
        }
    }
    service.removeWorkflowListener(listener);
    // Resume all of them, so some will be finished, some won't
    int j = 0;
    for (WorkflowInstance instance : instances) {
        WorkflowListener instanceListener = new IndividualWorkflowListener(instance.getId());
        service.addWorkflowListener(instanceListener);
        for (int k = 0; k <= (j % OPERATION_COUNT - 1); k++) {
            synchronized (instanceListener) {
                service.resume(instance.getId(), null);
                instanceListener.wait();
            }
        }
        j++;
    }
    // TODO: Add failed, failing, stopped etc. workflows as well
    // Get the statistics
    WorkflowStatistics stats = service.getStatistics();
    assertEquals(failed, stats.getFailed());
    assertEquals(failing, stats.getFailing());
    assertEquals(instantiated, stats.getInstantiated());
    assertEquals(succeeded, stats.getFinished());
    assertEquals(paused, stats.getPaused());
    assertEquals(running, stats.getRunning());
    assertEquals(stopped, stats.getStopped());
    assertEquals(total, stats.getTotal());
// TODO: Test the operations
// Make sure they are as expected
// for (WorkflowDefinitionReport report : stats.getDefinitions()) {
// 
// }
}
Also used : WorkflowStateListener(org.opencastproject.workflow.api.WorkflowStateListener) ArrayList(java.util.ArrayList) WorkflowDefinition(org.opencastproject.workflow.api.WorkflowDefinition) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowListener(org.opencastproject.workflow.api.WorkflowListener) WorkflowStatistics(org.opencastproject.workflow.api.WorkflowStatistics) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl) Test(org.junit.Test)

Aggregations

WorkflowListener (org.opencastproject.workflow.api.WorkflowListener)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 UUIDIdBuilderImpl (org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl)1 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)1 Organization (org.opencastproject.security.api.Organization)1 User (org.opencastproject.security.api.User)1 WorkflowDefinition (org.opencastproject.workflow.api.WorkflowDefinition)1 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)1 WorkflowStateListener (org.opencastproject.workflow.api.WorkflowStateListener)1 WorkflowStatistics (org.opencastproject.workflow.api.WorkflowStatistics)1