Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add job for schedule publication #2398

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.security:spring-security-oauth2-resource-server:6.4.1")
implementation("org.jobrunr:jobrunr-spring-boot-3-starter:7.3.2")
kiwikern marked this conversation as resolved.
Show resolved Hide resolved

implementation("org.springframework.cloud:spring-cloud-starter-kubernetes-client-config:3.1.3")

Expand Down Expand Up @@ -198,7 +199,7 @@ dependencies {
// implementation(files("../../neuris-juris-xml-export/build/libs/neuris-juris-xml-export-0.10.19.jar"))
// or with local gradle project (look also into settings.gradle.kts)
// implementation(project(":exporter"))

implementation("de.bund.digitalservice:neuris-caselaw-migration-schema:0.0.29")
// for local development:
// implementation(files("../../ris-data-migration/schema/build/libs/schema-0.0.28.jar"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,12 @@ Slice<DocumentationUnitListItemDTO> searchByDocumentationUnitSearchInputDeviatin
""",
nativeQuery = true)
List<UUID> getRandomDocumentationUnitIds();

@Query(
value =
"""
SELECT documentationUnit FROM DocumentationUnitDTO documentationUnit
WHERE documentationUnit.scheduledPublicationDateTime <= CURRENT_TIMESTAMP
""")
Slice<DocumentationUnitDTO> getScheduledDocumentationUnitsDueNow();
}
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,13 @@ public Map<RelatedDocumentationType, Long> getAllDocumentationUnitWhichLink(
public List<UUID> getRandomDocumentationUnitIds() {
return repository.getRandomDocumentationUnitIds();
}

@Override
@Transactional(transactionManager = "jpaTransactionManager")
public List<DocumentationUnit> getScheduledDocumentationUnitsDueNow() {
return repository
.getScheduledDocumentationUnitsDueNow()
.map(DocumentationUnitTransformer::transformToDomain)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public static DocumentationUnitDTO transformToDTO(
var managementData = updatedDomainObject.managementData();

builder.scheduledPublicationDateTime(managementData.scheduledPublicationDateTime());
builder.lastPublicationDateTime(managementData.lastPublicationDateTime());
}

addReferences(updatedDomainObject, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ Slice<DocumentationUnitListItem> searchByDocumentationUnitSearchInput(
Map<RelatedDocumentationType, Long> getAllDocumentationUnitWhichLink(UUID documentationUnitId);

List<UUID> getRandomDocumentationUnitIds();

/** Returns doc units with a scheduled publication date that is in the past. */
List<DocumentationUnit> getScheduledDocumentationUnitsDueNow();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.bund.digitalservice.ris.caselaw.domain;

import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.jobrunr.jobs.annotations.Job;
import org.jobrunr.jobs.annotations.Recurring;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ScheduledPublicationService {

private final DocumentationUnitRepository repository;

private final HandoverService handoverService;

public ScheduledPublicationService(
DocumentationUnitRepository repository, HandoverService handoverService) {

this.repository = repository;
this.handoverService = handoverService;
}

@Recurring(id = "scheduled-publication-job", interval = "PT1M") // Runs every 1 minute
@Job(name = "Publish scheduled doc units that are due")
public void handoverScheduledDocUnits() {
var scheduledDocUnitsDueNow = this.repository.getScheduledDocumentationUnitsDueNow();
if (!scheduledDocUnitsDueNow.isEmpty()) {
log.info("Publishing {} scheduled doc units due now", scheduledDocUnitsDueNow.size());
}
for (var docUnit : scheduledDocUnitsDueNow) {
// Even if the publication fails, we want to unset the scheduling.
handoverDocument(docUnit);
setPublicationDates(docUnit);
}
}

private void handoverDocument(DocumentationUnit docUnit) {
try {
this.handoverService.handoverDocumentationUnitAsMail(docUnit.uuid(), "[email protected]");
} catch (Exception e) {
log.error(
"Could not publish scheduled doc unit {}, scheduling will still be removed",
docUnit.documentNumber());
}
}

private void setPublicationDates(DocumentationUnit docUnit) {
try {
var updatedDocUnit =
docUnit.toBuilder()
.managementData(
docUnit.managementData().toBuilder()
.scheduledPublicationDateTime(null)
.lastPublicationDateTime(LocalDateTime.now())
.build())
.build();
repository.save(updatedDocUnit);
} catch (Exception e) {
log.error("Could not remove the scheduling for doc unit {}", docUnit.documentNumber());
}
}
}
9 changes: 9 additions & 0 deletions backend/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,12 @@ neuris:
BPatG: MPRE6******YY
BAG: KARE7****YYYY
# TODO: other docoffices

org:
jobrunr:
dashboard:
enabled: false
background-job-server:
enabled: true
job-scheduler:
enabled: true
Loading