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

Commit

Permalink
Feat/Add Shedlock (#252)
Browse files Browse the repository at this point in the history
* Added Shedlock
Moved Delete to DB
Changed Job to Cron
Added index

* removed rate
  • Loading branch information
mschulte-tsi authored Dec 9, 2021
1 parent 9013d1c commit 96c1e82
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 3 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,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 @@ -136,6 +136,8 @@ public static class Entities {
public static class Cleanup {

private Integer days = 21;
private String cron = "0 1 * * * *";
private Integer locklimit = 60;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.time.LocalDateTime;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
* This class represents the AppSession repository.
Expand Down Expand Up @@ -61,5 +63,7 @@ public interface VerificationAppSessionRepository extends JpaRepository<Verifica
*
* @param before the Date to delete by
*/
@Modifying
@Query("delete from VerificationAppSession a where a.createdAt < ?1")
void deleteByCreatedAtBefore(LocalDateTime before);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.time.LocalDateTime;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
* This class represents the Tan repository.
Expand Down Expand Up @@ -53,6 +55,8 @@ public interface VerificationTanRepository extends JpaRepository<VerificationTan
*
* @param before LocalDateTime to delete older entities
*/
@Modifying
@Query("delete from VerificationTan a where a.createdAt < ?1")
void deleteByCreatedAtBefore(LocalDateTime before);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 @@ -48,8 +49,10 @@ public class EntitiesCleanupService {
* All entities that are older than configured days get deleted.
*/
@Scheduled(
fixedDelayString = "${entities.cleanup.rate}"
cron = "${entities.cleanup.cron}"
)
@SchedulerLock(name = "VerificationCleanupService_cleanup", lockAtLeastFor = "PT0S",
lockAtMostFor = "${entities.cleanup.locklimit}")
@Transactional
public void cleanup() {
log.info("cleanup execution");
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ appsession:
tancountermax: 1
entities:
cleanup:
cron: "0 1 * * * *"
days: 21
rate: 3600000
initialFakeDelayMilliseconds: 10
fakeDelayMovingAverageSamples: 5
request:
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/db/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ databaseChangeLog:
- include:
file: changelog/v004-add-unique-registration-token-teletan.yml
relativeToChangelogFile: true
- include:
file: changelog/v005-add-shedlock.yml
relativeToChangelogFile: true
- include:
file: changelog/v006-add-index-dob-hashed-guid.yml
relativeToChangelogFile: true
29 changes: 29 additions & 0 deletions src/main/resources/db/changelog/v005-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
12 changes: 12 additions & 0 deletions src/main/resources/db/changelog/v006-add-index-dob-hashed-guid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
databaseChangeLog:
- changeSet:
id: add-unique-hashed-guid
author: mschulte-tsi
changes:
- createIndex:
tableName: app_session
indexName: idx_app_session_hashed_guid_dob
columns:
- column:
name: hashed_guid_dob
type: varchar(64)
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
@ActiveProfiles("local")
@SpringBootTest(
properties = {
"entities.cleanup.rate=1000"
"entities.cleanup.cron=* * * * * *"
}
)
@ContextConfiguration(classes = VerificationApplication.class)
Expand Down

0 comments on commit 96c1e82

Please sign in to comment.