Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14 job scheduler #3571

Merged
merged 12 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/levende-arbeidsforhold-ansettelse/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ dependencies {

implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-quartz'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

implementation 'org.springframework.cloud:spring-cloud-starter-vault-config'
implementation 'org.springframework.boot:spring-boot-starter-actuator'

implementation 'org.postgresql:postgresql:42.7.2'

implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'io.swagger.core.v3:swagger-annotations-jakarta:2.2.21'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package no.nav.registre.testnorge.levendearbeidsforholdansettelse.config;

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

@Configuration
public class SchedulerConfig {

@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.setThreadNamePrefix("taskScheduler-");
return taskScheduler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@NoArgsConstructor
@Getter
@Setter
@Table(name = "JOBB_PARAMETER")
@Table(name = "jobb_parameter")
public class JobbParameterEntity implements Serializable {
/*
@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package no.nav.registre.testnorge.levendearbeidsforholdansettelse.scheduler;

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 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;

private static final String DEFAULT_INTERVALL = "24";
private static final String INTERVALL_PARAM_NAVN = "intervall";

/**
* Funksjon som henter ut en intervallet fra databasen.
* Funksjonen kalles når appen kjøres opp og vil kun ha en effekt dersom intervallet eksisterer i databasen.
*/
@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_PARAM_NAVN)){
String intervall = param.getVerdi();
log.info("Parameter-verdi for intervall: {}", intervall);
rescheduleTask(intervall);
}
});
}

/**
* Avslutter nåværende schedule og starter en ny med det nye intervallet.
* @param intervall Heltall som representerer antall timer forsinkelse for job-scheduleren
*/
public void rescheduleTask(String intervall){

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

String cronExpression;
if (intervallErHeltall(intervall)){
cronExpression = lagCronExpression(intervall);
} else {
cronExpression = lagCronExpression(DEFAULT_INTERVALL);
}

scheduledFuture = taskScheduler.schedule(new AnsettelseJobb(), new CronTrigger(cronExpression));
log.info("Schedulet en task med intervall: {}", cronExpression);

}

/**
* 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 0 */" + intervallet + " ? * MON-SAT";
}

/**
* 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
*/
private static boolean intervallErHeltall(String intervall) {
try {
Integer.parseInt(intervall);
} catch (NumberFormatException e) {
return false;
}
return true;
}


/**
* Klasse for jobben som skal kjøres av scheduler
*/
private static class AnsettelseJobb implements Runnable {

/**
* Funksjon som automatisk blir kalt på av en task-scheduler og kjører ansettelsesservice utenom lørdag fra
* klokken 12:00 til mandag klokken 06:00
*/
@Override
public void run() {

/*
if (!((dag == mandag && clock <= 6AM) || (dag == søndag) || (dag == lørdag && clock >= 12PM)) ){
//Kall på AnsettelseService her
}
*/
log.info("Jobb kjørte!");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

INSERT INTO jobb_parameter (NAVN, TEKST, VERDI) VALUES ('antallOrganisasjoner', 'Organisasjoner', '100'),
('antallPersoner', 'Personer', '20'),
('typeArbeidsforhold', 'Type Arbeidsforhold', 'ordinaertArbeidsforhold'),
('arbeidstidsOrdning', 'Arbeidstids Ordning', 'ikkeSkift'),
('intervall', 'Intervall', '2'),
('stillingsprosent', 'Stillingsprosent', '100.0');
/*
insert into jobb_parameter (navn, tekst, verdi) values ("antallOrganisasjoner", "Antall Organisasjoner", "20");
insert into jobb_parameter (navn, tekst, verdi) values ("antallPersoner", "Antall Personer", "40");
Expand Down
Loading