Skip to content

Commit

Permalink
feat: scheduler to delete expired ini audit
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaDel03 committed Oct 3, 2024
1 parent 5f22707 commit 35a7392
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static final class Collections {
public static final String TRANSACTION_DATA = "transaction_data";

public static final String INI_EDS_INVOCATION = "ini_eds_invocation";
public static final String AUDIT_INI = "audit_ini";

public static final String VALIDATED_DOCUMENTS = "validated_documents";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.finanze.sanita.fse2.ms.gtw.garbage.enums;

public enum IniEventType {
INI_CREATE_SOAP,
INI_REPLACE_SOAP,
INI_UPDATE_SOAP,
INI_DELETE_SOAP,
INI_RIFERIMENTO_SOAP,
INI_GET_METADATI_SOAP

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public String getDictionaryCollection() {
}
return Constants.Collections.DICTIONARY;
}

@Bean("auditIni")
public String getAuditIniCollection() {
if (profileUtility.isTestProfile()) {
return Constants.Profile.TEST_PREFIX + Constants.Collections.AUDIT_INI;
}
return Constants.Collections.AUDIT_INI;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package it.finanze.sanita.fse2.ms.gtw.garbage.repository;

import java.util.Date;

public interface IAuditIniRetentionRepo {

void deleteExpiredAudit(Date date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package it.finanze.sanita.fse2.ms.gtw.garbage.repository.entity;

import it.finanze.sanita.fse2.ms.gtw.garbage.enums.IniEventType;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.util.Date;

@Document(collection = "#{@auditIni}")
@Data
public class AuditIniETY {

public static final String WORKFLOW_INSTANCE_ID = "workflow_instance_id";
public static final String EVENT_TYPE = "eventType";
public static final String EVENT_DATE = "eventDate";
public static final String MICROSERVICE_NAME = "microserviceName";
public static final String SOAP_REQUEST = "soapRequest";
public static final String SOAP_RESPONSE = "soapResponse";
public static final String EXPIRING_DATE = "expiring_date";

@Id
private String id;

@Field(name = WORKFLOW_INSTANCE_ID)
private final String workflowInstanceId;

@Field(name = EVENT_TYPE)
private final IniEventType eventType;

@Field(name = EVENT_DATE)
private final Date eventDate;

@Field(name = SOAP_REQUEST)
private String soapRequest;

@Field(name = SOAP_RESPONSE)
private String soapResponse;

@Field(name = MICROSERVICE_NAME)
private String microserviceName;

@Field(name = EXPIRING_DATE)
private Date expiringDate;

public AuditIniETY(String workflowInstanceId, IniEventType eventType, Date eventDate, String soapRequest, String soapResponse){
this.workflowInstanceId = workflowInstanceId;
this.eventType = eventType;
this.eventDate = eventDate;
this.soapRequest = soapRequest;
this.soapResponse = soapResponse;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package it.finanze.sanita.fse2.ms.gtw.garbage.repository.impl;

import it.finanze.sanita.fse2.ms.gtw.garbage.repository.IAuditIniRetentionRepo;
import it.finanze.sanita.fse2.ms.gtw.garbage.repository.entity.AuditIniETY;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
@Slf4j
public class AuditIniRetentionRepo implements IAuditIniRetentionRepo {

@Autowired
private MongoTemplate mongoTemplate;

@Override
public void deleteExpiredAudit(Date date) {
Pageable pageable = PageRequest.of(0, 100);
Query query = new Query(Criteria.where(AuditIniETY.EXPIRING_DATE).lte(date)).with(pageable);

List<AuditIniETY> audits;
int totalDeleted=0;

do {
audits = mongoTemplate.find(query, AuditIniETY.class);

int deletedInBatch=audits.size();
totalDeleted += deletedInBatch;

if (!audits.isEmpty()) {
mongoTemplate.remove(query, AuditIniETY.class);
}

pageable = pageable.next();
query.with(pageable);

log.debug("{} records has been deleted in the current batch", deletedInBatch);

} while (!audits.isEmpty());

log.debug("Total records deleted: {}", totalDeleted);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package it.finanze.sanita.fse2.ms.gtw.garbage.scheduler;

import it.finanze.sanita.fse2.ms.gtw.garbage.service.IAuditIniRetentionSRV;
import it.finanze.sanita.fse2.ms.gtw.garbage.service.IConfigSRV;
import it.finanze.sanita.fse2.ms.gtw.garbage.utility.DateUtility;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Slf4j
@Component
public class AuditIniRetentionScheduler {

@Autowired
private IAuditIniRetentionSRV auditIniRetentionSRV;

@Autowired
private IConfigSRV configSRV;

@Scheduled(cron = "${scheduler.audit.ini-retention}")
@SchedulerLock(name = "invokeRulesRetentionScheduler", lockAtMostFor = "60m")
public void action() {
run();
}

public void run() {
log.debug("Audit-ini Retention Scheduler - Retention Scheduler starting");
try {
log.debug("Checking for expired ini audit...");
auditIniRetentionSRV.deleteAudit(new Date());
} catch (Exception e) {
log.warn("Audit-ini Scheduler - Error while executing audit-ini data retention", e);
}
log.debug("Audit-ini Retention Scheduler - Retention Scheduler finished");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package it.finanze.sanita.fse2.ms.gtw.garbage.service;

import java.util.Date;

public interface IAuditIniRetentionSRV {

void deleteAudit(Date date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package it.finanze.sanita.fse2.ms.gtw.garbage.service.impl;

import it.finanze.sanita.fse2.ms.gtw.garbage.repository.IAuditIniRetentionRepo;
import it.finanze.sanita.fse2.ms.gtw.garbage.service.IAuditIniRetentionSRV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AuditIniRetentionSRV implements IAuditIniRetentionSRV{

@Autowired
IAuditIniRetentionRepo repo;

@Override
public void deleteAudit(Date date) {
repo.deleteExpiredAudit(date);
}
}

0 comments on commit 35a7392

Please sign in to comment.