aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-09-17 22:54:47 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-09-18 23:48:48 +0200
commitd254d8ef61f2793a079f3b04afc4dc0a798e6df2 (patch)
treeb01cbf8e88cbd93da78f77cb73627551eb6c8d07 /server
parent024672b883d5b81af0838106fe53df31365444f2 (diff)
downloadsonarqube-d254d8ef61f2793a079f3b04afc4dc0a798e6df2.tar.gz
sonarqube-d254d8ef61f2793a079f3b04afc4dc0a798e6df2.zip
SONAR-6630 At startup, delete past compute engine tasks older than 6 months
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/PurgeCeActivities.java61
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java5
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/PurgeCeActivitiesTest.java62
3 files changed, 126 insertions, 2 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/PurgeCeActivities.java b/server/sonar-server/src/main/java/org/sonar/server/computation/PurgeCeActivities.java
new file mode 100644
index 00000000000..00a6f7d5dbd
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/PurgeCeActivities.java
@@ -0,0 +1,61 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.computation;
+
+import java.util.Calendar;
+import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+
+@ServerSide
+public class PurgeCeActivities {
+
+ private static final Logger LOGGER = Loggers.get(PurgeCeActivities.class);
+
+ private final DbClient dbClient;
+ private final System2 system2;
+
+ public PurgeCeActivities(DbClient dbClient, System2 system2) {
+ this.dbClient = dbClient;
+ this.system2 = system2;
+ }
+
+ /**
+ * Do not rename. Used at server startup.
+ */
+ public void start() {
+ DbSession dbSession = dbClient.openSession(false);
+ try {
+ Calendar sixMonthsAgo = Calendar.getInstance();
+ sixMonthsAgo.setTimeInMillis(system2.now());
+ sixMonthsAgo.add(Calendar.DATE, -180);
+
+ LOGGER.info("Delete the Compute Engine tasks created before " + sixMonthsAgo.getTime());
+ dbClient.ceActivityDao().deleteOlderThan(dbSession, sixMonthsAgo.getTimeInMillis());
+ dbSession.commit();
+
+ } finally {
+ dbClient.closeSession(dbSession);
+ }
+ }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java
index f463289c455..565b7c5a5e8 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java
@@ -19,7 +19,7 @@
*/
package org.sonar.server.platform.platformlevel;
-import org.sonar.server.computation.CeQueueInitializer;
+import org.sonar.server.computation.PurgeCeActivities;
import org.sonar.server.issue.filter.RegisterIssueFilters;
import org.sonar.server.platform.ServerLifecycleNotifier;
import org.sonar.server.qualitygate.RegisterQualityGates;
@@ -64,7 +64,8 @@ public class PlatformLevelStartup extends PlatformLevel {
RegisterServletFilters.class,
RegisterIssueFilters.class,
RenameIssueWidgets.class,
- ServerLifecycleNotifier.class);
+ ServerLifecycleNotifier.class,
+ PurgeCeActivities.class);
}
@Override
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/PurgeCeActivitiesTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/PurgeCeActivitiesTest.java
new file mode 100644
index 00000000000..2b9f65d7172
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/PurgeCeActivitiesTest.java
@@ -0,0 +1,62 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.computation;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.db.DbTester;
+import org.sonar.db.ce.CeActivityDto;
+import org.sonar.db.ce.CeQueueDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PurgeCeActivitiesTest {
+
+ TestSystem2 system2 = new TestSystem2();
+
+ @Rule
+ public DbTester dbTester = DbTester.create(system2);
+ PurgeCeActivities underTest = new PurgeCeActivities(dbTester.getDbClient(), system2);
+
+ @Test
+ public void delete_older_than_6_months() throws Exception {
+ insertWithDate("VERY_OLD", 1_000_000_000_000L);
+ insertWithDate("RECENT", 1_500_000_000_000L);
+ system2.setNow(1_500_000_000_100L);
+
+ underTest.start();
+
+ assertThat(dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "VERY_OLD").isPresent()).isFalse();
+ assertThat(dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "RECENT").isPresent()).isTrue();
+ }
+
+ private void insertWithDate(String uuid, long date) {
+ CeQueueDto queueDto = new CeQueueDto();
+ queueDto.setUuid(uuid);
+ queueDto.setTaskType("fake");
+
+ CeActivityDto dto = new CeActivityDto(queueDto);
+ dto.setStatus(CeActivityDto.Status.SUCCESS);
+ system2.setNow(date);
+ dbTester.getDbClient().ceActivityDao().insert(dbTester.getSession(), dto);
+ dbTester.getSession().commit();
+ }
+}