Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit d1914a8

Browse files
authored
Feat/add logging and locking (#111)
* added logging * added shedlock
1 parent ac78a0c commit d1914a8

9 files changed

Lines changed: 64 additions & 9 deletions

File tree

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@
184184
<artifactId>rxjava</artifactId>
185185
<version>1.3.8</version>
186186
</dependency>
187+
<dependency>
188+
<groupId>net.javacrumbs.shedlock</groupId>
189+
<artifactId>shedlock-spring</artifactId>
190+
<version>4.25.0</version>
191+
</dependency>
192+
<dependency>
193+
<groupId>net.javacrumbs.shedlock</groupId>
194+
<artifactId>shedlock-provider-jdbc-template</artifactId>
195+
<version>4.25.0</version>
196+
</dependency>
187197
</dependencies>
188198

189199
<build>

src/main/java/app/coronawarn/testresult/TestResultCleanup.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.transaction.Transactional;
88
import lombok.RequiredArgsConstructor;
99
import lombok.extern.slf4j.Slf4j;
10+
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
1011
import org.springframework.scheduling.annotation.Scheduled;
1112
import org.springframework.stereotype.Component;
1213

@@ -22,8 +23,10 @@ public class TestResultCleanup {
2223
* All test results that are older than configured days should be marked as redeemed.
2324
*/
2425
@Scheduled(
25-
fixedDelayString = "${testresult.cleanup.redeem.rate}"
26+
cron = "${testresult.cleanup.redeem.cron}"
2627
)
28+
@SchedulerLock(name = "TestresultCleanupService_redeem", lockAtLeastFor = "PT0S",
29+
lockAtMostFor = "${testresult.cleanup.redeem.locklimit}")
2730
@Transactional
2831
public void redeem() {
2932
Integer redeemed = testResultRepository.updateResultByCreatedAtBefore(
@@ -36,8 +39,10 @@ public void redeem() {
3639
* All test results that are older than configured days should get deleted.
3740
*/
3841
@Scheduled(
39-
fixedDelayString = "${testresult.cleanup.delete.rate}"
42+
cron = "${testresult.cleanup.delete.cron}"
4043
)
44+
@SchedulerLock(name = "TestresultCleanupService_delete", lockAtLeastFor = "PT0S",
45+
lockAtMostFor = "${testresult.cleanup.delete.locklimit}")
4146
@Transactional
4247
public void delete() {
4348
Integer deleted = testResultRepository.deleteByCreatedAtBefore(

src/main/java/app/coronawarn/testresult/config/MtlsSecurityConfig.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ protected void configure(HttpSecurity http) throws Exception {
7878
@Override
7979
public UserDetailsService userDetailsService() {
8080
return hash -> {
81-
8281
boolean allowed = Stream.of(testResultConfig.getAllowedClientCertificates()
8382
.split(","))
8483
.map(String::trim)
@@ -103,8 +102,11 @@ private ThumbprintX509PrincipalExtractor() throws NoSuchAlgorithmException {
103102

104103
@Override
105104
public Object extractPrincipal(X509Certificate x509Certificate) {
105+
106106
try {
107-
return String.valueOf(Hex.encode(messageDigest.digest(x509Certificate.getEncoded())));
107+
String ret = String.valueOf(Hex.encode(messageDigest.digest(x509Certificate.getEncoded())));
108+
log.debug("Accessed by Subject {} Hash {}",x509Certificate.getSubjectDN().getName(), ret);
109+
return ret;
108110
} catch (CertificateEncodingException e) {
109111
log.error("Failed to extract bytes from certificate");
110112
return null;

src/main/java/app/coronawarn/testresult/config/TestResultConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static class Cleanup {
2828
@Setter
2929
public static class Scheduled {
3030

31-
private Integer days;
31+
private int days;
3232

3333
}
3434

src/main/resources/application.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ testresult:
4848
cleanup:
4949
redeem:
5050
days: 21
51-
rate: 3600000
51+
cron: 0 1 * * * *
52+
locklimit: 600
5253
delete:
5354
days: 60
54-
rate: 3600000
55+
cron: 0 0 * * * *
56+
locklimit: 600
5557
allowed-client-certificates:

src/main/resources/db/changelog.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ databaseChangeLog:
1111
- include:
1212
file: changelog/v003-add-labid-column.yml
1313
relativeToChangelogFile: true
14+
- include:
15+
file: changelog/v004-add-shedlock.yml
16+
relativeToChangelogFile: true
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
databaseChangeLog:
2+
- changeSet:
3+
id: add-shedlock
4+
author: mschulte-tsi
5+
changes:
6+
- createTable:
7+
tableName: shedlock
8+
columns:
9+
- column:
10+
name: name
11+
type: varchar(64)
12+
constraints:
13+
nullable: false
14+
primaryKey: true
15+
- column:
16+
name: lock_until
17+
type: datetime(2)
18+
constraints:
19+
nullable: false
20+
- column:
21+
name: locked_at
22+
type: datetime(2)
23+
constraints:
24+
nullable: false
25+
- column:
26+
name: locked_by
27+
type: varchar(255)
28+
constraints:
29+
nullable: false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package app.coronawarn.testresult;
2+
3+
public @interface SchedulerLock {
4+
}

src/test/java/app/coronawarn/testresult/TestResultCleanupTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
@SpringBootTest(
2121
properties = {
2222
"testresult.cleanup.redeem.days=21",
23-
"testresult.cleanup.redeem.rate=1000",
23+
"testresult.cleanup.redeem.cron=* * * * * *",
2424
"testresult.cleanup.delete.days=60",
25-
"testresult.cleanup.delete.rate=1000"
25+
"testresult.cleanup.delete.cron=* * * * * *"
2626
}
2727
)
2828
@ContextConfiguration(classes = TestResultApplication.class)

0 commit comments

Comments
 (0)