use of com.enonic.xp.scheduler.ScheduleCalendar 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.ScheduleCalendar in project xp by enonic.
the class SchedulerServiceImplTest method createOneTimeJob.
@Test
void createOneTimeJob() throws Exception {
final ScheduledJobName name = ScheduledJobName.from("test");
final DescriptorKey descriptor = DescriptorKey.from(ApplicationKey.from("com.enonic.app.features"), "landing");
final ScheduleCalendar calendar = calendarService.oneTime(Instant.parse("2021-02-25T10:44:33.170079900Z"));
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("2021-02-25T10:44:33.170079900Z", ((OneTimeCalendar) scheduledJob.getCalendar()).getValue().toString());
assertEquals(ScheduleCalendarType.ONE_TIME, scheduledJob.getCalendar().getType());
assertEquals("user:system:repo-test-user", scheduledJob.getCreator().toString());
assertEquals("user:system:repo-test-user", scheduledJob.getModifier().toString());
assertTrue(now.isBefore(scheduledJob.getModifiedTime()));
assertTrue(now.isBefore(scheduledJob.getCreatedTime()));
}
Aggregations