]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1953 Minimum period used in purge tasks should be configurable
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 12 Nov 2010 13:25:15 +0000 (13:25 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 12 Nov 2010 13:25:15 +0000 (13:25 +0000)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/purges/PurgeEntities.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/purges/PurgeUnprocessed.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/purges/PurgeEntitiesTest.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/purges/PurgeUnprocessedTest.java
sonar-plugin-api/src/main/java/org/sonar/api/purge/PurgeUtils.java [new file with mode: 0644]
sonar-plugin-api/src/test/java/org/sonar/api/purge/PurgeUtilsTest.java [new file with mode: 0644]

index ccd03543a3382d376b53b4baec8b29b1f589cd2b..80b2843aee5defd0cadf57e3acd361fb9c2b54bd 100644 (file)
  */
 package org.sonar.plugins.core.purges;
 
+import org.apache.commons.configuration.Configuration;
 import org.apache.commons.lang.time.DateUtils;
 import org.sonar.api.batch.PurgeContext;
 import org.sonar.api.database.DatabaseSession;
 import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.purge.PurgeUtils;
 import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.Logs;
 import org.sonar.core.purge.AbstractPurge;
 
 import java.util.Date;
@@ -36,14 +39,18 @@ import javax.persistence.Query;
  */
 public class PurgeEntities extends AbstractPurge {
 
-  private static final int MINIMUM_AGE_IN_HOURS = -12;
+  private Configuration configuration;
 
-  public PurgeEntities(DatabaseSession session) {
+  public PurgeEntities(DatabaseSession session, Configuration conf) {
     super(session);
+    this.configuration = conf;
   }
 
   public void purge(PurgeContext context) {
-    final Date beforeDate = DateUtils.addHours(new Date(), MINIMUM_AGE_IN_HOURS);
+    int minimumPeriodInHours = PurgeUtils.getMinimumPeriodInHours(configuration);
+    final Date beforeDate = DateUtils.addHours(new Date(), - minimumPeriodInHours);
+    Logs.INFO.info("Deleting files data before " + beforeDate);
+    
     Query query = getSession().createQuery("SELECT s.id FROM " + Snapshot.class.getSimpleName() + " s WHERE s.last=false AND scope=:scope AND s.createdAt<:date");
     query.setParameter("scope", Resource.SCOPE_ENTITY);
     query.setParameter("date", beforeDate);
index 08fc2ec55141947510760f6688b7233e3c7e7bf9..e4c6004a7c172062302dcb1ddf47310ca7c31b49 100644 (file)
  */
 package org.sonar.plugins.core.purges;
 
+import org.apache.commons.configuration.Configuration;
 import org.apache.commons.lang.time.DateUtils;
 import org.sonar.api.batch.PurgeContext;
 import org.sonar.api.database.DatabaseSession;
 import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.purge.PurgeUtils;
+import org.sonar.api.utils.Logs;
 import org.sonar.core.purge.AbstractPurge;
 
+import javax.persistence.Query;
 import java.util.Date;
 import java.util.List;
 
-import javax.persistence.Query;
-
 /**
  * @since 1.11
  */
 public class PurgeUnprocessed extends AbstractPurge {
 
-  private static final int MINIMUM_AGE_IN_HOURS = -12;
+  private Configuration configuration;
 
-  public PurgeUnprocessed(DatabaseSession session) {
+  public PurgeUnprocessed(DatabaseSession session, Configuration conf) {
     super(session);
+    this.configuration = conf;
   }
 
   public void purge(PurgeContext context) {
-    final Date beforeDate = DateUtils.addHours(new Date(), MINIMUM_AGE_IN_HOURS);
+    int minimumPeriodInHours = PurgeUtils.getMinimumPeriodInHours(configuration);
+    final Date beforeDate = DateUtils.addHours(new Date(), -minimumPeriodInHours);
+    Logs.INFO.info("Deleting unprocessed data before " + beforeDate);
+
     Query query = getSession().createQuery("SELECT s.id FROM " + Snapshot.class.getSimpleName() + " s WHERE s.last=false AND status=:status AND s.createdAt<:date");
     query.setParameter("status", Snapshot.STATUS_UNPROCESSED);
     query.setParameter("date", beforeDate);
index aa354af823f74162a56c1a74331f46a89b9b8c78..6eef94262f35305a39171c6527809c7ca29dbc7b 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.plugins.core.purges;
 
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.junit.Test;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
@@ -28,7 +29,7 @@ public class PurgeEntitiesTest extends AbstractDbUnitTestCase {
   public void purgeEntities() {
     setupData("sharedFixture", "purgeEntities");
 
-    final PurgeEntities purge = new PurgeEntities(getSession());
+    final PurgeEntities purge = new PurgeEntities(getSession(), new PropertiesConfiguration());
     purge.purge(null);
 
     checkTables("purgeEntities", "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources");
index 768098d6a00f734d72cc75c0e3b9b068872849fc..943768afdf1249147bcd07398de78105bef703eb 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.plugins.core.purges;
 
+import org.apache.commons.configuration.PropertiesConfiguration;
 import org.junit.Test;
 import org.sonar.jpa.test.AbstractDbUnitTestCase;
 
@@ -28,7 +29,7 @@ public class PurgeUnprocessedTest extends AbstractDbUnitTestCase {
   public void purgeUnprocessed() {
     setupData("sharedFixture", "purgeUnprocessed");
 
-    new PurgeUnprocessed(getSession()).purge(null);
+    new PurgeUnprocessed(getSession(), new PropertiesConfiguration()).purge(null);
 
     checkTables("purgeUnprocessed", "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources");
   }
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/purge/PurgeUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/purge/PurgeUtils.java
new file mode 100644 (file)
index 0000000..95f9c5f
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.api.purge;
+
+import org.apache.commons.configuration.Configuration;
+
+public final class PurgeUtils {
+
+  public static final int DEFAULT_MINIMUM_PERIOD_IN_HOURS = 12;
+  public static final String PROP_KEY_MINIMUM_PERIOD_IN_HOURS = "sonar.purge.minimumPeriodInHours";
+
+  private PurgeUtils() {
+    // only static methods
+  }
+
+  public static int getMinimumPeriodInHours(Configuration conf) {
+    int hours = DEFAULT_MINIMUM_PERIOD_IN_HOURS;
+    if (conf != null) {
+      hours = conf.getInt(PROP_KEY_MINIMUM_PERIOD_IN_HOURS, DEFAULT_MINIMUM_PERIOD_IN_HOURS);
+    }
+    return hours;
+  }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/purge/PurgeUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/purge/PurgeUtilsTest.java
new file mode 100644 (file)
index 0000000..f88ed19
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.api.purge;
+
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.junit.Test;
+import static org.hamcrest.core.Is.is;
+
+import static org.junit.Assert.assertThat;
+
+public class PurgeUtilsTest {
+
+  @Test
+  public void shouldReturnDefaultMinimumPeriod() {
+    assertThat(PurgeUtils.getMinimumPeriodInHours(new PropertiesConfiguration()), is(PurgeUtils.DEFAULT_MINIMUM_PERIOD_IN_HOURS));
+  }
+
+  @Test
+  public void shouldReturnMinimumPeriod() {
+    PropertiesConfiguration conf = new PropertiesConfiguration();
+    conf.setProperty(PurgeUtils.PROP_KEY_MINIMUM_PERIOD_IN_HOURS, "9");
+    assertThat(PurgeUtils.getMinimumPeriodInHours(conf), is(9));
+  }
+}