Skip to content

Commit

Permalink
EPMRPP-88632 || Update checks in CleanLogJob and CleanLaunchJob
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKustau committed Mar 18, 2024
1 parent 83a1dee commit d7c9f08
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CleanLaunchJob extends BaseCleanJob {
private static final String PROJECT_ID_PARAM = "projectId";
private static final String START_TIME_PARAM = "startTime";
private static final String SELECT_LAUNCH_ID_QUERY =
"SELECT id FROM launch WHERE project_id = :projectId AND start_time <= :startTime::TIMESTAMP AND important IS TRUE;";
"SELECT id FROM launch WHERE project_id = :projectId AND start_time <= :startTime::TIMESTAMP AND important IS FALSE;";
private static final String DELETE_CLUSTER_QUERY =
"DELETE FROM clusters WHERE clusters.launch_id IN (:ids);";
private static final String DELETE_LAUNCH_QUERY = "DELETE FROM launch WHERE id IN (:ids);";
Expand Down
61 changes: 35 additions & 26 deletions src/main/java/com/epam/reportportal/jobs/clean/CleanLogJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ public class CleanLogJob extends BaseCleanJob {
private static final String PROJECT_ID_PARAM = "projectId";
private static final String START_TIME_PARAM = "startTime";

private static final String DELETE_LOGS_QUERY = "DELETE FROM log WHERE project_id = ? AND log_time <= ?::TIMESTAMP;";
private static final String SELECT_LAUNCH_ID_QUERY = "SELECT id FROM launch WHERE project_id = :projectId AND start_time <= :startTime::TIMESTAMP;";
private static final String DELETE_LOGS_QUERY = """
DELETE FROM log
WHERE log.project_id = ? AND log.log_time <= ?::TIMESTAMP
AND COALESCE(log.launch_id,
(SELECT test_item.launch_id FROM test_item WHERE test_item.item_id = log.item_id),
(SELECT test_item.launch_id FROM test_item WHERE test_item.item_id =
(SELECT ti.retry_of FROM test_item ti WHERE ti.item_id = log.item_id)
)
) IN (SELECT launch.id FROM launch WHERE launch.important = FALSE);
""";
private static final String SELECT_LAUNCH_ID_QUERY =
"SELECT id FROM launch WHERE project_id = :projectId AND start_time <= :startTime::TIMESTAMP;";

private final CleanAttachmentJob cleanAttachmentJob;
private final IndexerServiceClient indexerServiceClient;
Expand All @@ -45,34 +55,34 @@ public CleanLogJob(JdbcTemplate jdbcTemplate, CleanAttachmentJob cleanAttachment
}

@Override
@Scheduled(cron = "${rp.environment.variable.clean.log.cron}")
@SchedulerLock(name = "cleanLog", lockAtMostFor = "24h")
public void execute() {
removeLogs();
cleanAttachmentJob.moveAttachments();
}
@Scheduled(cron = "${rp.environment.variable.clean.log.cron}")
@SchedulerLock(name = "cleanLog", lockAtMostFor = "24h")
public void execute() {
removeLogs();
cleanAttachmentJob.moveAttachments();
}

void removeLogs() {
AtomicInteger counter = new AtomicInteger(0);
// TODO: Need to refactor Logs to keep real it's launchId and combine code with
// CleanLaunch to avoid duplication
getProjectsWithAttribute(KEEP_LOGS).forEach((projectId, duration) -> {
final LocalDateTime lessThanDate = LocalDateTime.now(ZoneOffset.UTC).minus(duration);
int deleted = jdbcTemplate.update(DELETE_LOGS_QUERY, projectId, lessThanDate);
counter.addAndGet(deleted);
LOGGER.info("Delete {} logs for project {}", deleted, projectId);
// to avoid error message in analyzer log, doesn't find index
if (deleted > 0) {
indexerServiceClient.removeFromIndexLessThanLogDate(projectId, lessThanDate);
LOGGER.info("Send message for deletion to analyzer for project {}", projectId);
void removeLogs() {
AtomicInteger counter = new AtomicInteger(0);
// TODO: Need to refactor Logs to keep real it's launchId and combine code with
// CleanLaunch to avoid duplication
getProjectsWithAttribute(KEEP_LOGS).forEach((projectId, duration) -> {
final LocalDateTime lessThanDate = LocalDateTime.now(ZoneOffset.UTC).minus(duration);
int deleted = jdbcTemplate.update(DELETE_LOGS_QUERY, projectId, lessThanDate);
counter.addAndGet(deleted);
LOGGER.info("Delete {} logs for project {}", deleted, projectId);
// to avoid error message in analyzer log, doesn't find index
if (deleted > 0) {
indexerServiceClient.removeFromIndexLessThanLogDate(projectId, lessThanDate);
LOGGER.info("Send message for deletion to analyzer for project {}", projectId);

final List<Long> launchIds = getLaunchIds(projectId, lessThanDate);
if (!launchIds.isEmpty()) {
deleteLogsFromSearchEngineByLaunchIdsAndProjectId(launchIds, projectId);
}
}
});
}
}
});
}

private void deleteLogsFromSearchEngineByLaunchIdsAndProjectId(List<Long> launchIds,
Long projectId) {
Expand All @@ -84,8 +94,7 @@ private void deleteLogsFromSearchEngineByLaunchIdsAndProjectId(List<Long> launch

private List<Long> getLaunchIds(Long projectId, LocalDateTime lessThanDate) {
return namedParameterJdbcTemplate.queryForList(SELECT_LAUNCH_ID_QUERY,
Map.of(PROJECT_ID_PARAM, projectId, START_TIME_PARAM, lessThanDate),
Long.class
Map.of(PROJECT_ID_PARAM, projectId, START_TIME_PARAM, lessThanDate), Long.class
);
}
}

0 comments on commit d7c9f08

Please sign in to comment.