Search in sources :

Example 6 with VmSchedule

use of org.ligoj.app.plugin.vm.model.VmSchedule in project plugin-vm by ligoj.

the class VmScheduleResource method update.

/**
 * Update an existing schedule.
 *
 * @param subscription
 *            The target subscription. The subscription cannot be changed for a schedule.
 * @param schedule
 *            The schedule to save or update. The CRON expression may be either in the 5 either in 6 parts. The
 *            optional 6th corresponds to the "seconds" and will be prepended to the expression to conform to Quartz
 *            format.
 * @throws SchedulerException
 *             When the schedule cannot be done by Quartz.
 */
@PUT
@Transactional
public void update(@PathParam("subscription") final int subscription, final VmScheduleVo schedule) throws SchedulerException {
    // Check the schedule is related to the subscription
    final VmSchedule entity = checkOwnership(subscription, schedule.getId());
    // Persist the new schedules for each provided CRON
    checkAndSave(subscription, schedule, entity);
    // Remove current schedules from the Quartz memory
    unscheduleQuartz(schedule.getId());
    persistTrigger(checkAndSave(subscription, schedule, entity));
}
Also used : VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) PUT(javax.ws.rs.PUT) Transactional(javax.transaction.Transactional)

Example 7 with VmSchedule

use of org.ligoj.app.plugin.vm.model.VmSchedule in project plugin-vm by ligoj.

the class VmResource method writeSchedules.

/**
 * Write all schedules.
 */
private void writeSchedules(final OutputStream output, Collection<VmSchedule> schedules, final Map<Integer, VmExecution> lastExecutions) throws IOException {
    final Writer writer = new BufferedWriter(new OutputStreamWriter(output, "cp1252"));
    final FastDateFormat df = FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss");
    final Date now = DateUtils.newCalendar().getTime();
    writer.write("subscription;project;projectKey;projectName;node;cron;operation;lastDateHMS;lastTimestamp;lastOperation;vm;lastTrigger;lastSucceed;lastStatusText;lastErrorText;nextDateHMS;nextTimestamp");
    for (final VmSchedule schedule : schedules) {
        // The last execution of the related schedule
        final VmExecution execution = lastExecutions.get(schedule.getSubscription().getId());
        writeCommon(writer, schedule.getSubscription());
        writer.write(';');
        writer.write(schedule.getCron());
        writer.write(';');
        writer.write(schedule.getOperation().name());
        if (execution == null) {
            writer.write(";;;;;;;;");
        } else {
            // Last execution
            writeExecutionStatus(writer, execution, df);
        }
        // Next execution
        try {
            final Date next = new CronExpression(schedule.getCron()).getNextValidTimeAfter(now);
            writer.write(';');
            writer.write(df.format(next));
            writer.write(';');
            writer.write(String.valueOf(next.getTime()));
        } catch (final ParseException pe) {
            // Non blocking error
            log.error("Invalid CRON expression {}", schedule.getCron());
            writer.write(";ERROR;ERROR");
        }
    }
    // Ensure buffer is flushed
    writer.flush();
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) OutputStreamWriter(java.io.OutputStreamWriter) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) Date(java.util.Date) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) BufferedWriter(java.io.BufferedWriter)

Aggregations

VmSchedule (org.ligoj.app.plugin.vm.model.VmSchedule)7 Date (java.util.Date)3 Transactional (javax.transaction.Transactional)2 Test (org.junit.jupiter.api.Test)2 Subscription (org.ligoj.app.model.Subscription)2 VmExecution (org.ligoj.app.plugin.vm.model.VmExecution)2 CronExpression (org.quartz.CronExpression)2 BufferedWriter (java.io.BufferedWriter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 PUT (javax.ws.rs.PUT)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 FastDateFormat (org.apache.commons.lang3.time.FastDateFormat)1 AbstractServerTest (org.ligoj.app.AbstractServerTest)1 VmResource (org.ligoj.app.plugin.vm.VmResource)1