Search in sources :

Example 1 with ScheduleType

use of com.hubspot.singularity.ScheduleType in project Singularity by HubSpot.

the class ScheduleMigration method applyMigration.

@Override
public void applyMigration() {
    LOG.info("Starting migration to fix certain CRON schedules");
    final long start = System.currentTimeMillis();
    int num = 0;
    for (SingularityRequestWithState requestWithState : requestManager.getRequests()) {
        if (requestWithState.getRequest().isScheduled()) {
            Optional<String> schedule = requestWithState.getRequest().getSchedule();
            Optional<String> quartzSchedule = requestWithState.getRequest().getQuartzSchedule();
            Optional<ScheduleType> scheduleType = requestWithState.getRequest().getScheduleType();
            if (scheduleType.isPresent() && scheduleType.get() != ScheduleType.CRON) {
                LOG.info("Skipping {}, it had schedule type: {}", requestWithState.getRequest().getId(), scheduleType.get());
                continue;
            }
            if (quartzSchedule.isPresent() && schedule.isPresent() && quartzSchedule.get().equals(schedule.get())) {
                LOG.info("Skipping {}, assuming it was quartz - it had quartz schedule == schedule {}", requestWithState.getRequest().getId(), schedule.get());
                continue;
            }
            if (!schedule.isPresent()) {
                LOG.info("Skipping {}, it had no schedule", requestWithState.getRequest().getId());
                continue;
            }
            String actualSchedule = schedule.get();
            String newQuartzSchedule = null;
            try {
                newQuartzSchedule = validator.getQuartzScheduleFromCronSchedule(actualSchedule);
            } catch (WebApplicationException e) {
                LOG.error("Failed to convert {} ({}) due to {}", requestWithState.getRequest().getId(), actualSchedule, e.getResponse().getEntity());
                continue;
            }
            if (quartzSchedule.isPresent() && quartzSchedule.get().equals(newQuartzSchedule)) {
                LOG.info("Skipping {}, migration had no effect {}", requestWithState.getRequest().getId(), newQuartzSchedule);
                continue;
            }
            SingularityRequest newRequest = requestWithState.getRequest().toBuilder().setQuartzSchedule(Optional.of(newQuartzSchedule)).build();
            try {
                LOG.info("Saving new schedule (quartz {} - from {}) for {}", newQuartzSchedule, actualSchedule, newRequest.getId());
                curator.setData().forPath("/requests/all/" + newRequest.getId(), requestTranscoder.toBytes(new SingularityRequestWithState(newRequest, requestWithState.getState(), requestWithState.getTimestamp())));
                num++;
            } catch (Throwable t) {
                LOG.error("Failed to write {}", newRequest.getId(), t);
                throw Throwables.propagate(t);
            }
        }
    }
    LOG.info("Applied {} in {}", num, JavaUtils.duration(start));
}
Also used : ScheduleType(com.hubspot.singularity.ScheduleType) WebApplicationException(javax.ws.rs.WebApplicationException) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest)

Aggregations

ScheduleType (com.hubspot.singularity.ScheduleType)1 SingularityRequest (com.hubspot.singularity.SingularityRequest)1 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)1 WebApplicationException (javax.ws.rs.WebApplicationException)1