Search in sources :

Example 1 with HarvestTimerInfo

use of edu.harvard.iq.dataverse.harvest.client.HarvestTimerInfo in project dataverse by IQSS.

the class DataverseTimerServiceBean method createHarvestTimer.

public void createHarvestTimer(HarvestingClient harvestingClient) {
    if (harvestingClient.isScheduled()) {
        long intervalDuration = 0;
        Calendar initExpiration = Calendar.getInstance();
        initExpiration.set(Calendar.MINUTE, 0);
        initExpiration.set(Calendar.SECOND, 0);
        if (harvestingClient.getSchedulePeriod().equals(HarvestingClient.SCHEDULE_PERIOD_DAILY)) {
            intervalDuration = 1000 * 60 * 60 * 24;
            initExpiration.set(Calendar.HOUR_OF_DAY, harvestingClient.getScheduleHourOfDay());
        } else if (harvestingClient.getSchedulePeriod().equals(harvestingClient.SCHEDULE_PERIOD_WEEKLY)) {
            intervalDuration = 1000 * 60 * 60 * 24 * 7;
            initExpiration.set(Calendar.HOUR_OF_DAY, harvestingClient.getScheduleHourOfDay());
            // (saved as zero-based array but Calendar is one-based.)
            initExpiration.set(Calendar.DAY_OF_WEEK, harvestingClient.getScheduleDayOfWeek() + 1);
        } else {
            logger.log(Level.WARNING, "Could not set timer for harvesting client id=" + harvestingClient.getId() + ", unknown schedule period: " + harvestingClient.getSchedulePeriod());
            return;
        }
        Date initExpirationDate = initExpiration.getTime();
        Date currTime = new Date();
        if (initExpirationDate.before(currTime)) {
            initExpirationDate.setTime(initExpiration.getTimeInMillis() + intervalDuration);
        }
        logger.log(Level.INFO, "Setting timer for harvesting client " + harvestingClient.getName() + ", initial expiration: " + initExpirationDate);
        createTimer(initExpirationDate, intervalDuration, new HarvestTimerInfo(harvestingClient.getId(), harvestingClient.getName(), harvestingClient.getSchedulePeriod(), harvestingClient.getScheduleHourOfDay(), harvestingClient.getScheduleDayOfWeek()));
    }
}
Also used : Calendar(java.util.Calendar) HarvestTimerInfo(edu.harvard.iq.dataverse.harvest.client.HarvestTimerInfo) Date(java.util.Date)

Example 2 with HarvestTimerInfo

use of edu.harvard.iq.dataverse.harvest.client.HarvestTimerInfo in project dataverse by IQSS.

the class DataverseTimerServiceBean method removeHarvestTimers.

public void removeHarvestTimers() {
    // Remove all the harvest timers, if exist:
    // 
    // (the logging messages below are set to level INFO; it's ok,
    // since this code is only called on startup of the application,
    // and it may be useful to know what existing timers were encountered).
    logger.log(Level.INFO, "Removing existing harvest timers..");
    int i = 1;
    for (Iterator it = timerService.getTimers().iterator(); it.hasNext(); ) {
        Timer timer = (Timer) it.next();
        logger.log(Level.INFO, "HarvesterService: checking timer " + i);
        if (timer.getInfo() instanceof HarvestTimerInfo) {
            logger.log(Level.INFO, "HarvesterService: timer " + i + " is a harvesting one; removing.");
            timer.cancel();
        }
        i++;
    }
}
Also used : Timer(javax.ejb.Timer) Iterator(java.util.Iterator) HarvestTimerInfo(edu.harvard.iq.dataverse.harvest.client.HarvestTimerInfo)

Example 3 with HarvestTimerInfo

use of edu.harvard.iq.dataverse.harvest.client.HarvestTimerInfo in project dataverse by IQSS.

the class DataverseTimerServiceBean method handleTimeout.

/**
 * This method is called whenever an EJB Timer goes off.
 * Check to see if this is a Harvest Timer, and if it is
 * Run the harvest for the given (scheduled) dataverse
 * @param timer
 */
@Timeout
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void handleTimeout(javax.ejb.Timer timer) {
    if (!systemConfig.isTimerServer()) {
        // logger.info("I am not the timer server! - bailing out of handleTimeout()");
        Logger.getLogger(DataverseTimerServiceBean.class.getName()).log(Level.WARNING, null, "I am not the timer server! - but handleTimeout() got called. Please investigate!");
    }
    try {
        logger.log(Level.INFO, "Handling timeout on " + InetAddress.getLocalHost().getCanonicalHostName());
    } catch (UnknownHostException ex) {
        Logger.getLogger(DataverseTimerServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (timer.getInfo() instanceof MotherTimerInfo) {
        logger.info("Behold! I am the Master Timer, king of all timers! I'm here to create all the lesser timers!");
        removeHarvestTimers();
        for (HarvestingClient client : harvestingClientService.getAllHarvestingClients()) {
            createHarvestTimer(client);
        }
    } else if (timer.getInfo() instanceof HarvestTimerInfo) {
        HarvestTimerInfo info = (HarvestTimerInfo) timer.getInfo();
        try {
            logger.log(Level.INFO, "running a harvesting client: id=" + info.getHarvestingClientId());
            // Timer batch jobs are run by the main Admin user.
            // TODO: revisit how we retrieve the superuser here.
            // Should it be configurable somewhere, which superuser
            // runs these jobs? Should there be a central mechanism for obtaining
            // the "major", builtin superuser for this Dataverse instance?
            // -- L.A. 4.5, Aug. 2016
            // getAuthenticatedUser("admin");
            AuthenticatedUser adminUser = authSvc.getAdminUser();
            if (adminUser == null) {
                logger.info("Scheduled harvest: failed to locate the admin user! Exiting.");
                throw new IOException("Scheduled harvest: failed to locate the admin user");
            }
            logger.info("found admin user " + adminUser.getName());
            DataverseRequest dataverseRequest = new DataverseRequest(adminUser, (HttpServletRequest) null);
            harvesterService.doHarvest(dataverseRequest, info.getHarvestingClientId());
        } catch (Throwable e) {
            // Harvester Service should be handling any error notifications,
            // if/when things go wrong.
            // (TODO: -- verify this logic; harvesterService may still be able
            // to throw an IOException, if it could not run the harvest at all,
            // or could not for whatever reason modify the database record...
            // in this case we should, probably, log the error and try to send
            // a mail notification. -- L.A. 4.4)
            // dataverseService.setHarvestResult(info.getHarvestingDataverseId(), harvesterService.HARVEST_RESULT_FAILED);
            // mailService.sendHarvestErrorNotification(dataverseService.find().getSystemEmail(), dataverseService.find().getName());
            logException(e, logger);
        }
    } else if (timer.getInfo() instanceof ExportTimerInfo) {
        try {
            ExportTimerInfo info = (ExportTimerInfo) timer.getInfo();
            logger.info("Timer Service: Running a scheduled export job.");
            // try to export all unexported datasets:
            datasetService.exportAll();
            // and update all oai sets:
            oaiSetService.exportAllSets();
        } catch (Throwable e) {
            logException(e, logger);
        }
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient) HarvestTimerInfo(edu.harvard.iq.dataverse.harvest.client.HarvestTimerInfo) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) TransactionAttribute(javax.ejb.TransactionAttribute) Timeout(javax.ejb.Timeout)

Aggregations

HarvestTimerInfo (edu.harvard.iq.dataverse.harvest.client.HarvestTimerInfo)3 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)1 HarvestingClient (edu.harvard.iq.dataverse.harvest.client.HarvestingClient)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Timeout (javax.ejb.Timeout)1 Timer (javax.ejb.Timer)1 TransactionAttribute (javax.ejb.TransactionAttribute)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1