Skip to content

Commit

Permalink
Introduce delay in processing elements
Browse files Browse the repository at this point in the history
#deploy-levende-arbeidsforhold-ansettelse

Added a delay of one minute between processing each element within the 'opprettAnsettelse' method to prevent overwhelming external services. This change aims to enhance system stability and prevent any rate-limiting issues.
  • Loading branch information
krharum committed Oct 16, 2024
1 parent 5319434 commit 69331eb
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;

import java.security.SecureRandom;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Random;
Expand All @@ -30,13 +32,16 @@
import static no.nav.testnav.levendearbeidsforholdansettelse.entity.JobbParameterNavn.ANTALL_PERSONER;
import static no.nav.testnav.levendearbeidsforholdansettelse.entity.JobbParameterNavn.ARBEIDSFORHOLD_TYPE;
import static no.nav.testnav.levendearbeidsforholdansettelse.entity.JobbParameterNavn.STILLINGSPROSENT;
import static org.apache.commons.lang3.BooleanUtils.isTrue;
import static org.apache.commons.lang3.StringUtils.isNumeric;

@Slf4j
@Service
@RequiredArgsConstructor
public class AnsettelseService {

private static final Random RANDOM = new SecureRandom();

private final PdlService pdlService;
private final TenorConsumer tenorConsumer;
private final ArbeidsforholdService arbeidsforholdService;
Expand All @@ -48,7 +53,6 @@ public class AnsettelseService {
@Transactional
public void runAnsettelseService() {


var startTime = System.currentTimeMillis();

//Henter parametere fra db
Expand Down Expand Up @@ -86,7 +90,8 @@ private Flux<AnsettelseLogg> sjekkOgSendinnArbeidsforhold(OrganisasjonResponseDT
.doOnNext(status -> log.info("Opprettet arbeidsforhold orgnummer {}, ident {}, status {}",
kanAnsettes.getOrgnummer(), kanAnsettes.getIdent(), status))
.map(status ->
ansettelseLoggService.lagreAnsettelse(kanAnsettes, parametere)))));
ansettelseLoggService.lagreAnsettelse(kanAnsettes, parametere))))
.delayElements(Duration.ofMinutes(1)));
}

private Flux<KanAnsettesDTO> getPersonSomKanAnsettes(Integer stillingsprosent, DatoIntervall intervall,
Expand All @@ -103,7 +108,7 @@ private Flux<KanAnsettesDTO> getPersonSomKanAnsettes(Integer stillingsprosent, D
.reduce(0, (a, b) -> (int) (a + b))
.map(sum -> sum + stillingsprosent <= 100)
.map(done -> {
if (done) {
if (isTrue(done)) {
return Flux.just(KanAnsettesDTO.builder()
.ident(arbeidsforhold1.getIdent())
.orgnummer(organisasjon.getOrgnummer())
Expand Down Expand Up @@ -147,13 +152,9 @@ private static int getAntallAnsettelserIHverOrg(Map<String, String> parametere)
(int) getParameterValue(parametere, ANTALL_ORGANISASJONER);
}

private int tilfeldigTall(int max) {
Random random = new Random();
return random.nextInt(max);
}

private String hentTilfeldigYrkeskode(List<String> yrkeskoder) {
return yrkeskoder.get(tilfeldigTall(yrkeskoder.size()));

return yrkeskoder.get(RANDOM.nextInt(yrkeskoder.size()));
}

private static Object getParameterValue(Map<String, String> parametere, JobbParameterNavn parameterNavn) {
Expand Down

0 comments on commit 69331eb

Please sign in to comment.