Search in sources :

Example 11 with SchedulerException

use of org.opencastproject.scheduler.api.SchedulerException in project opencast by opencast.

the class SchedulerServiceImpl method getCalendar.

@Override
public String getCalendar(Opt<String> captureAgentId, Opt<String> seriesId, Opt<Date> cutoff) throws SchedulerException {
    try {
        AQueryBuilder query = assetManager.createQuery();
        Props p = new Props(query);
        Predicate predicate = withOrganization(query).and(withOwner(query)).and(query.hasPropertiesOf(p.namespace())).and(p.optOut().eq(false)).and(withVersion(query)).and(p.end().ge(DateTime.now().minusHours(1).toDate()));
        for (String agentId : captureAgentId) {
            predicate = predicate.and(p.agent().eq(agentId));
        }
        for (String series : seriesId) {
            predicate = predicate.and(query.seriesId().eq(series));
        }
        for (Date d : cutoff) {
            predicate = predicate.and(p.start().le(d));
        }
        ASelectQuery select = query.select(query.snapshot(), p.agent().target(), p.start().target(), p.end().target(), query.propertiesOf(CA_NAMESPACE)).where(predicate);
        Stream<ARecord> records = select.run().getRecords();
        CalendarGenerator cal = new CalendarGenerator(seriesService);
        for (ARecord record : records) {
            boolean blacklisted;
            // isBlacklisted() methods are not implemented in the persistence layer and return always false
            // try {
            // //blacklisted = isBlacklisted(record.getMediaPackageId());
            // } catch (NotFoundException e) {
            // continue;
            // }
            blacklisted = false;
            // Skip blacklisted events
            if (blacklisted)
                continue;
            Opt<MediaPackage> optMp = record.getSnapshot().map(episodeToMp);
            // If the event media package is empty, skip the event
            if (optMp.isNone()) {
                logger.warn("Mediapackage for event '{}' can't be found, event is not recorded", record.getMediaPackageId());
                continue;
            }
            Opt<DublinCoreCatalog> catalogOpt = loadEpisodeDublinCoreFromAsset(record.getSnapshot().get());
            if (catalogOpt.isNone()) {
                logger.warn("No episode catalog available, skipping!");
                continue;
            }
            Map<String, String> caMetadata = record.getProperties().filter(filterByNamespace._2(CA_NAMESPACE)).group(toKey, toValue);
            // If the even properties are empty, skip the event
            if (caMetadata.isEmpty()) {
                logger.warn("Properties for event '{}' can't be found, event is not recorded", record.getMediaPackageId());
                continue;
            }
            String agentId = record.getProperties().apply(Properties.getString(AGENT_CONFIG));
            Date start = record.getProperties().apply(Properties.getDate(START_DATE_CONFIG));
            Date end = record.getProperties().apply(Properties.getDate(END_DATE_CONFIG));
            Date lastModified = record.getSnapshot().get().getArchivalDate();
            // Add the entry to the calendar, skip it with a warning if adding fails
            try {
                cal.addEvent(optMp.get(), catalogOpt.get(), agentId, start, end, lastModified, toPropertyString(caMetadata));
            } catch (Exception e) {
                logger.warn("Error adding event '{}' to calendar, event is not recorded: {}", record.getMediaPackageId(), getStackTrace(e));
                continue;
            }
        }
        // Only validate calendars with events. Without any events, the iCalendar won't validate
        if (cal.getCalendar().getComponents().size() > 0) {
            try {
                cal.getCalendar().validate();
            } catch (ValidationException e) {
                logger.warn("Recording calendar could not be validated (returning it anyways): {}", getStackTrace(e));
            }
        }
        return cal.getCalendar().toString();
    } catch (Exception e) {
        if (e instanceof SchedulerException)
            throw e;
        logger.error("Failed getting calendar: {}", getStackTrace(e));
        throw new SchedulerException(e);
    }
}
Also used : ValidationException(net.fortuna.ical4j.model.ValidationException) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) Log.getHumanReadableTimeString(org.opencastproject.util.Log.getHumanReadableTimeString) Date(java.util.Date) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) IOException(java.io.IOException) ServiceException(org.osgi.framework.ServiceException) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SeriesException(org.opencastproject.series.api.SeriesException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ValidationException(net.fortuna.ical4j.model.ValidationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) Predicate(org.opencastproject.assetmanager.api.query.Predicate) ARecord(org.opencastproject.assetmanager.api.query.ARecord) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ASelectQuery(org.opencastproject.assetmanager.api.query.ASelectQuery) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 12 with SchedulerException

use of org.opencastproject.scheduler.api.SchedulerException in project opencast by opencast.

the class SchedulerServiceImpl method updateRecordingState.

@Override
public boolean updateRecordingState(String id, String state) throws NotFoundException, SchedulerException {
    notEmpty(id, "id");
    notEmpty(state, "state");
    if (!RecordingState.KNOWN_STATES.contains(state)) {
        logger.warn("Invalid recording state: {}.", state);
        return false;
    }
    try {
        AQueryBuilder query = assetManager.createQuery();
        Props p = new Props(query);
        AResult result = query.select(p.recordingStatus().target(), p.recordingLastHeard().target()).where(withOrganization(query).and(query.mediaPackageId(id).and(query.version().isLatest()).and(query.hasPropertiesOf(p.namespace())))).run();
        Opt<ARecord> record = result.getRecords().head();
        if (record.isNone())
            throw new NotFoundException();
        Opt<String> recordingState = record.get().getProperties().apply(Properties.getStringOpt(RECORDING_STATE_CONFIG));
        Opt<Long> lastHeard = record.get().getProperties().apply(Properties.getLongOpt(RECORDING_LAST_HEARD_CONFIG));
        if (recordingState.isSome() && lastHeard.isSome()) {
            Recording r = new RecordingImpl(id, recordingState.get(), lastHeard.get());
            if (state.equals(r.getState())) {
                logger.debug("Recording state not changed");
                // Reset the state anyway so that the last-heard-from time is correct...
                r.setState(state);
            } else {
                logger.debug("Setting Recording {} to state {}.", id, state);
                r.setState(state);
                sendRecordingUpdate(r);
            }
            assetManager.setProperty(p.recordingStatus().mk(id, r.getState()));
            assetManager.setProperty(p.recordingLastHeard().mk(id, r.getLastCheckinTime()));
            return true;
        } else {
            Recording r = new RecordingImpl(id, state);
            assetManager.setProperty(p.recordingStatus().mk(id, r.getState()));
            assetManager.setProperty(p.recordingLastHeard().mk(id, r.getLastCheckinTime()));
            sendRecordingUpdate(r);
            return true;
        }
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Failed to update recording status of event with mediapackage '{}': {}", id, getStackTrace(e));
        throw new SchedulerException(e);
    }
}
Also used : SchedulerException(org.opencastproject.scheduler.api.SchedulerException) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) NotFoundException(org.opencastproject.util.NotFoundException) RecordingImpl(org.opencastproject.scheduler.api.RecordingImpl) Log.getHumanReadableTimeString(org.opencastproject.util.Log.getHumanReadableTimeString) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) IOException(java.io.IOException) ServiceException(org.osgi.framework.ServiceException) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SeriesException(org.opencastproject.series.api.SeriesException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ValidationException(net.fortuna.ical4j.model.ValidationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) ARecord(org.opencastproject.assetmanager.api.query.ARecord) AResult(org.opencastproject.assetmanager.api.query.AResult) Recording(org.opencastproject.scheduler.api.Recording)

Example 13 with SchedulerException

use of org.opencastproject.scheduler.api.SchedulerException in project opencast by opencast.

the class SchedulerServiceImpl method getScheduleLastModified.

@Override
public String getScheduleLastModified(String captureAgentId) throws SchedulerException {
    notEmpty(captureAgentId, "captureAgentId");
    try {
        String lastModified = lastModifiedCache.getIfPresent(captureAgentId);
        if (lastModified != null)
            return lastModified;
        populateLastModifiedCache();
        lastModified = lastModifiedCache.getIfPresent(captureAgentId);
        // If still null set the empty calendar ETag
        if (lastModified == null) {
            lastModified = EMPTY_CALENDAR_ETAG;
            lastModifiedCache.put(captureAgentId, lastModified);
        }
        return lastModified;
    } catch (Exception e) {
        logger.error("Failed to get last modified of agent with id '{}': {}", captureAgentId, getStackTrace(e));
        throw new SchedulerException(e);
    }
}
Also used : SchedulerException(org.opencastproject.scheduler.api.SchedulerException) Log.getHumanReadableTimeString(org.opencastproject.util.Log.getHumanReadableTimeString) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) IOException(java.io.IOException) ServiceException(org.osgi.framework.ServiceException) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SeriesException(org.opencastproject.series.api.SeriesException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ValidationException(net.fortuna.ical4j.model.ValidationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 14 with SchedulerException

use of org.opencastproject.scheduler.api.SchedulerException in project opencast by opencast.

the class SchedulerServiceImpl method createTransaction.

@Override
public synchronized SchedulerTransaction createTransaction(String schedulingSource) throws SchedulerException {
    logger.debug(format("Transaction for source %s | create", schedulingSource));
    try {
        boolean hasTransaction = persistence.hasTransaction(schedulingSource);
        if (hasTransaction)
            throw new SchedulerConflictException("Transaction already exists");
        SchedulerTransaction transaction = new SchedulerTransactionImpl(schedulingSource);
        persistence.storeTransaction(transaction.getId(), schedulingSource);
        logger.info(format("Transaction for source %s | created | id=%s", transaction.getId(), schedulingSource));
        return transaction;
    } catch (SchedulerServiceDatabaseException e) {
        logger.error(format("Transaction for source %s | error | %s", schedulingSource, getStackTrace(e)));
        throw new SchedulerException(e);
    }
}
Also used : SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException)

Example 15 with SchedulerException

use of org.opencastproject.scheduler.api.SchedulerException in project opencast by opencast.

the class SchedulerServiceImpl method getDublinCore.

@Override
public DublinCoreCatalog getDublinCore(String mediaPackageId) throws NotFoundException, SchedulerException {
    notEmpty(mediaPackageId, "mediaPackageId");
    try {
        AQueryBuilder query = assetManager.createQuery();
        Props p = new Props(query);
        AResult result = query.select(query.snapshot()).where(withOrganization(query).and(query.mediaPackageId(mediaPackageId)).and(withVersion(query)).and(query.hasPropertiesOf(p.namespace()))).run();
        Opt<ARecord> record = result.getRecords().head();
        if (record.isNone())
            throw new NotFoundException();
        Opt<DublinCoreCatalog> dublinCore = loadEpisodeDublinCoreFromAsset(record.get().getSnapshot().get());
        if (dublinCore.isNone())
            throw new NotFoundException("No dublincore catalog found " + mediaPackageId);
        return dublinCore.get();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Failed to get dublin core catalog of event '{}': {}", mediaPackageId, getStackTrace(e));
        throw new SchedulerException(e);
    }
}
Also used : ARecord(org.opencastproject.assetmanager.api.query.ARecord) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) AResult(org.opencastproject.assetmanager.api.query.AResult) NotFoundException(org.opencastproject.util.NotFoundException) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) IOException(java.io.IOException) ServiceException(org.osgi.framework.ServiceException) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SeriesException(org.opencastproject.series.api.SeriesException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ValidationException(net.fortuna.ical4j.model.ValidationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException)

Aggregations

SchedulerException (org.opencastproject.scheduler.api.SchedulerException)83 NotFoundException (org.opencastproject.util.NotFoundException)76 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)68 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)62 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)60 HttpResponse (org.apache.http.HttpResponse)32 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)30 IOException (java.io.IOException)29 SeriesException (org.opencastproject.series.api.SeriesException)27 ValidationException (net.fortuna.ical4j.model.ValidationException)26 ServiceException (org.osgi.framework.ServiceException)26 ConfigurationException (org.osgi.service.cm.ConfigurationException)26 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)22 MediaPackage (org.opencastproject.mediapackage.MediaPackage)22 Date (java.util.Date)21 HttpGet (org.apache.http.client.methods.HttpGet)19 ARecord (org.opencastproject.assetmanager.api.query.ARecord)19 AResult (org.opencastproject.assetmanager.api.query.AResult)19 ArrayList (java.util.ArrayList)16 Log.getHumanReadableTimeString (org.opencastproject.util.Log.getHumanReadableTimeString)16