Skip to content

Commit

Permalink
bestillinger_orphans (#3608)
Browse files Browse the repository at this point in the history
Ny lifecycle bean for å stoppe bestillinger.
  • Loading branch information
rfc3092 authored Sep 9, 2024
1 parent 751674c commit 5dc7b23
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,14 @@ public interface BestillingRepository extends CrudRepository<Bestilling, Long> {

@Query(value = "select * from bestilling where id = :id for update", nativeQuery = true)
Optional<Bestilling> findByIdAndLock(@Param("id") Long id);

@Modifying
@Query("""
update Bestilling b
set b.ferdig = true,
b.stoppet = true
where b.ferdig = false
""")
int stopAllUnfinished();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ public interface OrganisasjonBestillingRepository extends CrudRepository<Organis
int deleteBestillingWithNoChildren(@Param("bestilling") OrganisasjonBestilling bestilling);

List<OrganisasjonBestilling> findByBruker(Bruker bruker);

@Modifying
@Query("""
update OrganisasjonBestilling ob
set ob.ferdig = true
where ob.ferdig = false
""")

int stopAllUnfinished();

}

0 comments on commit 5dc7b23

Please sign in to comment.