You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2013/10/17 11:23:34 UTC
svn commit: r1533018 - in
/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs:
JobManagerConfiguration.java JobManagerImpl.java
tasks/HistoryCleanUpTask.java
Author: cziegeler
Date: Thu Oct 17 09:23:34 2013
New Revision: 1533018
URL: http://svn.apache.org/r1533018
Log:
SLiNG-3184 : Create a job consumer to clean up the history
Modified:
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerConfiguration.java
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java
sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerConfiguration.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerConfiguration.java?rev=1533018&r1=1533017&r2=1533018&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerConfiguration.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerConfiguration.java Thu Oct 17 09:23:34 2013
@@ -242,6 +242,14 @@ public class JobManagerConfiguration {
return this.disabledDistribution;
}
+ public String getStoredCancelledJobsPath() {
+ return this.storedCancelledJobsPath;
+ }
+
+ public String getStoredSuccessfulJobsPath() {
+ return this.storedSuccessfulJobsPath;
+ }
+
/**
* Get the storage path for finished jobs.
* @param finishedJob The finished job
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java?rev=1533018&r1=1533017&r2=1533018&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/JobManagerImpl.java Thu Oct 17 09:23:34 2013
@@ -1565,4 +1565,8 @@ public class JobManagerImpl
}
return null;
}
+
+ public JobManagerConfiguration getConfiguration() {
+ return this.configuration;
+ }
}
Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java?rev=1533018&r1=1533017&r2=1533018&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/tasks/HistoryCleanUpTask.java Thu Oct 17 09:23:34 2013
@@ -30,8 +30,10 @@ import org.apache.sling.api.resource.Per
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.event.impl.jobs.JobManagerImpl;
import org.apache.sling.event.impl.support.BatchResourceRemover;
import org.apache.sling.event.jobs.Job;
+import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.consumer.JobExecutionContext;
import org.apache.sling.event.jobs.consumer.JobExecutionResult;
import org.apache.sling.event.jobs.consumer.JobExecutor;
@@ -55,6 +57,9 @@ public class HistoryCleanUpTask implemen
@Reference
private ResourceResolverFactory resourceResolverFactory;
+ @Reference
+ private JobManager jobManager;
+
@Override
public JobExecutionResult process(final Job job, final JobExecutionContext context) {
int age = job.getProperty(PROPERTY_AGE, DEFAULT_AGE);
@@ -69,97 +74,104 @@ public class HistoryCleanUpTask implemen
try {
resolver = this.resourceResolverFactory.getAdministrativeResourceResolver(null);
- String basePath = "";
- final Resource baseResource = resolver.getResource(basePath);
- // sanity check - should never be null
- if ( baseResource != null ) {
- final Iterator<Resource> topicIter = baseResource.listChildren();
- while ( !context.isStopped() && topicIter.hasNext() ) {
- final Resource topicResource = topicIter.next();
-
- // now years
- final Iterator<Resource> yearIter = topicResource.listChildren();
- while ( !context.isStopped() && yearIter.hasNext() ) {
- final Resource yearResource = yearIter.next();
- final int year = Integer.valueOf(yearResource.getName());
- final boolean oldYear = year < now.get(Calendar.YEAR);
-
- // months
- final Iterator<Resource> monthIter = yearResource.listChildren();
- while ( !context.isStopped() && monthIter.hasNext() ) {
- final Resource monthResource = monthIter.next();
- final int month = Integer.valueOf(monthResource.getName());
- final boolean oldMonth = oldYear || month < (now.get(Calendar.MONTH) + 1);
-
- // days
- final Iterator<Resource> dayIter = monthResource.listChildren();
- while ( !context.isStopped() && dayIter.hasNext() ) {
- final Resource dayResource = dayIter.next();
- final int day = Integer.valueOf(dayResource.getName());
- final boolean oldDay = oldMonth || day < now.get(Calendar.DAY_OF_MONTH);
-
- // hours
- final Iterator<Resource> hourIter = dayResource.listChildren();
- while ( !context.isStopped() && hourIter.hasNext() ) {
- final Resource hourResource = hourIter.next();
- final int hour = Integer.valueOf(hourResource.getName());
- final boolean oldHour = oldDay || hour < now.get(Calendar.HOUR_OF_DAY);
-
- // minutes
- final Iterator<Resource> minuteIter = hourResource.listChildren();
- while ( !context.isStopped() && minuteIter.hasNext() ) {
- final Resource minuteResource = minuteIter.next();
-
- // check if we can delete the minute
- final int minute = Integer.valueOf(minuteResource.getName());
- final boolean oldMinute = oldHour || minute <= now.get(Calendar.MINUTE);
- if ( oldMinute ) {
- BatchResourceRemover remover = new BatchResourceRemover();
- remover.delete(minuteResource);
- resolver.commit();
- }
- }
+ this.cleanup(now, resolver, context, ((JobManagerImpl)jobManager).getConfiguration().getStoredCancelledJobsPath());
+ this.cleanup(now, resolver, context, ((JobManagerImpl)jobManager).getConfiguration().getStoredSuccessfulJobsPath());
+
+
+ } catch (final PersistenceException pe) {
+ // in the case of an error, we just log this as a warning
+ this.logger.warn("Exception during job resource tree cleanup.", pe);
+ } catch (final LoginException ignore) {
+ this.ignoreException(ignore);
+ } finally {
+ if ( resolver != null ) {
+ resolver.close();
+ }
+ }
+ return context.result().succeeded();
+ }
- // check if we can delete the hour
- if ( !context.isStopped() && oldHour && !hourResource.listChildren().hasNext()) {
- resolver.delete(hourResource);
+ private void cleanup(final Calendar now,
+ final ResourceResolver resolver, final JobExecutionContext context, final String basePath)
+ throws PersistenceException {
+ final Resource baseResource = resolver.getResource(basePath);
+ // sanity check - should never be null
+ if ( baseResource != null ) {
+ final Iterator<Resource> topicIter = baseResource.listChildren();
+ while ( !context.isStopped() && topicIter.hasNext() ) {
+ final Resource topicResource = topicIter.next();
+
+ // now years
+ final Iterator<Resource> yearIter = topicResource.listChildren();
+ while ( !context.isStopped() && yearIter.hasNext() ) {
+ final Resource yearResource = yearIter.next();
+ final int year = Integer.valueOf(yearResource.getName());
+ final boolean oldYear = year < now.get(Calendar.YEAR);
+
+ // months
+ final Iterator<Resource> monthIter = yearResource.listChildren();
+ while ( !context.isStopped() && monthIter.hasNext() ) {
+ final Resource monthResource = monthIter.next();
+ final int month = Integer.valueOf(monthResource.getName());
+ final boolean oldMonth = oldYear || month < (now.get(Calendar.MONTH) + 1);
+
+ // days
+ final Iterator<Resource> dayIter = monthResource.listChildren();
+ while ( !context.isStopped() && dayIter.hasNext() ) {
+ final Resource dayResource = dayIter.next();
+ final int day = Integer.valueOf(dayResource.getName());
+ final boolean oldDay = oldMonth || day < now.get(Calendar.DAY_OF_MONTH);
+
+ // hours
+ final Iterator<Resource> hourIter = dayResource.listChildren();
+ while ( !context.isStopped() && hourIter.hasNext() ) {
+ final Resource hourResource = hourIter.next();
+ final int hour = Integer.valueOf(hourResource.getName());
+ final boolean oldHour = oldDay || hour < now.get(Calendar.HOUR_OF_DAY);
+
+ // minutes
+ final Iterator<Resource> minuteIter = hourResource.listChildren();
+ while ( !context.isStopped() && minuteIter.hasNext() ) {
+ final Resource minuteResource = minuteIter.next();
+
+ // check if we can delete the minute
+ final int minute = Integer.valueOf(minuteResource.getName());
+ final boolean oldMinute = oldHour || minute <= now.get(Calendar.MINUTE);
+ if ( oldMinute ) {
+ BatchResourceRemover remover = new BatchResourceRemover();
+ remover.delete(minuteResource);
resolver.commit();
}
}
- // check if we can delete the day
- if ( !context.isStopped() && oldDay && !dayResource.listChildren().hasNext()) {
- resolver.delete(dayResource);
+
+ // check if we can delete the hour
+ if ( !context.isStopped() && oldHour && !hourResource.listChildren().hasNext()) {
+ resolver.delete(hourResource);
resolver.commit();
}
}
-
- // check if we can delete the month
- if ( !context.isStopped() && oldMonth && !monthResource.listChildren().hasNext() ) {
- resolver.delete(monthResource);
+ // check if we can delete the day
+ if ( !context.isStopped() && oldDay && !dayResource.listChildren().hasNext()) {
+ resolver.delete(dayResource);
resolver.commit();
}
}
- // check if we can delete the year
- if ( !context.isStopped() && oldYear && !yearResource.listChildren().hasNext() ) {
- resolver.delete(yearResource);
+ // check if we can delete the month
+ if ( !context.isStopped() && oldMonth && !monthResource.listChildren().hasNext() ) {
+ resolver.delete(monthResource);
resolver.commit();
}
}
- }
- }
- } catch (final PersistenceException pe) {
- // in the case of an error, we just log this as a warning
- this.logger.warn("Exception during job resource tree cleanup.", pe);
- } catch (final LoginException ignore) {
- this.ignoreException(ignore);
- } finally {
- if ( resolver != null ) {
- resolver.close();
+ // check if we can delete the year
+ if ( !context.isStopped() && oldYear && !yearResource.listChildren().hasNext() ) {
+ resolver.delete(yearResource);
+ resolver.commit();
+ }
+ }
}
}
- return context.result().succeeded();
}
/**