use of org.apache.sling.event.impl.jobs.JobImpl in project sling by apache.
the class JobQueueImpl method removeAll.
/**
* @see org.apache.sling.event.jobs.Queue#removeAll()
*/
@Override
public synchronized void removeAll() {
final Set<String> topics = this.cache.getTopics();
logger.debug("Removing all jobs for queue {} : {}", queueName, topics);
if (!topics.isEmpty()) {
final ResourceResolver resolver = this.services.configuration.createResourceResolver();
try {
final Resource baseResource = resolver.getResource(this.services.configuration.getLocalJobsPath());
// sanity check - should never be null
if (baseResource != null) {
final BatchResourceRemover brr = new BatchResourceRemover();
for (final String t : topics) {
final Resource topicResource = baseResource.getChild(t.replace('/', '.'));
if (topicResource != null) {
JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {
@Override
public boolean handle(final JobImpl job) {
final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
// sanity check
if (jobResource != null) {
try {
brr.delete(jobResource);
} catch (final PersistenceException ignore) {
logger.error("Unable to remove job " + job, ignore);
topicResource.getResourceResolver().revert();
topicResource.getResourceResolver().refresh();
}
}
return true;
}
});
}
}
try {
resolver.commit();
} catch (final PersistenceException ignore) {
logger.error("Unable to remove jobs", ignore);
}
}
} finally {
resolver.close();
}
}
}
use of org.apache.sling.event.impl.jobs.JobImpl in project sling by apache.
the class QueueJobCache method loadJobs.
/**
* Load the next N x numberOf(topics) jobs
* @param checkingTopics The set of topics to check.
*/
private void loadJobs(final String queueName, final Set<String> checkingTopics, final StatisticsManager statisticsManager) {
logger.debug("Starting jobs loading from {}...", checkingTopics);
final Map<String, List<JobImpl>> topicCache = new HashMap<String, List<JobImpl>>();
final ResourceResolver resolver = this.configuration.createResourceResolver();
try {
final Resource baseResource = resolver.getResource(this.configuration.getLocalJobsPath());
// sanity check - should never be null
if (baseResource != null) {
for (final String topic : checkingTopics) {
final Resource topicResource = baseResource.getChild(topic.replace('/', '.'));
if (topicResource != null) {
topicCache.put(topic, loadJobs(queueName, topic, topicResource, statisticsManager));
}
}
}
} finally {
resolver.close();
}
orderTopics(topicCache);
logger.debug("Finished jobs loading {}", this.cache.size());
}
use of org.apache.sling.event.impl.jobs.JobImpl in project sling by apache.
the class QueueJobCache method loadJobs.
/**
* Load the next N x numberOf(topics) jobs.
* @param topic The topic
* @param topicResource The parent resource of the jobs
* @return The cache which will be filled with the jobs.
*/
private List<JobImpl> loadJobs(final String queueName, final String topic, final Resource topicResource, final StatisticsManager statisticsManager) {
logger.debug("Loading jobs from topic {}", topic);
final List<JobImpl> list = new ArrayList<JobImpl>();
final AtomicBoolean scanTopic = new AtomicBoolean(false);
JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {
@Override
public boolean handle(final JobImpl job) {
if (job.getProcessingStarted() == null && !job.hasReadErrors()) {
list.add(job);
statisticsManager.jobQueued(queueName, topic);
if (list.size() == maxPreloadLimit) {
scanTopic.set(true);
}
} else if (job.getProcessingStarted() != null) {
logger.debug("Ignoring job {} - processing already started.", job);
} else {
// error reading job
scanTopic.set(true);
if (job.isReadErrorRecoverable()) {
logger.debug("Ignoring job {} due to recoverable read errors.", job);
} else {
logger.debug("Failing job {} due to unrecoverable read errors.", job);
final JobHandler handler = new JobHandler(job, null, configuration);
handler.finished(JobState.ERROR, true, null);
}
}
return list.size() < maxPreloadLimit;
}
});
if (scanTopic.get()) {
synchronized (this.topicsWithNewJobs) {
this.topicsWithNewJobs.add(topic);
}
}
logger.debug("Caching {} jobs for topic {}", list.size(), topic);
return list;
}
use of org.apache.sling.event.impl.jobs.JobImpl in project sling by apache.
the class FindUnfinishedJobsTask method initTopic.
/**
* Initialize a topic and update all jobs from that topic.
* Reset started time and increase retry count of unfinished jobs
* @param topicResource The topic resource
*/
private void initTopic(final Resource topicResource) {
logger.debug("Initializing topic {}...", topicResource.getName());
JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {
@Override
public boolean handle(final JobImpl job) {
if (job.getProcessingStarted() != null) {
logger.debug("Found unfinished job {}", job.getId());
job.retry();
try {
final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
// sanity check
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
mvm.remove(Job.PROPERTY_JOB_STARTED_TIME);
mvm.put(Job.PROPERTY_JOB_RETRY_COUNT, job.getRetryCount());
if (job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class) == null) {
mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
}
jobResource.getResourceResolver().commit();
}
} catch (final PersistenceException ignore) {
logger.error("Unable to update unfinished job " + job, ignore);
}
} else if (job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class) == null) {
logger.debug("Found job without queued date {}", job.getId());
try {
final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
// sanity check
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
jobResource.getResourceResolver().commit();
}
} catch (final PersistenceException ignore) {
logger.error("Unable to update queued date for job " + job.getId(), ignore);
}
}
return true;
}
});
logger.debug("Topic {} initialized", topicResource.getName());
}
use of org.apache.sling.event.impl.jobs.JobImpl in project sling by apache.
the class QueueJobCache method getNextJob.
/**
* Get the next job.
* This method is potentially called concurrently, and
* {@link #reschedule(String, JobHandler, StatisticsManager)} and {@link #handleNewTopics(Set)}
* can be called concurrently.
* @param jobConsumerManager The job consumer manager
* @param statisticsManager The statistics manager
* @param queue The queue
* @param doFull Whether to do a full scan
* @return The job handler or {@code null}.
*/
public JobHandler getNextJob(final JobConsumerManager jobConsumerManager, final StatisticsManager statisticsManager, final Queue queue, final boolean doFull) {
JobHandler handler = null;
if (!this.queueIsBlocked.get()) {
synchronized (this.cache) {
boolean retry;
do {
retry = false;
if (this.cache.isEmpty()) {
final Set<String> checkingTopics = new HashSet<String>();
synchronized (this.topicsWithNewJobs) {
checkingTopics.addAll(this.topicsWithNewJobs);
this.topicsWithNewJobs.clear();
}
if (doFull) {
checkingTopics.addAll(this.topics);
}
if (!checkingTopics.isEmpty()) {
this.loadJobs(queue.getName(), checkingTopics, statisticsManager);
}
}
if (!this.cache.isEmpty()) {
final JobImpl job = this.cache.remove(0);
final JobExecutor consumer = jobConsumerManager.getExecutor(job.getTopic());
handler = new JobHandler(job, consumer, this.configuration);
if (consumer != null) {
if (!handler.startProcessing(queue)) {
statisticsManager.jobDequeued(queue.getName(), handler.getJob().getTopic());
if (logger.isDebugEnabled()) {
logger.debug("Discarding removed job {}", Utility.toString(job));
}
handler = null;
retry = true;
}
} else {
statisticsManager.jobDequeued(queue.getName(), handler.getJob().getTopic());
// no consumer on this instance, assign to another instance
handler.reassign();
handler = null;
retry = true;
}
}
} while (handler == null && retry);
}
}
return handler;
}
Aggregations