Search in sources :

Example 26 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException 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 27 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class ScheduledJobHandler method remove.

/**
     * Remove a scheduled job
     * @param info The schedule info
     */
public void remove(final ScheduledJobInfoImpl info) {
    final String scheduleKey = ResourceHelper.filterName(info.getName());
    final ResourceResolver resolver = configuration.createResourceResolver();
    try {
        final StringBuilder sb = new StringBuilder(configuration.getScheduledJobsPath(true));
        sb.append(scheduleKey);
        final String path = sb.toString();
        final Resource eventResource = resolver.getResource(path);
        if (eventResource != null) {
            resolver.delete(eventResource);
            resolver.commit();
        }
    } catch (final PersistenceException pe) {
        // we ignore the exception if removing fails
        ignoreException(pe);
    } finally {
        resolver.close();
    }
    synchronized (this.scheduledJobs) {
        final Holder h = scheduledJobs.remove(scheduleKey);
        if (h != null && h.info != null) {
            jobScheduler.unscheduleJob(h.info);
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 28 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class UpgradeTask method processJobsFromPreviousVersions.

/**
     * Handle jobs from previous versions (<= 3.1.4) by moving them to the unassigned area
     */
private void processJobsFromPreviousVersions() {
    final ResourceResolver resolver = configuration.createResourceResolver();
    if (resolver != null) {
        try {
            this.processJobsFromPreviousVersions(resolver.getResource(configuration.getPreviousVersionAnonPath()));
            this.processJobsFromPreviousVersions(resolver.getResource(configuration.getPreviousVersionIdentifiedPath()));
        } catch (final PersistenceException pe) {
            this.logger.warn("Problems moving jobs from previous version.", pe);
        } finally {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 29 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class UpgradeTask method upgradeBridgedJobs.

/**
     * Upgrade bridged jobs
     * @param rootResource  The root resource (topic resource)
     */
private void upgradeBridgedJobs(final Resource topicResource) {
    final String topicName = topicResource.getName().replace('.', '/');
    final QueueConfigurationManager qcm = configuration.getQueueConfigurationManager();
    if (qcm == null) {
        return;
    }
    final QueueInfo info = qcm.getQueueInfo(topicName);
    JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.ResourceCallback() {

        @Override
        public boolean handle(final Resource rsrc) {
            try {
                final ValueMap vm = ResourceHelper.getValueMap(rsrc);
                final String targetId = caps.detectTarget(topicName, vm, info);
                final Map<String, Object> props = new HashMap<>(vm);
                final String newPath;
                if (targetId != null) {
                    newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                    props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
                    props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
                } else {
                    newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                    props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
                    props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
                }
                props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                try {
                    ResourceHelper.getOrCreateResource(topicResource.getResourceResolver(), newPath, props);
                    topicResource.getResourceResolver().delete(rsrc);
                    topicResource.getResourceResolver().commit();
                } catch (final PersistenceException pe) {
                    logger.warn("Unable to move job from previous version " + rsrc.getPath(), pe);
                    topicResource.getResourceResolver().refresh();
                    topicResource.getResourceResolver().revert();
                }
            } catch (final InstantiationException ie) {
                logger.warn("Unable to move job from previous version " + rsrc.getPath(), ie);
                topicResource.getResourceResolver().refresh();
                topicResource.getResourceResolver().revert();
            }
            return caps.isActive();
        }
    });
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) QueueConfigurationManager(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) JobTopicTraverser(org.apache.sling.event.impl.jobs.JobTopicTraverser)

Example 30 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class CheckTopologyTask method assignJobs.

/**
     * Try to assign all jobs from the jobs root.
     * The jobs are stored by topic
     * @param jobsRoot The root of the jobs
     * @param unassign Whether to unassign the job if no instance is found.
     */
private void assignJobs(final Resource jobsRoot, final boolean unassign) {
    final ResourceResolver resolver = jobsRoot.getResourceResolver();
    final Iterator<Resource> topicIter = jobsRoot.listChildren();
    while (caps.isActive() && topicIter.hasNext()) {
        final Resource topicResource = topicIter.next();
        final String topicName = topicResource.getName().replace('.', '/');
        logger.debug("Found topic {}", topicName);
        // first check if there is an instance for these topics
        final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topicName);
        if (potentialTargets != null && potentialTargets.size() > 0) {
            final QueueConfigurationManager qcm = this.configuration.getQueueConfigurationManager();
            if (qcm == null) {
                break;
            }
            final QueueInfo info = qcm.getQueueInfo(topicName);
            logger.debug("Found queue {} for {}", info.queueConfiguration, topicName);
            JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {

                @Override
                public boolean handle(final Resource rsrc) {
                    try {
                        final ValueMap vm = ResourceHelper.getValueMap(rsrc);
                        final String targetId = caps.detectTarget(topicName, vm, info);
                        if (targetId != null) {
                            final String newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                            final Map<String, Object> props = new HashMap<>(vm);
                            props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
                            props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
                            props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                            try {
                                ResourceHelper.getOrCreateResource(resolver, newPath, props);
                                resolver.delete(rsrc);
                                resolver.commit();
                                final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
                                configuration.getAuditLogger().debug("REASSIGN OK {} : {}", targetId, jobId);
                            } catch (final PersistenceException pe) {
                                logger.warn("Unable to move unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
                                resolver.refresh();
                                resolver.revert();
                            }
                        }
                    } catch (final InstantiationException ie) {
                        // something happened with the resource in the meantime
                        logger.warn("Unable to move unassigned job from " + rsrc.getPath(), ie);
                        resolver.refresh();
                        resolver.revert();
                    }
                    return caps.isActive();
                }
            });
        }
        // now unassign if there are still jobs
        if (caps.isActive() && unassign) {
            // we have to move everything to the unassigned area
            JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {

                @Override
                public boolean handle(final Resource rsrc) {
                    try {
                        final ValueMap vm = ResourceHelper.getValueMap(rsrc);
                        final String newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                        final Map<String, Object> props = new HashMap<>(vm);
                        props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
                        props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
                        props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                        try {
                            ResourceHelper.getOrCreateResource(resolver, newPath, props);
                            resolver.delete(rsrc);
                            resolver.commit();
                            final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
                            configuration.getAuditLogger().debug("REUNASSIGN OK : {}", jobId);
                        } catch (final PersistenceException pe) {
                            logger.warn("Unable to unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
                            resolver.refresh();
                            resolver.revert();
                        }
                    } catch (final InstantiationException ie) {
                        // something happened with the resource in the meantime
                        logger.warn("Unable to unassigned job from " + rsrc.getPath(), ie);
                        resolver.refresh();
                        resolver.revert();
                    }
                    return caps.isActive();
                }
            });
        }
    }
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) QueueConfigurationManager(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager) InstanceDescription(org.apache.sling.discovery.InstanceDescription) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) JobTopicTraverser(org.apache.sling.event.impl.jobs.JobTopicTraverser)

Aggregations

PersistenceException (org.apache.sling.api.resource.PersistenceException)146 Resource (org.apache.sling.api.resource.Resource)102 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)63 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)37 HashMap (java.util.HashMap)35 LoginException (org.apache.sling.api.resource.LoginException)32 RepositoryException (javax.jcr.RepositoryException)27 ValueMap (org.apache.sling.api.resource.ValueMap)23 Node (javax.jcr.Node)18 Map (java.util.Map)15 Calendar (java.util.Calendar)14 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ConfigurationPersistenceException (org.apache.sling.caconfig.spi.ConfigurationPersistenceException)6 InstanceDescription (org.apache.sling.discovery.InstanceDescription)6 QueueInfo (org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo)6 Test (org.junit.Test)5 Session (javax.jcr.Session)4