Search in sources :

Example 1 with ScheduleCalendar

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();
}
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 2 with ScheduleCalendar

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()));
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) Instant(java.time.Instant) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) OneTimeCalendar(com.enonic.xp.scheduler.OneTimeCalendar) DescriptorKey(com.enonic.xp.page.DescriptorKey) ScheduleCalendar(com.enonic.xp.scheduler.ScheduleCalendar) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Aggregations

CreateScheduledJobParams (com.enonic.xp.scheduler.CreateScheduledJobParams)2 ScheduleCalendar (com.enonic.xp.scheduler.ScheduleCalendar)2 PropertyTree (com.enonic.xp.data.PropertyTree)1 DescriptorKey (com.enonic.xp.page.DescriptorKey)1 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)1 OneTimeCalendar (com.enonic.xp.scheduler.OneTimeCalendar)1 ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)1 ScheduledJobName (com.enonic.xp.scheduler.ScheduledJobName)1 PrincipalKey (com.enonic.xp.security.PrincipalKey)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TimeZone (java.util.TimeZone)1 Matcher (java.util.regex.Matcher)1 Test (org.junit.jupiter.api.Test)1