Search in sources :

Example 1 with ScheduleInfoImpl

use of org.apache.sling.event.impl.support.ScheduleInfoImpl in project sling by apache.

the class JobSchedulerImpl method addScheduledJob.

/**
     * Add a scheduled job
     * @param topic The job topic
     * @param properties The job properties
     * @param scheduleName The schedule name
     * @param isSuspended Whether it is suspended
     * @param scheduleInfos The scheduling information
     * @param errors Optional list to contain potential errors
     * @return A new job info or {@code null}
     */
public ScheduledJobInfo addScheduledJob(final String topic, final Map<String, Object> properties, final String scheduleName, final boolean isSuspended, final List<ScheduleInfoImpl> scheduleInfos, final List<String> errors) {
    final List<String> msgs = new ArrayList<>();
    if (scheduleName == null || scheduleName.length() == 0) {
        msgs.add("Schedule name not specified");
    }
    final String errorMessage = Utility.checkJob(topic, properties);
    if (errorMessage != null) {
        msgs.add(errorMessage);
    }
    if (scheduleInfos.size() == 0) {
        msgs.add("No schedule defined for " + scheduleName);
    }
    for (final ScheduleInfoImpl info : scheduleInfos) {
        info.check(msgs);
    }
    if (msgs.size() == 0) {
        try {
            final ScheduledJobInfo info = this.scheduledJobHandler.addOrUpdateJob(topic, properties, scheduleName, isSuspended, scheduleInfos);
            if (info != null) {
                return info;
            }
            msgs.add("Unable to persist scheduled job.");
        } catch (final PersistenceException pe) {
            msgs.add("Unable to persist scheduled job: " + scheduleName);
            logger.warn("Unable to persist scheduled job", pe);
        }
    } else {
        for (final String msg : msgs) {
            logger.warn(msg);
        }
    }
    if (errors != null) {
        errors.addAll(msgs);
    }
    return null;
}
Also used : ScheduleInfoImpl(org.apache.sling.event.impl.support.ScheduleInfoImpl) ScheduledJobInfo(org.apache.sling.event.jobs.ScheduledJobInfo) ArrayList(java.util.ArrayList) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 2 with ScheduleInfoImpl

use of org.apache.sling.event.impl.support.ScheduleInfoImpl in project sling by apache.

the class JobSchedulerImpl method startScheduledJob.

/**
     * Start a scheduled job
     * @param info The scheduling info
     */
private void startScheduledJob(final ScheduledJobInfoImpl info) {
    if (this.active.get()) {
        if (!info.isSuspended()) {
            this.configuration.getAuditLogger().debug("SCHEDULED OK name={}, topic={}, properties={} : {}", new Object[] { info.getName(), info.getJobTopic(), info.getJobProperties() }, info.getSchedules());
            int index = 0;
            for (final ScheduleInfo si : info.getSchedules()) {
                final String name = info.getSchedulerJobId() + "-" + String.valueOf(index);
                ScheduleOptions options = null;
                switch(si.getType()) {
                    case DAILY:
                    case WEEKLY:
                    case HOURLY:
                    case MONTHLY:
                    case YEARLY:
                    case CRON:
                        options = this.scheduler.EXPR(((ScheduleInfoImpl) si).getCronExpression());
                        break;
                    case DATE:
                        options = this.scheduler.AT(((ScheduleInfoImpl) si).getNextScheduledExecution());
                        break;
                }
                // Create configuration for scheduled job
                final Map<String, Serializable> config = new HashMap<>();
                config.put(PROPERTY_READ_JOB, info);
                config.put(PROPERTY_SCHEDULE_INDEX, index);
                this.scheduler.schedule(this, options.name(name).config(config).canRunConcurrently(false));
                index++;
            }
        } else {
            this.configuration.getAuditLogger().debug("SCHEDULED SUSPENDED name={}, topic={}, properties={} : {}", new Object[] { info.getName(), info.getJobTopic(), info.getJobProperties(), info.getSchedules() });
        }
    }
}
Also used : Serializable(java.io.Serializable) ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) ScheduleInfoImpl(org.apache.sling.event.impl.support.ScheduleInfoImpl) HashMap(java.util.HashMap) ScheduleInfo(org.apache.sling.event.jobs.ScheduleInfo)

Example 3 with ScheduleInfoImpl

use of org.apache.sling.event.impl.support.ScheduleInfoImpl in project sling by apache.

the class ScheduledJobHandler method writeScheduledJob.

private Map<String, Object> writeScheduledJob(final String jobTopic, final Map<String, Object> jobProperties, final String scheduleName, final boolean suspend, final List<ScheduleInfoImpl> scheduleInfos) throws PersistenceException {
    final ResourceResolver resolver = this.configuration.createResourceResolver();
    try {
        // create properties
        final Map<String, Object> properties = new HashMap<String, Object>();
        if (jobProperties != null) {
            for (final Map.Entry<String, Object> entry : jobProperties.entrySet()) {
                final String propName = entry.getKey();
                if (!ResourceHelper.ignoreProperty(propName)) {
                    properties.put(propName, entry.getValue());
                }
            }
        }
        properties.put(ResourceHelper.PROPERTY_JOB_TOPIC, jobTopic);
        properties.put(Job.PROPERTY_JOB_CREATED, Calendar.getInstance());
        properties.put(Job.PROPERTY_JOB_CREATED_INSTANCE, Environment.APPLICATION_ID);
        // put scheduler name and scheduler info
        properties.put(ResourceHelper.PROPERTY_SCHEDULE_NAME, scheduleName);
        final String[] infoArray = new String[scheduleInfos.size()];
        int index = 0;
        for (final ScheduleInfoImpl info : scheduleInfos) {
            infoArray[index] = info.getSerializedString();
            index++;
        }
        properties.put(ResourceHelper.PROPERTY_SCHEDULE_INFO, infoArray);
        if (suspend) {
            properties.put(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED, Boolean.TRUE);
        }
        // create path and resource
        properties.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, ResourceHelper.RESOURCE_TYPE_SCHEDULED_JOB);
        final String path = this.configuration.getScheduledJobsPath(true) + ResourceHelper.filterName(scheduleName);
        // update existing resource
        final Resource existingInfo = resolver.getResource(path);
        if (existingInfo != null) {
            resolver.delete(existingInfo);
            logger.debug("Updating scheduled job {} at {}", properties, path);
        } else {
            logger.debug("Storing new scheduled job {} at {}", properties, path);
        }
        ResourceHelper.createAndCommitResource(resolver, path, properties);
        // put back real schedule infos
        properties.put(ResourceHelper.PROPERTY_SCHEDULE_INFO, scheduleInfos);
        return properties;
    } finally {
        resolver.close();
    }
}
Also used : ScheduleInfoImpl(org.apache.sling.event.impl.support.ScheduleInfoImpl) HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Map(java.util.Map)

Aggregations

ScheduleInfoImpl (org.apache.sling.event.impl.support.ScheduleInfoImpl)3 HashMap (java.util.HashMap)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 ScheduleOptions (org.apache.sling.commons.scheduler.ScheduleOptions)1 ScheduleInfo (org.apache.sling.event.jobs.ScheduleInfo)1 ScheduledJobInfo (org.apache.sling.event.jobs.ScheduledJobInfo)1