Skip to content

Commit

Permalink
Legger til job-scheduler logikk for initiell job-scheduling + cron-ex…
Browse files Browse the repository at this point in the history
…pression formatering
  • Loading branch information
ulrikHesmyr committed Jul 22, 2024
1 parent 51bd452 commit 73e198d
Showing 1 changed file with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.registre.testnorge.levendearbeidsforholdansettelse.entity.JobbParameterEntity;
import no.nav.registre.testnorge.levendearbeidsforholdansettelse.service.JobbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.List;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Component
@EnableScheduling
@RequiredArgsConstructor
public class JobScheduler {

private final JobbService jobbService;

@Autowired
private final TaskScheduler taskScheduler;

private ScheduledFuture<?> scheduledFuture;

public JobScheduler(TaskScheduler taskScheduler) {
this.taskScheduler = taskScheduler;
}
private String intervall = "1";

/**
* Funksjon som henter ut en intervallet (Som skal være en verdi med gyldig cron-expression) fra databasen.
Expand All @@ -35,23 +37,39 @@ public JobScheduler(TaskScheduler taskScheduler) {
@EventListener(ApplicationReadyEvent.class)
public void scheduleTask(){
//Hent ut intervall fra databasen, eller sett default-verdi
log.info("Alle parametere: {}", jobbService.hentAlleParametere().toString());

List<JobbParameterEntity> parametere = jobbService.hentAlleParametere();
parametere.forEach(param -> {
if(param.getNavn().equals("intervall")){
log.info("Parameter-verdi for intervall: {}", param.getVerdi());
String nyttIntervall = param.getVerdi();
if (!intervall.equals(nyttIntervall)) {
this.intervall = nyttIntervall;
}

log.info("Schedulet en task!");
rescheduleTask("0 * * ? * MON-FRI");
rescheduleTask(intervall);
}
});
}

/**
* Avslutter nåværende schedule og starter en ny med det nye intervallet.
* Forutsetning at parameter allerede er på riktig cron-expression format.
* @param cronExpression Det nye intervallet
* @param intervallet Heltall som representerer antall timer forsinkelse
*/
public void rescheduleTask(String cronExpression){
public void rescheduleTask(String intervallet){
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
}
String cronExpression = lagCronExpression(intervallet);
scheduledFuture = taskScheduler.schedule(new Job(), new CronTrigger(cronExpression));
log.info("Schedulet en task med intervall: {}", cronExpression);
}

/**
* Jobb som kan kjøres når den blir kalt på av en scheduler
*/
private static class Job implements Runnable {

@Override
Expand All @@ -60,4 +78,13 @@ public void run() {
log.info("Jobb kjørte! Holder på å ansette folk nå!");
}
}

/**
* Formatterer en cron-expression for å kjøre en jobb hver x. time
* @param intervallet Heltall som representerer antall timer forsinkelse
* @return Ferdig formattert og gyldig cron-expression
*/
private String lagCronExpression(String intervallet) {
return "0 */" + intervallet + " * ? * MON-FRI";
}
}

0 comments on commit 73e198d

Please sign in to comment.