Skip to content

Commit

Permalink
Merge branch '32-scheduler-ansettelse-consumer' into feature/levende-…
Browse files Browse the repository at this point in the history
…arbeidsforhold
  • Loading branch information
ulrikHesmyr committed Jul 30, 2024
2 parents 62dd39a + b03665a commit d13eee5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@Configuration
public class SchedulerConfig {

@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.setThreadNamePrefix("taskScheduler-");
return taskScheduler;
public ScheduledExecutorService taskScheduler() {
return Executors.newScheduledThreadPool(4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
import lombok.extern.slf4j.Slf4j;
import no.nav.levendearbeidsforholdscheduler.domain.StatusRespons;
import no.nav.levendearbeidsforholdscheduler.scheduler.JobbScheduler;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

import static no.nav.levendearbeidsforholdscheduler.utils.Utils.intervallErHeltall;
import static no.nav.levendearbeidsforholdscheduler.utils.Utils.lagCronExpression;
import static no.nav.levendearbeidsforholdscheduler.utils.Utils.sifferTilHeltall;

@Slf4j
@RestController
Expand All @@ -39,17 +37,14 @@ public ResponseEntity<String> reschedule(HttpServletRequest request) {
return ResponseEntity.badRequest().body("intervall er ikke spesifisert");
}

String cronExpression;
boolean erGyldigHeltall = intervallErHeltall(intervall);

if (erGyldigHeltall){
cronExpression = lagCronExpression(intervall);
var resultat = sifferTilHeltall(intervall);
if (resultat.isPresent()){
jobbScheduler.startScheduler(resultat.get());
return ResponseEntity.ok("Scheduler har begynt med intervall " + intervall);
} else {
return ResponseEntity.badRequest().body("intervall er ikke gyldig heltall");
}

jobbScheduler.startScheduler(cronExpression);
return ResponseEntity.ok("Scheduler har begynt med intervall " + intervall);
}

//TODO: Gjøre om endepunkt til å returnere med klokkeslettet neste jobb skal kjøre
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
import lombok.extern.slf4j.Slf4j;
import no.nav.levendearbeidsforholdscheduler.consumer.command.AnsettelseCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import static no.nav.levendearbeidsforholdscheduler.utils.Utils.hentKalender;


@Slf4j
@Component
@EnableScheduling
Expand All @@ -28,7 +25,7 @@ public class JobbScheduler {
private final AnsettelseCommand ansettelseCommand;

@Autowired
private final TaskScheduler taskScheduler;
private final ScheduledExecutorService taskScheduler;

private ScheduledFuture<?> scheduledFuture;

Expand All @@ -38,6 +35,8 @@ public class JobbScheduler {
private static final int SLUTT_KLOKKESLETT = 12;
private static final int SLUTT_DAG = 6;

private static final long INITIELL_FORSINKELSE = 0;

/**
* Funksjon som brukes for å sjekke om scheduleren kjører for øyeblikket
* @return true hvis scheduler kjører for øyeblikket
Expand Down Expand Up @@ -65,15 +64,15 @@ public Optional<String> hentTidspunktNesteKjoring(){

/**
* Avslutter eventuelt nåværende schedule og starter en ny med det nye intervallet.
* @param cronExpression Gyldig cron-expression (forutsetter at expression er allerede formattert)
* @param intervall Positivt heltall som representerer times-intervall for scheduler
*/
public void startScheduler(String cronExpression){
public void startScheduler(long intervall){

if (scheduledFuture != null) {
scheduledFuture.cancel(true);
}

scheduledFuture = taskScheduler.schedule(new AnsettelseJobb(), new CronTrigger(cronExpression));
scheduledFuture = taskScheduler.scheduleAtFixedRate(new AnsettelseJobb(), INITIELL_FORSINKELSE, intervall, TimeUnit.HOURS);

}

Expand Down Expand Up @@ -125,8 +124,7 @@ private class AnsettelseJobb implements Runnable {
@Override
public void run() {
if(sjekkOmGyldigTidsrom(START_KLOKKESLETT, START_DAG, SLUTT_KLOKKESLETT, SLUTT_DAG)){
//TODO: Kall på consumer som sender request til levende-arbeidsforhold-ansettelse app
//ansettelseCommand.aktiverAnsettelseService();
ansettelseCommand.aktiverAnsettelseService();
}

log.info("Kjørte jobb!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Optional;

public class Utils {

/**
* 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
* Funksjon som validerer om intervall er et positivt heltall
* @param intervall Tekst med siffer som representerer antall timer forsinkelse for job-scheduleren
* @return intervallet som heltall dersom det er et gyldig heltall
*/
public static String lagCronExpression(String intervallet) {
return "0 0 */" + intervallet + " ? * MON-SAT";
}

public static Optional<Long> sifferTilHeltall(String intervall) {

/**
* Funksjon som validerer om intervall er et positivt heltall. Brukes kun i this.rescheduleTask() metoden.
* @param intervall Heltall som representerer antall timer forsinkelse for job-scheduleren
* @return true hvis intervallet er et positivt heltall og false hvis ikke
*/
public static boolean intervallErHeltall(String intervall) {
try {
Integer.parseInt(intervall);
long tall = Integer.parseInt(intervall);
return Optional.of(tall);
} catch (NumberFormatException e) {
return false;
return Optional.empty();
}
return true;
}

public static Calendar hentKalender(){
Expand Down

0 comments on commit d13eee5

Please sign in to comment.