Search in sources :

Example 1 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo in project sling by apache.

the class JobManagerImpl method stopJobById.

private void stopJobById(final String jobId, final boolean forward) {
    final JobImpl job = (JobImpl) this.getJobById(jobId);
    if (job != null && !this.configuration.isStoragePath(job.getResourcePath())) {
        // get the queue configuration
        final QueueInfo queueInfo = this.configuration.getQueueConfigurationManager().getQueueInfo(job.getTopic());
        final JobQueueImpl queue = (JobQueueImpl) this.qManager.getQueue(queueInfo.queueName);
        boolean stopped = false;
        if (queue != null) {
            stopped = queue.stopJob(job);
        }
        if (forward && !stopped) {
            // mark the job as stopped
            final JobHandler jh = new JobHandler(job, null, this.configuration);
            jh.finished(JobState.STOPPED, true, null);
        }
    }
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) JobQueueImpl(org.apache.sling.event.impl.jobs.queues.JobQueueImpl)

Example 2 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo in project sling by apache.

the class JobManagerImpl method addJobInternal.

/**
     * Persist the job in the resource tree
     * @param jobTopic The required job topic
     * @param jobName The optional job name
     * @param passedJobProperties The optional job properties
     * @return The persisted job or <code>null</code>.
     */
private Job addJobInternal(final String jobTopic, final Map<String, Object> jobProperties, final List<String> errors) {
    final QueueInfo info = this.configuration.getQueueConfigurationManager().getQueueInfo(jobTopic);
    final TopologyCapabilities caps = this.configuration.getTopologyCapabilities();
    info.targetId = (caps == null ? null : caps.detectTarget(jobTopic, jobProperties, info));
    if (logger.isDebugEnabled()) {
        if (info.targetId != null) {
            logger.debug("Persisting job {} into queue {}, target={}", new Object[] { Utility.toString(jobTopic, jobProperties), info.queueName, info.targetId });
        } else {
            logger.debug("Persisting job {} into queue {}", Utility.toString(jobTopic, jobProperties), info.queueName);
        }
    }
    final ResourceResolver resolver = this.configuration.createResourceResolver();
    try {
        final JobImpl job = this.writeJob(resolver, jobTopic, jobProperties, info);
        if (info.targetId != null) {
            this.configuration.getAuditLogger().debug("ASSIGN OK {} : {}", info.targetId, job.getId());
        } else {
            this.configuration.getAuditLogger().debug("UNASSIGN OK : {}", job.getId());
        }
        return job;
    } catch (final PersistenceException re) {
        // something went wrong, so let's log it
        this.logger.error("Exception during persisting new job '" + Utility.toString(jobTopic, jobProperties) + "'", re);
    } finally {
        resolver.close();
    }
    if (errors != null) {
        errors.add("Unable to persist new job.");
    }
    return null;
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) TopologyCapabilities(org.apache.sling.event.impl.jobs.config.TopologyCapabilities)

Example 3 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo 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 4 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo 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)

Example 5 with QueueInfo

use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo in project sling by apache.

the class JobHandler method reassign.

/**
     * Reassign to a new instance.
     */
public void reassign() {
    final QueueInfo queueInfo = this.configuration.getQueueConfigurationManager().getQueueInfo(job.getTopic());
    // Sanity check if queue configuration has changed
    final TopologyCapabilities caps = this.configuration.getTopologyCapabilities();
    final String targetId = (caps == null ? null : caps.detectTarget(job.getTopic(), job.getProperties(), queueInfo));
    final ResourceResolver resolver = this.configuration.createResourceResolver();
    try {
        final Resource jobResource = resolver.getResource(job.getResourcePath());
        if (jobResource != null) {
            try {
                final ValueMap vm = ResourceHelper.getValueMap(jobResource);
                final String newPath = this.configuration.getUniquePath(targetId, job.getTopic(), job.getId(), job.getProperties());
                final Map<String, Object> props = new HashMap<>(vm);
                props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
                if (targetId == null) {
                    props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
                } else {
                    props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
                }
                props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                try {
                    ResourceHelper.getOrCreateResource(resolver, newPath, props);
                    resolver.delete(jobResource);
                    resolver.commit();
                } catch (final PersistenceException pe) {
                    this.configuration.getMainLogger().warn("Unable to reassign job " + job.getId(), pe);
                }
            } catch (final InstantiationException ie) {
                // something happened with the resource in the meantime
                this.configuration.getMainLogger().debug("Unable to instantiate job", ie);
            }
        }
    } finally {
        resolver.close();
    }
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) TopologyCapabilities(org.apache.sling.event.impl.jobs.config.TopologyCapabilities)

Aggregations

QueueInfo (org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo)9 HashMap (java.util.HashMap)6 PersistenceException (org.apache.sling.api.resource.PersistenceException)6 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 Resource (org.apache.sling.api.resource.Resource)4 QueueConfigurationManager (org.apache.sling.event.impl.jobs.config.QueueConfigurationManager)4 Map (java.util.Map)3 InstanceDescription (org.apache.sling.discovery.InstanceDescription)3 JobTopicTraverser (org.apache.sling.event.impl.jobs.JobTopicTraverser)3 TopologyCapabilities (org.apache.sling.event.impl.jobs.config.TopologyCapabilities)2 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1 JobQueueImpl (org.apache.sling.event.impl.jobs.queues.JobQueueImpl)1