Search in sources :

Example 1 with StdScheduler

use of org.quartz.impl.StdScheduler in project plugin-vm by ligoj.

the class VmScheduleResourceTest method createAndUpdateSchedule.

@Test
public void createAndUpdateSchedule() throws Exception {
    final ApplicationContext mockContext = Mockito.mock(ApplicationContext.class);
    final VmScheduleRepository repository = Mockito.mock(VmScheduleRepository.class);
    final VmResource mockResource = Mockito.mock(VmResource.class);
    final Subscription entity = this.subscriptionRepository.findOneExpected(subscription);
    Mockito.when(mockContext.getBean(VmScheduleRepository.class)).thenReturn(repository);
    Mockito.when(mockContext.getBean(SecurityHelper.class)).thenReturn(Mockito.mock(SecurityHelper.class));
    Mockito.when(mockContext.getBean(VmResource.class)).thenReturn(mockResource);
    final StdScheduler scheduler = (StdScheduler) vmSchedulerFactoryBean.getScheduler();
    final QuartzScheduler qscheduler = (QuartzScheduler) FieldUtils.getField(StdScheduler.class, "sched", true).get(scheduler);
    final QuartzSchedulerResources resources = (QuartzSchedulerResources) FieldUtils.getField(QuartzScheduler.class, "resources", true).get(qscheduler);
    final JobDetail jobDetail = scheduler.getJobDetail(scheduler.getJobKeys(GroupMatcher.anyJobGroup()).iterator().next());
    // "ON" call would fail
    Mockito.doThrow(new RuntimeException()).when(mockResource).execute(entity, VmOperation.ON);
    try {
        // Mock the factory
        jobDetail.getJobDataMap().put("context", mockContext);
        ((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true);
        Assertions.assertEquals(1, this.repository.findAll().size());
        // Schedule all operations within the next 2 seconds
        final String cron = "" + ((DateUtils.newCalendar().get(Calendar.SECOND) + 2) % 60) + " * * * * ?";
        final int id = mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.OFF)));
        mockSchedule(repository, resource.create(subscription, newSchedule(cron + " *", VmOperation.ON)));
        Assertions.assertEquals(3, this.repository.findAll().size());
        // Yield for the schedules
        Thread.sleep(2500);
        // Check the executions
        Mockito.verify(mockResource).execute(entity, VmOperation.OFF);
        // Failed
        Mockito.verify(mockResource).execute(entity, VmOperation.ON);
        Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.REBOOT);
        Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.RESET);
        Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SHUTDOWN);
        Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SUSPEND);
        // Update the CRON and the operation
        final VmScheduleVo vo = newSchedule("" + ((DateUtils.newCalendar().get(Calendar.SECOND) + 2) % 60) + " * * * * ?", VmOperation.SHUTDOWN);
        vo.setId(id);
        resource.update(subscription, vo);
        Assertions.assertEquals(3, this.repository.findAll().size());
        // Yield for the schedules
        Thread.sleep(2500);
        Mockito.verify(mockResource).execute(entity, VmOperation.SHUTDOWN);
    } finally {
        // Restore the factory's context
        jobDetail.getJobDataMap().put("context", applicationContext);
        ((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true);
    }
}
Also used : QuartzScheduler(org.quartz.core.QuartzScheduler) QuartzSchedulerResources(org.quartz.core.QuartzSchedulerResources) RAMJobStore(org.quartz.simpl.RAMJobStore) VmResource(org.ligoj.app.plugin.vm.VmResource) ApplicationContext(org.springframework.context.ApplicationContext) JobDetail(org.quartz.JobDetail) VmScheduleRepository(org.ligoj.app.plugin.vm.dao.VmScheduleRepository) Subscription(org.ligoj.app.model.Subscription) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) StdScheduler(org.quartz.impl.StdScheduler) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 2 with StdScheduler

use of org.quartz.impl.StdScheduler in project plugin-vm by ligoj.

the class VmScheduleResourceTest method unscheduleAll.

@Test
public void unscheduleAll() throws Exception {
    Assertions.assertEquals(1, repository.findAll().size());
    repository.deleteAll();
    Assertions.assertEquals(0, repository.findAll().size());
    final Subscription entity = this.subscriptionRepository.findOneExpected(subscription);
    final ApplicationContext mockContext = Mockito.mock(ApplicationContext.class);
    final VmScheduleRepository repository = Mockito.mock(VmScheduleRepository.class);
    final VmResource mockResource = Mockito.mock(VmResource.class);
    Mockito.when(mockContext.getBean(VmScheduleRepository.class)).thenReturn(repository);
    Mockito.when(mockContext.getBean(SecurityHelper.class)).thenReturn(Mockito.mock(SecurityHelper.class));
    Mockito.when(mockContext.getBean(VmResource.class)).thenReturn(mockResource);
    final StdScheduler scheduler = (StdScheduler) vmSchedulerFactoryBean.getScheduler();
    final QuartzScheduler qscheduler = (QuartzScheduler) FieldUtils.getField(StdScheduler.class, "sched", true).get(scheduler);
    final QuartzSchedulerResources resources = (QuartzSchedulerResources) FieldUtils.getField(QuartzScheduler.class, "resources", true).get(qscheduler);
    final JobDetail jobDetail = scheduler.getJobDetail(scheduler.getJobKeys(GroupMatcher.anyJobGroup()).iterator().next());
    // One call would fail
    Mockito.doThrow(new RuntimeException()).when(mockResource).execute(entity, VmOperation.ON);
    final Subscription otherEntity = new Subscription();
    try {
        // Mock the factory
        jobDetail.getJobDataMap().put("context", mockContext);
        ((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true);
        // Schedule all operations within the next 2 seconds
        final String cron = "" + ((DateUtils.newCalendar().get(Calendar.SECOND) + 2) % 60) + " * * * * ? *";
        mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
        mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
        mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
        mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
        mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
        Assertions.assertEquals(5, this.repository.findAll().size());
        // Persist another VM schedule for another subscription within the
        // next 2 seconds
        otherEntity.setProject(entity.getProject());
        otherEntity.setNode(entity.getNode());
        this.subscriptionRepository.saveAndFlush(otherEntity);
        final VmScheduleVo schedule2 = newSchedule("0 0 0 1 1 ? 2050", VmOperation.ON);
        resource.create(otherEntity.getId(), schedule2);
        Assertions.assertEquals(6, this.repository.findAll().size());
        // Yield for the schedules
        Thread.sleep(2500);
    } finally {
        // Restore the factory's context
        jobDetail.getJobDataMap().put("context", applicationContext);
        ((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true);
    }
    Mockito.inOrder(mockResource).verify(mockResource, Mockito.calls(5)).execute(entity, VmOperation.ON);
    Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.OFF);
    Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.REBOOT);
    Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.RESET);
    Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SHUTDOWN);
    Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SUSPEND);
    // Remove all triggers of the subscription
    resource.unscheduleAll(subscription);
    Assertions.assertEquals(1, this.repository.findAll().size());
    resource.unscheduleAll(otherEntity.getId());
    Assertions.assertEquals(0, this.repository.findAll().size());
}
Also used : QuartzScheduler(org.quartz.core.QuartzScheduler) QuartzSchedulerResources(org.quartz.core.QuartzSchedulerResources) RAMJobStore(org.quartz.simpl.RAMJobStore) VmResource(org.ligoj.app.plugin.vm.VmResource) ApplicationContext(org.springframework.context.ApplicationContext) JobDetail(org.quartz.JobDetail) VmScheduleRepository(org.ligoj.app.plugin.vm.dao.VmScheduleRepository) Subscription(org.ligoj.app.model.Subscription) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) StdScheduler(org.quartz.impl.StdScheduler) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 3 with StdScheduler

use of org.quartz.impl.StdScheduler in project OpenClinica by OpenClinica.

the class XsltTransformJob method postErrorMessage.

private void postErrorMessage(String message, JobExecutionContext context) {
    String SCHEDULER = "schedulerFactoryBean";
    try {
        ApplicationContext appContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
        StdScheduler scheduler = (StdScheduler) appContext.getBean(SCHEDULER);
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap = jobDetail.getJobDataMap();
        dataMap.put("failMessage", message);
        jobDetail.setJobDataMap(dataMap);
        // replace the job with the extra data
        scheduler.addJob(jobDetail, true);
    } catch (SchedulerException e) {
        throw new IllegalStateException("Error processing post error message", e);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) StdScheduler(org.quartz.impl.StdScheduler)

Example 4 with StdScheduler

use of org.quartz.impl.StdScheduler in project OpenClinica by OpenClinica.

the class XsltTransformJob method postSuccessMessage.

private void postSuccessMessage(String message, JobExecutionContext context) {
    String SCHEDULER = "schedulerFactoryBean";
    try {
        ApplicationContext appContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
        StdScheduler scheduler = (StdScheduler) appContext.getBean(SCHEDULER);
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap = jobDetail.getJobDataMap();
        dataMap.put("successMsg", message);
        jobDetail.setJobDataMap(dataMap);
        // replace the job with the extra data
        scheduler.addJob(jobDetail, true);
    } catch (SchedulerException e) {
        throw new IllegalStateException("Error processing post success message", e);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) StdScheduler(org.quartz.impl.StdScheduler)

Aggregations

JobDetail (org.quartz.JobDetail)4 StdScheduler (org.quartz.impl.StdScheduler)4 ApplicationContext (org.springframework.context.ApplicationContext)4 Test (org.junit.jupiter.api.Test)2 AbstractServerTest (org.ligoj.app.AbstractServerTest)2 Subscription (org.ligoj.app.model.Subscription)2 VmResource (org.ligoj.app.plugin.vm.VmResource)2 VmScheduleRepository (org.ligoj.app.plugin.vm.dao.VmScheduleRepository)2 SecurityHelper (org.ligoj.bootstrap.core.security.SecurityHelper)2 JobDataMap (org.quartz.JobDataMap)2 SchedulerException (org.quartz.SchedulerException)2 QuartzScheduler (org.quartz.core.QuartzScheduler)2 QuartzSchedulerResources (org.quartz.core.QuartzSchedulerResources)2 RAMJobStore (org.quartz.simpl.RAMJobStore)2