3 * Copyright (C) 2009-2022 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.server.pushapi.scheduler.purge;
22 import java.time.Instant;
23 import java.time.temporal.ChronoUnit;
25 import org.sonar.api.Startable;
26 import org.sonar.api.config.Configuration;
27 import org.sonar.api.server.ServerSide;
28 import org.sonar.api.utils.System2;
29 import org.sonar.api.utils.log.Logger;
30 import org.sonar.api.utils.log.Loggers;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.server.util.GlobalLockManager;
35 import static java.util.concurrent.TimeUnit.SECONDS;
38 public class PushEventsPurgeScheduler implements Startable {
39 private static final Logger LOG = Loggers.get(PushEventsPurgeScheduler.class);
40 private static final String LOCK_NAME = "PushPurgeCheck";
41 private static final String INITIAL_DELAY_IN_SECONDS = "sonar.push.events.purge.initial.delay";
42 private static final String ENQUEUE_DELAY_IN_SECONDS = "sonar.push.events.purge.enqueue.delay";
43 private static final int ENQUEUE_LOCK_DELAY_IN_SECONDS = 60;
45 private final DbClient dbClient;
46 private final Configuration config;
47 private final GlobalLockManager lockManager;
48 private final PushEventsPurgeExecutorService executorService;
49 private final System2 system;
51 public PushEventsPurgeScheduler(DbClient dbClient, Configuration config, GlobalLockManager lockManager,
52 PushEventsPurgeExecutorService executorService, System2 system) {
53 this.dbClient = dbClient;
54 this.executorService = executorService;
56 this.lockManager = lockManager;
62 executorService.scheduleAtFixedRate(this::checks, getInitialDelay(),
63 getEnqueueDelay(), SECONDS);
66 private void checks() {
68 // Avoid enqueueing push events purge task multiple times
69 if (!lockManager.tryLock(LOCK_NAME, ENQUEUE_LOCK_DELAY_IN_SECONDS)) {
72 purgeExpiredPushEvents();
73 } catch (Exception e) {
74 LOG.error("Error in Push Events Purge scheduler", e);
78 private void purgeExpiredPushEvents() {
79 try (DbSession dbSession = dbClient.openSession(false)) {
80 Set<String> uuids = dbClient.pushEventDao().selectUuidsOfExpiredEvents(dbSession, getExpiredTimestamp());
81 LOG.debug(String.format("%s push events to be deleted...", uuids.size()));
82 dbClient.pushEventDao().deleteByUuids(dbSession, uuids);
87 public long getInitialDelay() {
88 return config.getLong(INITIAL_DELAY_IN_SECONDS).orElse(60 * 60L);
91 public long getEnqueueDelay() {
92 return config.getLong(ENQUEUE_DELAY_IN_SECONDS).orElse(60 * 60L);
95 private long getExpiredTimestamp() {
96 return Instant.ofEpochMilli(system.now())
97 .minus(1, ChronoUnit.HOURS)