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