Search in sources :

Example 6 with CreateScheduledJobParams

use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.

the class SchedulerServiceActivatorTest method rescheduleFinishedRescheduleTask.

@Test
void rescheduleFinishedRescheduleTask() {
    final CreateScheduledJobParams jobParams = CreateScheduledJobParams.create().name(ScheduledJobName.from("name")).descriptor(DescriptorKey.from("appKey:descriptorName")).calendar(calendarService.cron("* * * * *", TimeZone.getDefault())).config(new PropertyTree()).build();
    mockNode(jobParams);
    when(schedulerConfig.jobs()).thenReturn(Set.of(jobParams));
    when(schedulerExecutorService.getAllFutures()).thenReturn(Set.of(RescheduleTask.NAME));
    final ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
    when(scheduledFuture.isDone()).thenReturn(true);
    final Optional<? extends ScheduledFuture<?>> optional = Optional.of(scheduledFuture);
    doReturn(optional).when(schedulerExecutorService).get(RescheduleTask.NAME);
    activator.activate(bundleContext);
    verify(schedulerExecutorService, times(1)).scheduleAtFixedRate(isA(SchedulableTask.class), anyLong(), anyLong(), isA(TimeUnit.class));
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) PropertyTree(com.enonic.xp.data.PropertyTree) SchedulableTask(com.enonic.xp.impl.scheduler.distributed.SchedulableTask) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test)

Example 7 with CreateScheduledJobParams

use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.

the class SchedulerConfigImpl method parseProperties.

private CreateScheduledJobParams parseProperties(final ScheduledJobName name, final Configuration properties) {
    final CreateScheduledJobParams.Builder job = CreateScheduledJobParams.create().name(name);
    TimeZone timeZone = null;
    String cronValue = null;
    for (final Map.Entry<String, String> entry : properties.asMap().entrySet()) {
        final String value = entry.getValue();
        final Matcher matcher = JOB_PROPERTY_PATTERN.matcher(entry.getKey());
        if (matcher.matches()) {
            final String propertyName = matcher.group("property");
            switch(propertyName) {
                case ScheduledJobPropertyNames.DESCRIPTION:
                    job.description(value);
                    break;
                case ScheduledJobPropertyNames.DESCRIPTOR:
                    job.descriptor(DescriptorKey.from(value));
                    break;
                case ScheduledJobPropertyNames.ENABLED:
                    job.enabled(Boolean.parseBoolean(value));
                    break;
                case ScheduledJobPropertyNames.USER:
                    job.user(PrincipalKey.from("user:" + value));
                    break;
                case ScheduledJobPropertyNames.CONFIG:
                    try {
                        job.config(treeMarshallerService.marshal(MAPPER.readValue(value, HashMap.class)));
                    } catch (JsonProcessingException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case ScheduledJobPropertyNames.CALENDAR_TIMEZONE:
                    timeZone = TimeZone.getTimeZone(value);
                    break;
                case "cron":
                    cronValue = value;
                    break;
                default:
                    throw new IllegalArgumentException(String.format("[%s] is invalid job property.", propertyName));
            }
        }
    }
    final ScheduleCalendar calendar = calendarService.cron(cronValue, timeZone != null ? timeZone : TimeZone.getDefault());
    return job.calendar(calendar).build();
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) TimeZone(java.util.TimeZone) Matcher(java.util.regex.Matcher) ScheduleCalendar(com.enonic.xp.scheduler.ScheduleCalendar) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 8 with CreateScheduledJobParams

use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.

the class SchedulerConfigImpl method jobs.

@Override
public Set<CreateScheduledJobParams> jobs() {
    final Configuration jobConfig = this.config.subConfig(JOB_PROPERTY_PREFIX);
    final Set<ScheduledJobName> jobNames = parseNames(jobConfig);
    return jobNames.stream().map(name -> parseProperties(name, jobConfig.subConfig(name.getValue() + "."))).collect(Collectors.toSet());
}
Also used : TimeZone(java.util.TimeZone) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) HashMap(java.util.HashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ConfigInterpolator(com.enonic.xp.config.ConfigInterpolator) Collectors(java.util.stream.Collectors) PropertyTreeMarshallerService(com.enonic.xp.form.PropertyTreeMarshallerService) CalendarService(com.enonic.xp.scheduler.CalendarService) Configuration(com.enonic.xp.config.Configuration) Component(org.osgi.service.component.annotations.Component) Matcher(java.util.regex.Matcher) PrincipalKey(com.enonic.xp.security.PrincipalKey) DescriptorKey(com.enonic.xp.page.DescriptorKey) Map(java.util.Map) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) Activate(org.osgi.service.component.annotations.Activate) ConfigBuilder(com.enonic.xp.config.ConfigBuilder) Pattern(java.util.regex.Pattern) CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) Reference(org.osgi.service.component.annotations.Reference) ScheduleCalendar(com.enonic.xp.scheduler.ScheduleCalendar) Configuration(com.enonic.xp.config.Configuration) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName)

Example 9 with CreateScheduledJobParams

use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.

the class SchedulerServiceImplTest method create.

@Test
void create() throws Exception {
    final ScheduledJobName name = ScheduledJobName.from("test");
    final DescriptorKey descriptor = DescriptorKey.from(ApplicationKey.from("com.enonic.app.features"), "landing");
    final CronCalendar calendar = calendarService.cron("* * * * *", TimeZone.getDefault());
    final PropertyTree config = new PropertyTree();
    config.addString("string", "value");
    final PrincipalKey user = PrincipalKey.from("user:system:user");
    final CreateScheduledJobParams params = CreateScheduledJobParams.create().name(name).descriptor(descriptor).calendar(calendar).config(config).description("description").enabled(true).user(user).build();
    final Instant now = Instant.now();
    Thread.sleep(100);
    final ScheduledJob scheduledJob = adminContext().callWith(() -> schedulerService.create(params));
    assertEquals(name, scheduledJob.getName());
    assertEquals(descriptor, scheduledJob.getDescriptor());
    assertEquals(calendar.getCronValue(), ((CronCalendar) scheduledJob.getCalendar()).getCronValue());
    assertEquals(calendar.getTimeZone(), ((CronCalendar) scheduledJob.getCalendar()).getTimeZone());
    assertEquals(config, scheduledJob.getConfig());
    assertEquals("description", scheduledJob.getDescription());
    assertEquals(user, scheduledJob.getUser());
    assertEquals("user:system:repo-test-user", scheduledJob.getModifier().toString());
    assertEquals("user:system:repo-test-user", scheduledJob.getCreator().toString());
    assertTrue(now.isBefore(scheduledJob.getCreatedTime()));
    assertTrue(now.isBefore(scheduledJob.getModifiedTime()));
    assertTrue(scheduledJob.isEnabled());
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) CronCalendar(com.enonic.xp.scheduler.CronCalendar) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) Instant(java.time.Instant) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) DescriptorKey(com.enonic.xp.page.DescriptorKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 10 with CreateScheduledJobParams

use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.

the class SchedulerServiceImplTest method createWithoutAccess.

@Test
void createWithoutAccess() {
    final ScheduledJobName name = ScheduledJobName.from("test");
    final CronCalendar calendar = calendarService.cron("* * * * *", TimeZone.getDefault());
    final CreateScheduledJobParams params = CreateScheduledJobParams.create().name(name).calendar(calendar).descriptor(DescriptorKey.from(ApplicationKey.from("com.enonic.app.features"), "landing")).config(new PropertyTree()).build();
    assertThrows(NodeAccessException.class, () -> schedulerService.create(params));
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) CronCalendar(com.enonic.xp.scheduler.CronCalendar) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Aggregations

CreateScheduledJobParams (com.enonic.xp.scheduler.CreateScheduledJobParams)17 Test (org.junit.jupiter.api.Test)13 PropertyTree (com.enonic.xp.data.PropertyTree)11 ScheduledJobName (com.enonic.xp.scheduler.ScheduledJobName)7 ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)6 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)5 DescriptorKey (com.enonic.xp.page.DescriptorKey)4 CronCalendar (com.enonic.xp.scheduler.CronCalendar)4 PrincipalKey (com.enonic.xp.security.PrincipalKey)4 HashMap (java.util.HashMap)4 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)3 ScheduleCalendar (com.enonic.xp.scheduler.ScheduleCalendar)3 SchedulableTask (com.enonic.xp.impl.scheduler.distributed.SchedulableTask)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Instant (java.time.Instant)2 Map (java.util.Map)2 TimeZone (java.util.TimeZone)2 TimeUnit (java.util.concurrent.TimeUnit)2 Matcher (java.util.regex.Matcher)2 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)1