diff --git a/apps/dolly-backend/src/main/java/no/nav/dolly/DollyBackendLifecycle.java b/apps/dolly-backend/src/main/java/no/nav/dolly/DollyBackendLifecycle.java new file mode 100644 index 00000000000..479c55068ea --- /dev/null +++ b/apps/dolly-backend/src/main/java/no/nav/dolly/DollyBackendLifecycle.java @@ -0,0 +1,53 @@ +package no.nav.dolly; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import no.nav.dolly.repository.BestillingRepository; +import no.nav.dolly.repository.OrganisasjonBestillingRepository; +import org.springframework.context.SmartLifecycle; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Component +@Profile({"dev", "local", "prod"}) +@RequiredArgsConstructor +@Slf4j +public class DollyBackendLifecycle implements SmartLifecycle { + + private final BestillingRepository bestillingRepository; + private final OrganisasjonBestillingRepository organisasjonBestillingRepository; + + private boolean isRunning = false; + + + @Override + public boolean isAutoStartup() { + return true; + } + + @Override + public boolean isRunning() { + return isRunning; + } + + @Override + public int getPhase() { + return 0; + } + + @Override + @Transactional + public void start() { + isRunning = true; + var unfinishedBestilling = bestillingRepository.stopAllUnfinished(); + var unfinishedOrganisasjonBestilling = organisasjonBestillingRepository.stopAllUnfinished(); + log.info("Stoppet {} kjørende bestilling(er), {} kjørende organisasjonsbestilling(er)", unfinishedBestilling, unfinishedOrganisasjonBestilling); + } + + @Override + public void stop() { + isRunning = false; + } + +} diff --git a/apps/dolly-backend/src/main/java/no/nav/dolly/repository/BestillingRepository.java b/apps/dolly-backend/src/main/java/no/nav/dolly/repository/BestillingRepository.java index 746f606fb4c..939f470c0e8 100644 --- a/apps/dolly-backend/src/main/java/no/nav/dolly/repository/BestillingRepository.java +++ b/apps/dolly-backend/src/main/java/no/nav/dolly/repository/BestillingRepository.java @@ -82,4 +82,14 @@ public interface BestillingRepository extends CrudRepository { @Query(value = "select * from bestilling where id = :id for update", nativeQuery = true) Optional findByIdAndLock(@Param("id") Long id); + + @Modifying + @Query(""" + update Bestilling b + set b.ferdig = true, + b.stoppet = true + where b.ferdig = false +""") + int stopAllUnfinished(); + } diff --git a/apps/dolly-backend/src/main/java/no/nav/dolly/repository/OrganisasjonBestillingRepository.java b/apps/dolly-backend/src/main/java/no/nav/dolly/repository/OrganisasjonBestillingRepository.java index f47dfcc8ab6..7e607b3c971 100644 --- a/apps/dolly-backend/src/main/java/no/nav/dolly/repository/OrganisasjonBestillingRepository.java +++ b/apps/dolly-backend/src/main/java/no/nav/dolly/repository/OrganisasjonBestillingRepository.java @@ -16,4 +16,14 @@ public interface OrganisasjonBestillingRepository extends CrudRepository findByBruker(Bruker bruker); + + @Modifying + @Query(""" + update OrganisasjonBestilling ob + set ob.ferdig = true + where ob.ferdig = false +""") + + int stopAllUnfinished(); + } \ No newline at end of file