Search in sources :

Example 1 with ScheduleInfo

use of com.emc.vipr.model.catalog.ScheduleInfo in project coprhd-controller by CoprHD.

the class OrderExecution method createScheduledOrder.

protected static ScheduledEventCreateParam createScheduledOrder(OrderCreateParam orderParam) {
    if (!isSchedulerEnabled()) {
        return null;
    }
    ScheduleInfo scheduleInfo = new ScheduleInfo();
    String cycleFrequency = params.get("scheduler.cycleFrequency");
    if (cycleFrequency != null) {
        scheduleInfo.setCycleFrequency(Integer.parseInt(cycleFrequency));
    } else {
        scheduleInfo.setCycleFrequency(1);
    }
    String cycleType = params.get("scheduler.cycleType");
    if (cycleType != null) {
        ScheduleCycleType cycleTypeEnum = ScheduleCycleType.valueOf(cycleType);
        scheduleInfo.setCycleType(cycleTypeEnum);
        List<String> sectionsInCycleList = Lists.newArrayList();
        if (cycleTypeEnum == ScheduleCycleType.WEEKLY) {
            String sectionsInCycle = params.get("scheduler.dayOfWeek");
            sectionsInCycleList.add(sectionsInCycle);
        } else if (cycleTypeEnum == ScheduleCycleType.MONTHLY) {
            String sectionsInCycle = params.get("scheduler.dayOfMonth");
            sectionsInCycleList.add(sectionsInCycle);
        }
        scheduleInfo.setSectionsInCycle(sectionsInCycleList);
    } else {
        scheduleInfo.setCycleType(ScheduleCycleType.DAILY);
    }
    String currentTimezoneOffsetInMins = params.get("scheduler.currentTimezoneOffsetInMins");
    Integer timezoneOffset = Integer.parseInt(currentTimezoneOffsetInMins);
    String startDate = params.get("scheduler.startDate");
    String startTime = params.get("scheduler.startTime");
    String isoDateTimeStr = String.format("%sT%s", startDate, startTime);
    DateTime startDateTime = DateTime.parse(isoDateTimeStr, ISODateTimeFormat.localDateOptionalTimeParser().withZone(TimeUtils.getLocalTimeZone(timezoneOffset)));
    startDateTime = startDateTime.withZone(DateTimeZone.UTC);
    scheduleInfo.setHourOfDay(startDateTime.getHourOfDay());
    scheduleInfo.setMinuteOfHour(startDateTime.getMinuteOfHour());
    scheduleInfo.setStartDate(String.format("%d-%02d-%02d", startDateTime.getYear(), startDateTime.getMonthOfYear(), startDateTime.getDayOfMonth()));
    String recurrence = params.get("scheduler.recurrence");
    int recurrenceNum = 1;
    if (recurrence != null) {
        recurrenceNum = Integer.parseInt(recurrence);
        if (recurrenceNum == -1) {
            String range = params.get("scheduler.rangeOfRecurrence");
            recurrenceNum = Integer.parseInt(range);
        }
    }
    scheduleInfo.setReoccurrence(recurrenceNum);
    /*
         * if reoccurence number large than 1, we must make sure the name contains patten {datetime},
         * with the pattern in the name, vipr know how to generate dynamic name for each snaphot/fullcopy.
         */
    if (recurrenceNum != 1) {
        List<Parameter> parameters = orderParam.getParameters();
        CatalogServiceRestRep service = CatalogServiceUtils.getCatalogService(orderParam.getCatalogService());
        Logger.info("creating order with parameter for: " + service.getBaseService());
        String nameToValidate = SCHEDULED_SERVICE.get(service.getBaseService());
        for (Parameter parameter : parameters) {
            if (parameter.getLabel().equals(nameToValidate) && !parameter.getValue().contains("{datetime}")) {
                Validation.addError(nameToValidate, "need to add patten '{datetime}' in the name for reoccuring scheduled operation");
            }
            Logger.info(parameter.getLabel() + " = " + parameter.getValue() + ", " + parameter.getFriendlyLabel() + " = " + parameter.getFriendlyValue());
        }
    }
    String maxNumOfCopies = params.get("scheduler.maxNumOfCopies");
    if (maxNumOfCopies != null) {
        orderParam.setAdditionalScheduleInfo(maxNumOfCopies);
    }
    scheduleInfo.setDurationLength(3600);
    ScheduledEventCreateParam eventParam = new ScheduledEventCreateParam();
    eventParam.setOrderCreateParam(orderParam);
    eventParam.setScheduleInfo(scheduleInfo);
    return eventParam;
}
Also used : Parameter(com.emc.vipr.model.catalog.Parameter) CatalogServiceRestRep(com.emc.vipr.model.catalog.CatalogServiceRestRep) ScheduledEventCreateParam(com.emc.vipr.model.catalog.ScheduledEventCreateParam) ScheduleInfo(com.emc.vipr.model.catalog.ScheduleInfo) DateTime(org.joda.time.DateTime) ScheduleCycleType(com.emc.vipr.model.catalog.ScheduleCycleType)

Example 2 with ScheduleInfo

use of com.emc.vipr.model.catalog.ScheduleInfo in project coprhd-controller by CoprHD.

the class OrderService method scheduleReoccurenceOrders.

private void scheduleReoccurenceOrders() throws Exception {
    lock = coordinatorClient.getLock(LOCK_NAME);
    try {
        lock.acquire();
        List<ScheduledEvent> scheduledEvents = dataManager.getAllReoccurrenceEvents();
        for (ScheduledEvent event : scheduledEvents) {
            if (event.getEventStatus() != ScheduledEventStatus.APPROVED) {
                log.debug("Skipping event {} which is not in APPROVED status.", event.getId());
                continue;
            }
            URI orderId = event.getLatestOrderId();
            Order order = getOrderById(orderId, false);
            if (!(OrderStatus.valueOf(order.getOrderStatus()).equals(OrderStatus.SUCCESS) || OrderStatus.valueOf(order.getOrderStatus()).equals(OrderStatus.PARTIAL_SUCCESS) || OrderStatus.valueOf(order.getOrderStatus()).equals(OrderStatus.ERROR) || OrderStatus.valueOf(order.getOrderStatus()).equals(OrderStatus.CANCELLED))) {
                log.debug("Skipping event {} whose latest order {} is not finished yet.", event.getId(), order.getId());
                continue;
            }
            log.info("Trying to schedule a new order for event {} : {}", event.getId(), ScheduleInfo.deserialize(org.apache.commons.codec.binary.Base64.decodeBase64(event.getScheduleInfo().getBytes(UTF_8))).toString());
            StorageOSUser user = StorageOSUser.deserialize(org.apache.commons.codec.binary.Base64.decodeBase64(event.getStorageOSUser().getBytes(UTF_8)));
            OrderCreateParam createParam = OrderCreateParam.deserialize(org.apache.commons.codec.binary.Base64.decodeBase64(event.getOrderCreationParam().getBytes(UTF_8)));
            ScheduleInfo scheduleInfo = ScheduleInfo.deserialize(org.apache.commons.codec.binary.Base64.decodeBase64(event.getScheduleInfo().getBytes(UTF_8)));
            Calendar nextScheduledTime = ScheduleTimeHelper.getNextScheduledTime(order.getScheduledTime(), scheduleInfo);
            int retry = 0;
            if (order.getExecutionWindowId() != null && !order.getExecutionWindowId().getURI().equals(ExecutionWindow.NEXT)) {
                ExecutionWindow window = client.executionWindows().findById(order.getExecutionWindowId().getURI());
                if (window != null) {
                    ExecutionWindowHelper helper = new ExecutionWindowHelper(window);
                    if (nextScheduledTime != null && !helper.isActive(nextScheduledTime)) {
                        log.warn("Execution window {} might be changed after the event is scheduled.", order.getExecutionWindowId().getURI());
                        log.warn("Otherwise it is a HOURLY scheduled event");
                        do {
                            nextScheduledTime = ScheduleTimeHelper.getNextScheduledTime(nextScheduledTime, scheduleInfo);
                            retry++;
                        } while (nextScheduledTime != null && !helper.isActive(nextScheduledTime) && retry < ScheduleTimeHelper.SCHEDULE_TIME_RETRY_THRESHOLD);
                        if (retry == ScheduleTimeHelper.SCHEDULE_TIME_RETRY_THRESHOLD) {
                            log.error("Failed to find next scheduled time that match with {}", order.getExecutionWindowId().getURI());
                            nextScheduledTime = null;
                        }
                    }
                } else {
                    log.error("Execution window {} does not exist.", order.getExecutionWindowId().getURI());
                }
            }
            if (nextScheduledTime == null) {
                log.info("Scheduled event {} should be set finished.", event.getId());
                event.setEventStatus(ScheduledEventStatus.FINISHED);
            } else {
                createParam.setScheduledTime(ScheduleTimeHelper.convertCalendarToStr(nextScheduledTime));
                order = createNewOrder(user, uri(order.getTenant()), createParam);
                orderManager.processOrder(order);
                event.setLatestOrderId(order.getId());
                log.info("Scheduled an new order {} for event {} ...", order.getId(), event.getId());
            }
            client.save(event);
        }
    } catch (Exception e) {
        log.error("Failed to schedule next orders", e);
    } finally {
        try {
            lock.release();
        } catch (Exception e) {
            log.error("Error releasing order scheduler lock", e);
        }
    }
}
Also used : Order(com.emc.storageos.db.client.model.uimodels.Order) ScheduledEvent(com.emc.storageos.db.client.model.uimodels.ScheduledEvent) ExecutionWindowHelper(com.emc.storageos.db.client.util.ExecutionWindowHelper) ExecutionWindow(com.emc.storageos.db.client.model.uimodels.ExecutionWindow) StorageOSUser(com.emc.storageos.security.authentication.StorageOSUser) Calendar(java.util.Calendar) OrderCreateParam(com.emc.vipr.model.catalog.OrderCreateParam) URI(java.net.URI) ScheduleInfo(com.emc.vipr.model.catalog.ScheduleInfo) TimeSeriesConstraint(com.emc.storageos.db.client.constraint.TimeSeriesConstraint) InvalidParameterException(java.security.InvalidParameterException) WebApplicationException(javax.ws.rs.WebApplicationException) URISyntaxException(java.net.URISyntaxException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException)

Aggregations

ScheduleInfo (com.emc.vipr.model.catalog.ScheduleInfo)2 TimeSeriesConstraint (com.emc.storageos.db.client.constraint.TimeSeriesConstraint)1 ExecutionWindow (com.emc.storageos.db.client.model.uimodels.ExecutionWindow)1 Order (com.emc.storageos.db.client.model.uimodels.Order)1 ScheduledEvent (com.emc.storageos.db.client.model.uimodels.ScheduledEvent)1 ExecutionWindowHelper (com.emc.storageos.db.client.util.ExecutionWindowHelper)1 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)1 StorageOSUser (com.emc.storageos.security.authentication.StorageOSUser)1 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)1 CatalogServiceRestRep (com.emc.vipr.model.catalog.CatalogServiceRestRep)1 OrderCreateParam (com.emc.vipr.model.catalog.OrderCreateParam)1 Parameter (com.emc.vipr.model.catalog.Parameter)1 ScheduleCycleType (com.emc.vipr.model.catalog.ScheduleCycleType)1 ScheduledEventCreateParam (com.emc.vipr.model.catalog.ScheduledEventCreateParam)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 InvalidParameterException (java.security.InvalidParameterException)1 Calendar (java.util.Calendar)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 DateTime (org.joda.time.DateTime)1