return Optional.absent();
}
+ public void deleteIfExists(LogFileRef ref) {
+ File logFile = new File(logsDir, ref.getRelativePath());
+ logFile.delete();
+ }
+
/**
* Initialize logging of a Compute Engine task. Must be called
* before first writing of log.
if (!taskUuid.equals(that.taskUuid)) {
return false;
}
- return !(componentUuid != null ? !componentUuid.equals(that.componentUuid) : that.componentUuid != null);
+ return componentUuid == null ? that.componentUuid == null : componentUuid.equals(that.componentUuid);
}
package org.sonar.server.computation.queue;
import java.util.Calendar;
+import java.util.List;
import org.sonar.api.platform.Server;
import org.sonar.api.platform.ServerStartHandler;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.log.Loggers;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.ce.CeActivityDto;
+import org.sonar.server.computation.log.CeLogging;
+import org.sonar.server.computation.log.LogFileRef;
@ServerSide
public class PurgeCeActivities implements ServerStartHandler {
private final DbClient dbClient;
private final System2 system2;
+ private final CeLogging ceLogging;
- public PurgeCeActivities(DbClient dbClient, System2 system2) {
+ public PurgeCeActivities(DbClient dbClient, System2 system2, CeLogging ceLogging) {
this.dbClient = dbClient;
this.system2 = system2;
+ this.ceLogging = ceLogging;
}
@Override
public void onServerStart(Server server) {
- DbSession dbSession = dbClient.openSession(false);
+ DbSession dbSession = dbClient.openSession(true);
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());
+ LOGGER.info("Delete the Compute Engine tasks created before {}", sixMonthsAgo.getTime());
+ List<CeActivityDto> dtos = dbClient.ceActivityDao().selectOlderThan(dbSession, sixMonthsAgo.getTimeInMillis());
+ for (CeActivityDto dto : dtos) {
+ dbClient.ceActivityDao().deleteByUuid(dbSession, dto.getUuid());
+ ceLogging.deleteIfExists(LogFileRef.from(dto));
+ }
dbSession.commit();
} finally {
import org.sonar.db.DbTester;
import org.sonar.db.ce.CeActivityDto;
import org.sonar.db.ce.CeQueueDto;
+import org.sonar.db.ce.CeTaskTypes;
+import org.sonar.server.computation.log.CeLogging;
+import org.sonar.server.computation.log.LogFileRef;
import org.sonar.server.computation.queue.PurgeCeActivities;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
public class PurgeCeActivitiesTest {
@Rule
public DbTester dbTester = DbTester.create(system2);
- PurgeCeActivities underTest = new PurgeCeActivities(dbTester.getDbClient(), system2);
+ CeLogging ceLogging = mock(CeLogging.class);
+ PurgeCeActivities underTest = new PurgeCeActivities(dbTester.getDbClient(), system2, ceLogging);
@Test
public void delete_older_than_6_months() throws Exception {
assertThat(dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "VERY_OLD").isPresent()).isFalse();
assertThat(dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "RECENT").isPresent()).isTrue();
+ verify(ceLogging).deleteIfExists(new LogFileRef(CeTaskTypes.REPORT, "VERY_OLD", null));
+ verify(ceLogging, never()).deleteIfExists(new LogFileRef(CeTaskTypes.REPORT, "RECENT", null));
}
private void insertWithDate(String uuid, long date) {
CeQueueDto queueDto = new CeQueueDto();
queueDto.setUuid(uuid);
- queueDto.setTaskType("fake");
+ queueDto.setTaskType(CeTaskTypes.REPORT);
CeActivityDto dto = new CeActivityDto(queueDto);
dto.setStatus(CeActivityDto.Status.SUCCESS);
}
}
- public void deleteOlderThan(DbSession dbSession, long beforeDate) {
- mapper(dbSession).deleteOlderThan(beforeDate);
+ public List<CeActivityDto> selectOlderThan(DbSession dbSession, long beforeDate) {
+ return mapper(dbSession).selectOlderThan(beforeDate);
+ }
+
+ public void deleteByUuid(DbSession dbSession, String uuid) {
+ mapper(dbSession).deleteByUuid(uuid);
}
/**
List<CeActivityDto> selectByQuery(@Param("query") CeActivityQuery query, RowBounds rowBounds);
+ List<CeActivityDto> selectOlderThan(@Param("beforeDate") long beforeDate);
+
int countByQuery(@Param("query") CeActivityQuery query);
void insert(CeActivityDto dto);
void updateIsLastToTrueForUuid(@Param("uuid") String uuid, @Param("updatedAt") long updatedAt);
- void deleteOlderThan(@Param("beforeDate") long beforeDate);
+ void deleteByUuid(@Param("uuid") String uuid);
}
*/
package org.sonar.db.ce;
-public class CeTaskTypes {
+public final class CeTaskTypes {
private CeTaskTypes() {
// only statics
</where>
</select>
+ <select id="selectOlderThan" parameterType="long" resultType="org.sonar.db.ce.CeActivityDto">
+ select <include refid="columns"/>
+ from ce_activity ca
+ where ca.created_at < #{beforeDate,jdbcType=BIGINT}
+ </select>
+
<insert id="insert" parameterType="org.sonar.db.ce.CeActivityDto" useGeneratedKeys="false">
insert into ce_activity
(uuid, component_uuid, status, task_type, is_last, is_last_key, submitter_login, submitted_at, started_at,
where uuid=#{uuid}
</update>
- <delete id="deleteOlderThan" parameterType="long">
+ <delete id="deleteByUuid" parameterType="string">
delete from ce_activity
- where created_at < #{beforeDate,jdbcType=BIGINT}
+ where uuid=#{uuid}
</delete>
-
</mapper>
}
@Test
- public void test_selectByQuery() throws Exception {
+ public void test_selectByQuery() {
insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS);
insert("TASK_2", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED);
insert("TASK_3", REPORT, "PROJECT_2", CeActivityDto.Status.SUCCESS);
}
@Test
- public void test_countByQuery() throws Exception {
+ public void test_countByQuery() {
insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS);
insert("TASK_2", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED);
insert("TASK_3", REPORT, "PROJECT_2", CeActivityDto.Status.SUCCESS);
}
@Test
- public void select_and_count_by_date() throws Exception {
+ public void select_and_count_by_date() {
insertWithDates("UUID1", 1_450_000_000_000L, 1_470_000_000_000L);
insertWithDates("UUID2", 1_460_000_000_000L, 1_480_000_000_000L);
}
@Test
- public void deleteOlderThan() throws Exception {
+ public void selectOlderThan() {
insertWithCreationDate("TASK_1", 1_450_000_000_000L);
insertWithCreationDate("TASK_2", 1_460_000_000_000L);
insertWithCreationDate("TASK_3", 1_470_000_000_000L);
- underTest.deleteOlderThan(db.getSession(), 1_465_000_000_000L);
- db.getSession().commit();
+ List<CeActivityDto> dtos = underTest.selectOlderThan(db.getSession(), 1_465_000_000_000L);
+ assertThat(dtos).extracting("uuid").containsOnly("TASK_1", "TASK_2");
+ }
+
+ @Test
+ public void deleteByUuid() {
+ insert("TASK_1", "REPORT", "COMPONENT1", CeActivityDto.Status.SUCCESS);
+ insert("TASK_2", "REPORT", "COMPONENT1", CeActivityDto.Status.SUCCESS);
+ underTest.deleteByUuid(db.getSession(), "TASK_1");
assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").isPresent()).isFalse();
- assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").isPresent()).isFalse();
- assertThat(underTest.selectByUuid(db.getSession(), "TASK_3").isPresent()).isTrue();
+ assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").isPresent()).isTrue();
+ }
+
+ @Test
+ public void deleteByUuid_does_nothing_if_uuid_does_not_exist() {
+ insert("TASK_1", "REPORT", "COMPONENT1", CeActivityDto.Status.SUCCESS);
+
+ // must not fail
+ underTest.deleteByUuid(db.getSession(), "TASK_2");
+
+ assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").isPresent()).isTrue();
}
private void insert(String uuid, String type, String componentUuid, CeActivityDto.Status status) {