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

Commit

Permalink
Feat/add logging and locking (#111)
Browse files Browse the repository at this point in the history
* added logging

* added shedlock
  • Loading branch information
mschulte-tsi authored Dec 3, 2021
1 parent ac78a0c commit d1914a8
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 9 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@
<artifactId>rxjava</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>4.25.0</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>4.25.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -22,8 +23,10 @@ public class TestResultCleanup {
* All test results that are older than configured days should be marked as redeemed.
*/
@Scheduled(
fixedDelayString = "${testresult.cleanup.redeem.rate}"
cron = "${testresult.cleanup.redeem.cron}"
)
@SchedulerLock(name = "TestresultCleanupService_redeem", lockAtLeastFor = "PT0S",
lockAtMostFor = "${testresult.cleanup.redeem.locklimit}")
@Transactional
public void redeem() {
Integer redeemed = testResultRepository.updateResultByCreatedAtBefore(
Expand All @@ -36,8 +39,10 @@ public void redeem() {
* All test results that are older than configured days should get deleted.
*/
@Scheduled(
fixedDelayString = "${testresult.cleanup.delete.rate}"
cron = "${testresult.cleanup.delete.cron}"
)
@SchedulerLock(name = "TestresultCleanupService_delete", lockAtLeastFor = "PT0S",
lockAtMostFor = "${testresult.cleanup.delete.locklimit}")
@Transactional
public void delete() {
Integer deleted = testResultRepository.deleteByCreatedAtBefore(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ protected void configure(HttpSecurity http) throws Exception {
@Override
public UserDetailsService userDetailsService() {
return hash -> {

boolean allowed = Stream.of(testResultConfig.getAllowedClientCertificates()
.split(","))
.map(String::trim)
Expand All @@ -103,8 +102,11 @@ private ThumbprintX509PrincipalExtractor() throws NoSuchAlgorithmException {

@Override
public Object extractPrincipal(X509Certificate x509Certificate) {

try {
return String.valueOf(Hex.encode(messageDigest.digest(x509Certificate.getEncoded())));
String ret = String.valueOf(Hex.encode(messageDigest.digest(x509Certificate.getEncoded())));
log.debug("Accessed by Subject {} Hash {}",x509Certificate.getSubjectDN().getName(), ret);
return ret;
} catch (CertificateEncodingException e) {
log.error("Failed to extract bytes from certificate");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class Cleanup {
@Setter
public static class Scheduled {

private Integer days;
private int days;

}

Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ testresult:
cleanup:
redeem:
days: 21
rate: 3600000
cron: 0 1 * * * *
locklimit: 600
delete:
days: 60
rate: 3600000
cron: 0 0 * * * *
locklimit: 600
allowed-client-certificates:
3 changes: 3 additions & 0 deletions src/main/resources/db/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ databaseChangeLog:
- include:
file: changelog/v003-add-labid-column.yml
relativeToChangelogFile: true
- include:
file: changelog/v004-add-shedlock.yml
relativeToChangelogFile: true
29 changes: 29 additions & 0 deletions src/main/resources/db/changelog/v004-add-shedlock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
databaseChangeLog:
- changeSet:
id: add-shedlock
author: mschulte-tsi
changes:
- createTable:
tableName: shedlock
columns:
- column:
name: name
type: varchar(64)
constraints:
nullable: false
primaryKey: true
- column:
name: lock_until
type: datetime(2)
constraints:
nullable: false
- column:
name: locked_at
type: datetime(2)
constraints:
nullable: false
- column:
name: locked_by
type: varchar(255)
constraints:
nullable: false
4 changes: 4 additions & 0 deletions src/test/java/app/coronawarn/testresult/SchedulerLock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package app.coronawarn.testresult;

public @interface SchedulerLock {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
@SpringBootTest(
properties = {
"testresult.cleanup.redeem.days=21",
"testresult.cleanup.redeem.rate=1000",
"testresult.cleanup.redeem.cron=* * * * * *",
"testresult.cleanup.delete.days=60",
"testresult.cleanup.delete.rate=1000"
"testresult.cleanup.delete.cron=* * * * * *"
}
)
@ContextConfiguration(classes = TestResultApplication.class)
Expand Down

0 comments on commit d1914a8

Please sign in to comment.