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);
}
}
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;
}
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));
}
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));
}
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());
}
Aggregations