Skip to content

Commit add3ded

Browse files
authored
Merge pull request #19 from redlink-gmbh/fix-jdbc-connection-exception
MORE-Platform#237: Fixed a DB leak
2 parents 8042d4e + 16acdca commit add3ded

1 file changed

Lines changed: 43 additions & 38 deletions

File tree

studymanager-services/src/main/java/io/redlink/more/studymanager/scheduling/ValidateActiveObservationDataCron.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import io.redlink.more.studymanager.core.factory.ObservationFactory;
55
import io.redlink.more.studymanager.core.io.TimeRange;
66
import io.redlink.more.studymanager.core.io.Timeframe;
7-
import io.redlink.more.studymanager.model.*;
7+
import io.redlink.more.studymanager.model.Observation;
8+
import io.redlink.more.studymanager.model.OccurredObservation;
9+
import io.redlink.more.studymanager.model.Participant;
10+
import io.redlink.more.studymanager.model.Study;
811
import io.redlink.more.studymanager.sdk.MoreSDK;
912
import io.redlink.more.studymanager.service.ObservationService;
1013
import io.redlink.more.studymanager.service.OccurredObservationService;
@@ -41,7 +44,7 @@ public ValidateActiveObservationDataCron(
4144
OccurredObservationService occurredObservationService,
4245
ObservationService observationService,
4346
MoreSDK sdk
44-
){
47+
) {
4548
this.studyService = studyService;
4649
this.participantService = participantService;
4750
this.occurredObservationService = occurredObservationService;
@@ -51,8 +54,8 @@ public ValidateActiveObservationDataCron(
5154

5255

5356
//TODO: Make schedules configureable
54-
@Scheduled(cron="30 */5 * * * ?") //every 5min, 30sec after updating active observation data
55-
public void validateActiveObservationData(){
57+
@Scheduled(cron = "30 */5 * * * ?") //every 5min, 30sec after updating active observation data
58+
public void validateActiveObservationData() {
5659
final Instant now = Instant.now();
5760
final LocalTime currentTime = LocalTime.ofInstant(now, ZoneId.systemDefault());
5861
//once every hour validate all occurred observations
@@ -61,25 +64,27 @@ public void validateActiveObservationData(){
6164
final Set<ObservationDataState> validateStates = validateAll
6265
? EnumSet.allOf(ObservationDataState.class)
6366
: EnumSet.complementOf(EnumSet.of(ObservationDataState.COMPLETE));
64-
Map<String, ObservationFactory> observationFactories = new HashMap<>();
65-
studyService.getStudiesByStates(Study.Status.ACTIVE_STATES).forEach(study -> {
66-
//NOTE: OccurredObservations will refer the same observations and participants several times, A single lookup
67-
// per run is sufficient, so we keep them in a cache
68-
Map<Integer, Observation> observationCache = new HashMap<>();
69-
Map<Integer, Participant> participantCache = new HashMap<>();
70-
Map<Integer, io.redlink.more.studymanager.core.component.Observation> observationComponentCache = new HashMap<>();
71-
try (var ooStrem = occurredObservationService.streamOccurredObservations(study.getStudyId(), null, null, validateAll, validateStates)) {
72-
ooStrem
73-
.filter(oo -> validateAll || oo.end().plus(1, ChronoUnit.DAYS).isAfter(now))
74-
.forEach(occurredObservation -> validateOccurrence(
75-
study,
76-
occurredObservation,
77-
observationCache,
78-
observationFactories,
79-
observationComponentCache,
80-
participantCache));
81-
}
82-
});
67+
Map<String, ObservationFactory> observationFactories = new HashMap<>();
68+
try (var stream = studyService.getStudiesByStates(Study.Status.ACTIVE_STATES)) {
69+
stream.forEach(study -> {
70+
//NOTE: OccurredObservations will refer the same observations and participants several times, A single lookup
71+
// per run is sufficient, so we keep them in a cache
72+
Map<Integer, Observation> observationCache = new HashMap<>();
73+
Map<Integer, Participant> participantCache = new HashMap<>();
74+
Map<Integer, io.redlink.more.studymanager.core.component.Observation> observationComponentCache = new HashMap<>();
75+
try (var ooStrem = occurredObservationService.streamOccurredObservations(study.getStudyId(), null, null, validateAll, validateStates)) {
76+
ooStrem
77+
.filter(oo -> validateAll || oo.end().plus(1, ChronoUnit.DAYS).isAfter(now))
78+
.forEach(occurredObservation -> validateOccurrence(
79+
study,
80+
occurredObservation,
81+
observationCache,
82+
observationFactories,
83+
observationComponentCache,
84+
participantCache));
85+
}
86+
});
87+
}
8388
}
8489

8590
private void validateOccurrence(Study study, OccurredObservation occuredObservation, Map<Integer, Observation> observationCache, Map<String, ObservationFactory> observationFactories, Map<Integer, io.redlink.more.studymanager.core.component.Observation> observationComponentCache, Map<Integer, Participant> participantCache) {
@@ -112,17 +117,17 @@ private void validateOccurrence(Study study, OccurredObservation occuredObservat
112117

113118

114119
private void validate(ValidateionContext ctx) {
115-
if(Instant.now().isBefore(ctx.occurrence.start())) {
120+
if (Instant.now().isBefore(ctx.occurrence.start())) {
116121
//do not try to validate occurrences that are in the future
117122
log.warn("Unexpected call to validate {} with a future start timestamp", ctx.occurrence);
118123
return;
119124
}
120125
var validationResults = sdk.validateData(
121-
ctx.getStudyId(), ctx.getStudyGroupId(),ctx.getObservationId(),
122-
ctx.participant.getParticipantId(), ctx.getTimeRage(),
123-
ctx.observationFactory.getMeasurementSet());
126+
ctx.getStudyId(), ctx.getStudyGroupId(), ctx.getObservationId(),
127+
ctx.participant.getParticipantId(), ctx.getTimeRage(),
128+
ctx.observationFactory.getMeasurementSet());
124129
final OccurredObservation updatedOccurredObservation;
125-
if(validationResults != null) {
130+
if (validationResults != null) {
126131
var validationResult = ctx.observationComponent.validateData(ctx.occurrence.start(), ctx.occurrence.end(), validationResults);
127132
log.debug("validated {}: results: {}, state: {}", ctx.occurrence, validationResults, validationResult);
128133
updatedOccurredObservation = new OccurredObservation(
@@ -153,27 +158,27 @@ private void validate(ValidateionContext ctx) {
153158

154159

155160
private record ValidateionContext(
156-
Study study,
157-
OccurredObservation occurrence,
158-
Participant participant,
159-
Observation observation,
160-
ObservationFactory observationFactory,
161-
io.redlink.more.studymanager.core.component.Observation observationComponent
161+
Study study,
162+
OccurredObservation occurrence,
163+
Participant participant,
164+
Observation observation,
165+
ObservationFactory observationFactory,
166+
io.redlink.more.studymanager.core.component.Observation observationComponent
162167
) {
163168

164-
public Long getStudyId(){
169+
public Long getStudyId() {
165170
return study.getStudyId();
166171
}
167172

168-
public Integer getStudyGroupId(){
173+
public Integer getStudyGroupId() {
169174
return observation.getStudyGroupId();
170175
}
171176

172-
public Integer getObservationId(){
177+
public Integer getObservationId() {
173178
return observation.getObservationId();
174179
}
175180

176-
public Integer getParticipantId(){
181+
public Integer getParticipantId() {
177182
return participant.getParticipantId();
178183
}
179184

0 commit comments

Comments
 (0)