Search in sources :

Example 1 with VmSchedule

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

the class VmScheduleResource method afterPropertiesSet.

@Override
@Transactional
public void afterPropertiesSet() throws SchedulerException {
    // Recreate all schedules from the database
    final List<VmSchedule> schedules = repository.findAll();
    log.info("Schedules {} jobs from database", schedules.size());
    for (final VmSchedule schedule : schedules) {
        persistTrigger(schedule);
    }
}
Also used : VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) Transactional(javax.transaction.Transactional)

Example 2 with VmSchedule

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

the class VmScheduleResource method findAll.

/**
 * Return all schedules related to given subscription.
 *
 * @param subscription
 *            The subscription identifier.
 * @return All schedules related to given subscription.
 * @throws ParseException
 *             When CRON cannot be parsed.
 */
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public List<VmScheduleVo> findAll(final int subscription) throws ParseException {
    final List<VmScheduleVo> schedules = new ArrayList<>();
    final Date now = DateUtils.newCalendar().getTime();
    for (final VmSchedule schedule : repository.findBySubscription(subscription)) {
        // Copy basic attributes
        final VmScheduleVo vo = new VmScheduleVo();
        vo.setCron(schedule.getCron());
        vo.setOperation(schedule.getOperation());
        vo.setId(schedule.getId());
        vo.setNext(new CronExpression(schedule.getCron()).getNextValidTimeAfter(now));
        schedules.add(vo);
    }
    return schedules;
}
Also used : ArrayList(java.util.ArrayList) CronExpression(org.quartz.CronExpression) Date(java.util.Date) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule)

Example 3 with VmSchedule

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

the class VmResourceTest method downloadNodeSchedulesReport.

@Test
public void downloadNodeSchedulesReport() throws Exception {
    final VmResource resource = new VmResource();
    applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    resource.locator = mockServicePluginLocator;
    final AtomicReference<VmOperation> operation = new AtomicReference<>(null);
    // The third call is skipped
    Mockito.doNothing().when(mockVmTool).execute(ArgumentMatchers.argThat(new ArgumentMatcher<VmExecution>() {

        @Override
        public boolean matches(final VmExecution argument) {
            argument.setOperation(operation.get());
            return true;
        }
    }));
    // Report without executions
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadNodeSchedulesReport("service:vm:test", "file1").getEntity()).write(output);
    List<String> lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(2, lines.size());
    Assertions.assertEquals("subscription;project;projectKey;projectName;node;cron;operation;lastDateHMS;lastTimestamp;lastOperation;vm;lastTrigger;lastSucceed;lastStatusText;lastErrorText;nextDateHMS;nextTimestamp", lines.get(0));
    // No last execution available
    Assertions.assertTrue(lines.get(1).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;0 0 0 1 1 \\? 2050;OFF;;;;;;;;;2050/01/01 00:00:00;2524604400000"), "Was : " + lines.get(1));
    output.close();
    // Manual execution
    operation.set(VmOperation.OFF);
    Integer id = resource.execute(subscription, VmOperation.OFF);
    vmExecutionRepository.findOneExpected(id).setStatusText("status1");
    // Manual execution by schedule, by pass the security check
    securityHelper.setUserName(SecurityHelper.SYSTEM_USERNAME);
    final Subscription entity = subscriptionRepository.findOneExpected(subscription);
    operation.set(VmOperation.SHUTDOWN);
    id = resource.execute(entity, VmOperation.ON);
    vmExecutionRepository.findOneExpected(id).setVm("vm1");
    // This call will be skipped
    operation.set(null);
    Assertions.assertNull(resource.execute(entity, VmOperation.REBOOT));
    // Restore the current user
    initSpringSecurityContext(getAuthenticationName());
    // Report contains only the last executions (OFF/SHUTDOWN/[REBOOT = skipped])
    output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadNodeSchedulesReport("service:vm:test", "file1").getEntity()).write(output);
    lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(2, lines.size());
    Assertions.assertTrue(lines.get(1).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;0 0 0 1 1 \\? 2050;OFF;.+;.+;SHUTDOWN;vm1;_system;true;;;2050/01/01 00:00:00;2524604400000"), "Was : " + lines.get(1));
    // Next execution where schedule CRON has been updated
    vmScheduleRepository.findBy("subscription.id", subscription).setCron("INVALID");
    operation.set(VmOperation.SHUTDOWN);
    id = resource.execute(entity, VmOperation.ON);
    vmExecutionRepository.findOneExpected(id).setVm("vm1");
    output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadNodeSchedulesReport("service:vm:test", "file1").getEntity()).write(output);
    lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(2, lines.size());
    Assertions.assertTrue(lines.get(1).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;INVALID;OFF;.+;.+;SHUTDOWN;vm1;fdaugan;true;;;ERROR;ERROR"));
    // Add another schedule to the same subscription, with an execution
    final VmSchedule schedule = new VmSchedule();
    schedule.setOperation(VmOperation.ON);
    schedule.setCron("0 0 0 1 1 ? 2049");
    schedule.setSubscription(entity);
    vmScheduleRepository.saveAndFlush(schedule);
    final VmExecution execution = new VmExecution();
    execution.setDate(new Date());
    execution.setSubscription(entity);
    execution.setTrigger("_system");
    execution.setOperation(VmOperation.ON);
    execution.setSucceed(true);
    vmExecutionRepository.saveAndFlush(execution);
    output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadNodeSchedulesReport("service:vm:test:test", "file1").getEntity()).write(output);
    lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(3, lines.size());
    Assertions.assertTrue(lines.get(2).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;0 0 0 1 1 \\? 2049;ON;.+;.+;ON;;_system;true;;;2049/01/01 00:00:00;2493068400000"), "Was : " + lines.get(2));
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) AtomicReference(java.util.concurrent.atomic.AtomicReference) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) ByteArrayInputStream(java.io.ByteArrayInputStream) VmOperation(org.ligoj.app.plugin.vm.model.VmOperation) ArgumentMatcher(org.mockito.ArgumentMatcher) Subscription(org.ligoj.app.model.Subscription) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 4 with VmSchedule

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

the class VmJobTest method format.

@Test
public void format() {
    final VmSchedule vmSchedule = new VmSchedule();
    vmSchedule.setId(6789);
    final Subscription subscription = new Subscription();
    subscription.setId(12345);
    vmSchedule.setSubscription(subscription);
    Assertions.assertEquals("6789-12345", VmJob.format(vmSchedule));
}
Also used : Subscription(org.ligoj.app.model.Subscription) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) Test(org.junit.jupiter.api.Test)

Example 5 with VmSchedule

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

the class VmJob method executeInternal.

@Override
protected void executeInternal(final JobExecutionContext arg0) {
    // Extract the job data to execute the operation
    final int schedule = arg0.getMergedJobDataMap().getInt("schedule");
    final ApplicationContext context = ObjectUtils.defaultIfNull((ApplicationContext) arg0.getMergedJobDataMap().get("context"), SpringUtils.getApplicationContext());
    final VmSchedule entity = context.getBean(VmScheduleRepository.class).findOneExpected(schedule);
    log.info("Executing {} for schedule {}, subscription {}", entity.getOperation(), entity.getId(), entity.getSubscription().getId());
    // Set the user
    context.getBean(SecurityHelper.class).setUserName(SecurityHelper.SYSTEM_USERNAME);
    // Execute the operation
    context.getBean(VmResource.class).execute(entity.getSubscription(), entity.getOperation());
    log.info("Succeed {} for schedule {}, subscription {}", entity.getOperation(), entity.getId(), entity.getSubscription().getId());
}
Also used : VmResource(org.ligoj.app.plugin.vm.VmResource) ApplicationContext(org.springframework.context.ApplicationContext) VmScheduleRepository(org.ligoj.app.plugin.vm.dao.VmScheduleRepository) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper)

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