3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.purge;
22 import java.util.Collection;
23 import org.sonar.api.CoreProperties;
24 import org.sonar.api.ce.ComputeEngineSide;
25 import org.sonar.api.config.Configuration;
26 import org.sonar.api.server.ServerSide;
27 import org.sonar.api.utils.TimeUtils;
28 import org.sonar.api.utils.log.Logger;
29 import org.sonar.api.utils.log.Loggers;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.purge.PurgeConfiguration;
32 import org.sonar.db.purge.PurgeDao;
33 import org.sonar.db.purge.PurgeListener;
34 import org.sonar.db.purge.PurgeProfiler;
35 import org.sonar.db.purge.period.DefaultPeriodCleaner;
37 import static org.sonar.db.purge.PurgeConfiguration.newDefaultPurgeConfiguration;
41 public class ProjectCleaner {
42 private static final Logger LOG = Loggers.get(ProjectCleaner.class);
44 private final PurgeProfiler profiler;
45 private final PurgeListener purgeListener;
46 private final PurgeDao purgeDao;
47 private final DefaultPeriodCleaner periodCleaner;
49 public ProjectCleaner(PurgeDao purgeDao, DefaultPeriodCleaner periodCleaner, PurgeProfiler profiler, PurgeListener purgeListener) {
50 this.purgeDao = purgeDao;
51 this.periodCleaner = periodCleaner;
52 this.profiler = profiler;
53 this.purgeListener = purgeListener;
56 public ProjectCleaner purge(DbSession session, String rootUuid, String projectUuid, Configuration projectConfig, Collection<String> disabledComponentUuids) {
57 long start = System.currentTimeMillis();
60 periodCleaner.clean(session, rootUuid, projectConfig);
62 PurgeConfiguration configuration = newDefaultPurgeConfiguration(projectConfig, rootUuid, projectUuid, disabledComponentUuids);
63 purgeDao.purge(session, configuration, purgeListener, profiler);
66 logProfiling(start, projectConfig);
70 private void logProfiling(long start, Configuration config) {
71 if (config.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY).orElse(false)) {
72 long duration = System.currentTimeMillis() - start;
73 LOG.info("\n -------- Profiling for purge: " + TimeUtils.formatDuration(duration) + " --------\n");
74 profiler.dump(duration, LOG);
75 LOG.info("\n -------- End of profiling for purge --------\n");