Search in sources :

Example 1 with ServiceRegistration

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

the class IngestDownloadWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    boolean deleteExternal = BooleanUtils.toBoolean(currentOperation.getConfiguration(DELETE_EXTERNAL));
    String baseUrl = workspace.getBaseUri().toString();
    List<URI> externalUris = new ArrayList<URI>();
    for (MediaPackageElement element : mediaPackage.getElements()) {
        if (element.getURI() == null)
            continue;
        if (element.getElementType() == MediaPackageElement.Type.Publication) {
            logger.debug("Skipping downloading media package element {} from media package {} " + "because it is a publication: {}", element.getIdentifier(), mediaPackage.getIdentifier().compact(), element.getURI());
            continue;
        }
        URI originalElementUri = element.getURI();
        if (originalElementUri.toString().startsWith(baseUrl)) {
            logger.info("Skipping downloading already existing element {}", originalElementUri);
            continue;
        }
        // Download the external URI
        File file;
        try {
            file = workspace.get(element.getURI());
        } catch (Exception e) {
            logger.warn("Unable to download the external element {}", element.getURI());
            throw new WorkflowOperationException("Unable to download the external element " + element.getURI(), e);
        }
        // Put to working file repository and rewrite URI on element
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            URI uri = workspace.put(mediaPackage.getIdentifier().compact(), element.getIdentifier(), FilenameUtils.getName(element.getURI().getPath()), in);
            element.setURI(uri);
        } catch (Exception e) {
            logger.warn("Unable to store downloaded element '{}': {}", element.getURI(), e.getMessage());
            throw new WorkflowOperationException("Unable to store downloaded element " + element.getURI(), e);
        } finally {
            IOUtils.closeQuietly(in);
            try {
                workspace.delete(originalElementUri);
            } catch (Exception e) {
                logger.warn("Unable to delete ingest-downloaded element {}: {}", element.getURI(), e);
            }
        }
        logger.info("Downloaded the external element {}", originalElementUri);
        // Store origianl URI for deletion
        externalUris.add(originalElementUri);
    }
    if (!deleteExternal || externalUris.size() == 0)
        return createResult(mediaPackage, Action.CONTINUE);
    // Find all external working file repository base Urls
    logger.debug("Assembling list of external working file repositories");
    List<String> externalWfrBaseUrls = new ArrayList<String>();
    try {
        for (ServiceRegistration reg : serviceRegistry.getServiceRegistrationsByType(WorkingFileRepository.SERVICE_TYPE)) {
            if (baseUrl.startsWith(reg.getHost())) {
                logger.trace("Skpping local working file repository");
                continue;
            }
            externalWfrBaseUrls.add(UrlSupport.concat(reg.getHost(), reg.getPath()));
        }
        logger.debug("{} external working file repositories found", externalWfrBaseUrls.size());
    } catch (ServiceRegistryException e) {
        logger.error("Unable to load WFR services from service registry: {}", e.getMessage());
        throw new WorkflowOperationException(e);
    }
    for (URI uri : externalUris) {
        String elementUri = uri.toString();
        // Delete external working file repository URI's
        String wfrBaseUrl = null;
        for (String url : externalWfrBaseUrls) {
            if (elementUri.startsWith(url)) {
                wfrBaseUrl = url;
                break;
            }
        }
        if (wfrBaseUrl == null) {
            logger.info("Unable to delete external URI {}, no working file repository found", elementUri);
            continue;
        }
        HttpDelete delete;
        if (elementUri.startsWith(UrlSupport.concat(wfrBaseUrl, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX))) {
            String wfrDeleteUrl = elementUri.substring(0, elementUri.lastIndexOf("/"));
            delete = new HttpDelete(wfrDeleteUrl);
        } else if (elementUri.startsWith(UrlSupport.concat(wfrBaseUrl, WorkingFileRepository.COLLECTION_PATH_PREFIX))) {
            delete = new HttpDelete(elementUri);
        } else {
            logger.info("Unable to handle working file repository URI {}", elementUri);
            continue;
        }
        HttpResponse response = null;
        try {
            response = client.execute(delete);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_OK) {
                logger.info("Sucessfully deleted external URI {}", delete.getURI());
            } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
                logger.info("External URI {} has already been deleted", delete.getURI());
            } else {
                logger.info("Unable to delete external URI {}, status code '{}' returned", delete.getURI(), statusCode);
            }
        } catch (TrustedHttpClientException e) {
            logger.warn("Unable to execute DELETE request on external URI {}", delete.getURI());
            throw new WorkflowOperationException(e);
        } finally {
            client.close(response);
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) FileInputStream(java.io.FileInputStream) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) File(java.io.File) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 2 with ServiceRegistration

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

the class ServicesListProvider method getList.

@Override
public Map<String, String> getList(String listName, ResourceListQuery query, Organization organization) throws ListProviderException {
    ServicesListQuery servicesQuery;
    try {
        servicesQuery = (ServicesListQuery) query;
    } catch (ClassCastException ex) {
        servicesQuery = new ServicesListQuery(query);
    }
    Map<String, String> result = new HashMap<String, String>();
    if (LIST_STATUS.equals(listName)) {
        for (ServiceState s : ServiceState.values()) {
            result.put(s.toString(), SERVICE_STATUS_FILTER_PREFIX + s.toString());
        }
        return result;
    }
    List<ServiceRegistration> serviceRegistrations;
    try {
        serviceRegistrations = serviceRegistry.getServiceRegistrations();
    } catch (ServiceRegistryException ex) {
        throw new ListProviderException("Failed to get service registrations.", ex);
    }
    for (ServiceRegistration serviceRegistration : serviceRegistrations) {
        if (servicesQuery.getHostname().isSome() && !StringUtils.equals(servicesQuery.getHostname().get(), serviceRegistration.getHost()))
            continue;
        if (servicesQuery.getActions().isSome() && servicesQuery.getActions().get() && serviceRegistration.getServiceState() == ServiceState.NORMAL)
            continue;
        result.put(serviceRegistration.getServiceType(), serviceRegistration.getServiceType());
    }
    if (servicesQuery.getLimit().isSome() || servicesQuery.getLimit().isSome()) {
        int limit = servicesQuery.getLimit().getOrElse(0);
        int offset = servicesQuery.getOffset().getOrElse(0);
        result = new SmartIterator(limit, offset).applyLimitAndOffset(result);
    }
    return result;
}
Also used : ServiceState(org.opencastproject.serviceregistry.api.ServiceState) SmartIterator(org.opencastproject.util.SmartIterator) HashMap(java.util.HashMap) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) ServicesListQuery(org.opencastproject.index.service.resources.list.query.ServicesListQuery) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 3 with ServiceRegistration

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

the class ServiceRegistryJpaImpl method unregisterHost.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#unregisterHost(java.lang.String)
 */
@Override
public void unregisterHost(String host) throws ServiceRegistryException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        HostRegistrationJpaImpl existingHostRegistration = fetchHostRegistration(em, host);
        if (existingHostRegistration == null) {
            throw new IllegalArgumentException("Host '" + host + "' is not registered, so it can not be unregistered");
        }
        existingHostRegistration.setOnline(false);
        for (ServiceRegistration serviceRegistration : getServiceRegistrationsByHost(host)) {
            unRegisterService(serviceRegistration.getServiceType(), serviceRegistration.getHost());
        }
        em.merge(existingHostRegistration);
        logger.info("Unregistering {}", host);
        tx.commit();
        logger.info("Host {} unregistered", host);
        hostsStatistics.updateHost(existingHostRegistration);
    } catch (Exception e) {
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) HostRegistrationJpaImpl(org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl) 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) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 4 with ServiceRegistration

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

the class MaintenanceCommand method list.

public String list() {
    try {
        StringBuilder sb = new StringBuilder();
        for (ServiceRegistration reg : serviceRegistry.getServiceRegistrations()) {
            sb.append(reg.getServiceType());
            sb.append("@");
            sb.append(reg.getHost());
            if (reg.isInMaintenanceMode()) {
                sb.append(" (maintenance mode)");
            }
            sb.append("\n");
        }
        return sb.toString();
    } catch (ServiceRegistryException e) {
        return "Error: " + e.getMessage() + "\n";
    }
}
Also used : ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 5 with ServiceRegistration

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

the class JobTest method testGetHostsCount.

@Test
public void testGetHostsCount() throws Exception {
    Job localRunning1 = serviceRegistry.createJob(JOB_TYPE_1, OPERATION_NAME, null, null, false);
    localRunning1.setStatus(Status.RUNNING);
    localRunning1.setJobType(regType1Localhost.getServiceType());
    localRunning1.setProcessingHost(regType1Localhost.getHost());
    localRunning1 = serviceRegistry.updateJob(localRunning1);
    Job localRunning2 = serviceRegistry.createJob(JOB_TYPE_1, OPERATION_NAME, null, null, false);
    localRunning2.setStatus(Status.RUNNING);
    localRunning2.setJobType(regType1Localhost.getServiceType());
    localRunning2.setProcessingHost(regType1Localhost.getHost());
    localRunning2 = serviceRegistry.updateJob(localRunning2);
    Job localFinished = serviceRegistry.createJob(JOB_TYPE_1, OPERATION_NAME, null, null, false);
    // Simulate starting the job
    localFinished.setStatus(Status.RUNNING);
    localFinished.setJobType(regType1Localhost.getServiceType());
    localFinished.setProcessingHost(regType1Localhost.getHost());
    localFinished = serviceRegistry.updateJob(localFinished);
    // Finish the job
    localFinished = serviceRegistry.getJob(localFinished.getId());
    localFinished.setStatus(Status.FINISHED);
    localFinished = serviceRegistry.updateJob(localFinished);
    Job remoteRunning = serviceRegistry.createJob(regType1Remotehost.getHost(), regType1Remotehost.getServiceType(), OPERATION_NAME, null, null, false, null);
    remoteRunning.setStatus(Status.RUNNING);
    remoteRunning.setJobType(regType1Remotehost.getServiceType());
    remoteRunning.setProcessingHost(regType1Remotehost.getHost());
    remoteRunning = serviceRegistry.updateJob(remoteRunning);
    Job remoteFinished = serviceRegistry.createJob(regType1Remotehost.getHost(), regType1Remotehost.getServiceType(), OPERATION_NAME, null, null, false, null);
    // Simulate starting the job
    remoteFinished.setStatus(Status.RUNNING);
    remoteFinished.setJobType(regType1Remotehost.getServiceType());
    remoteFinished.setProcessingHost(regType1Remotehost.getHost());
    remoteFinished = serviceRegistry.updateJob(remoteFinished);
    // Finish the job
    remoteFinished = serviceRegistry.getJob(remoteFinished.getId());
    remoteFinished.setStatus(Status.FINISHED);
    remoteFinished = serviceRegistry.updateJob(remoteFinished);
    Job otherTypeRunning = serviceRegistry.createJob(JOB_TYPE_2, OPERATION_NAME, null, null, false);
    otherTypeRunning.setStatus(Status.RUNNING);
    otherTypeRunning.setJobType(regType2Localhost.getServiceType());
    otherTypeRunning.setProcessingHost(regType2Localhost.getHost());
    otherTypeRunning = serviceRegistry.updateJob(otherTypeRunning);
    Job otherTypeFinished = serviceRegistry.createJob(JOB_TYPE_2, OPERATION_NAME, null, null, false);
    // Simulate starting the job
    otherTypeFinished.setStatus(Status.RUNNING);
    otherTypeFinished.setJobType(regType2Localhost.getServiceType());
    otherTypeFinished.setProcessingHost(regType2Localhost.getHost());
    otherTypeFinished = serviceRegistry.updateJob(otherTypeFinished);
    // Finish the job
    otherTypeFinished = serviceRegistry.getJob(otherTypeFinished.getId());
    otherTypeFinished.setStatus(Status.FINISHED);
    otherTypeFinished = serviceRegistry.updateJob(otherTypeFinished);
    List<ServiceRegistration> type1Hosts = serviceRegistry.getServiceRegistrationsByLoad(JOB_TYPE_1);
    List<ServiceRegistration> type2Hosts = serviceRegistry.getServiceRegistrationsByLoad(JOB_TYPE_2);
    // The number of service registrations is equal on both hosts
    assertEquals(2, type1Hosts.size());
    assertEquals(2, type2Hosts.size());
    // Count the number of jobs that are runnging
    assertEquals(3, serviceRegistry.count(JOB_TYPE_1, Status.RUNNING));
    assertEquals(1, serviceRegistry.count(JOB_TYPE_2, Status.RUNNING));
    // Localhost has more jobs running than remotehost
    assertEquals(REMOTEHOST, type1Hosts.get(0).getHost());
    assertEquals(LOCALHOST, type1Hosts.get(1).getHost());
    assertEquals(REMOTEHOST, type2Hosts.get(0).getHost());
    assertEquals(LOCALHOST, type2Hosts.get(1).getHost());
}
Also used : JaxbJob(org.opencastproject.job.api.JaxbJob) Job(org.opencastproject.job.api.Job) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Test(org.junit.Test)

Aggregations

ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)20 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 EntityManager (javax.persistence.EntityManager)5 Job (org.opencastproject.job.api.Job)5 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)5 NotFoundException (org.opencastproject.util.NotFoundException)5 URISyntaxException (java.net.URISyntaxException)4 NoResultException (javax.persistence.NoResultException)4 PersistenceException (javax.persistence.PersistenceException)4 RollbackException (javax.persistence.RollbackException)4 HostRegistration (org.opencastproject.serviceregistry.api.HostRegistration)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 EntityTransaction (javax.persistence.EntityTransaction)3 ServiceRegistry (org.opencastproject.serviceregistry.api.ServiceRegistry)3 SystemLoad (org.opencastproject.serviceregistry.api.SystemLoad)3 HostRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.HostRegistrationJpaImpl)3 ServiceRegistrationJpaImpl (org.opencastproject.serviceregistry.impl.jpa.ServiceRegistrationJpaImpl)3 ConfigurationException (org.osgi.service.cm.ConfigurationException)3