Commit 32751635 authored by Roberto Loayza Miljanovich's avatar Roberto Loayza Miljanovich

Merge branch 'dev_marcos' into 'developer'

Dev marcos

See merge request ByteBot/web/bytebot-service!24
parents 60f4a48c 31fd50ae
......@@ -99,10 +99,12 @@
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>4.30.0</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>4.30.0</version>
</dependency>
<!-- Fin de Lock para scheduling -->
......
package com.bytesw.bytebot.etl.batch.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class DBConfig{
@Value("${spring.datasource.driverClassName}")
private String classDriver;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(classDriver);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
}
\ No newline at end of file
......@@ -27,6 +27,10 @@ import com.bytesw.bytebot.repository.*;
import com.bytesw.xdf.multitenant.core.ThreadLocalStorage;
import com.google.gson.Gson;
import lombok.extern.log4j.Log4j2;
import net.javacrumbs.shedlock.core.DefaultLockingTaskExecutor;
import net.javacrumbs.shedlock.core.LockingTaskExecutor;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
......@@ -37,11 +41,15 @@ import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.time.Duration;
......@@ -55,6 +63,9 @@ import java.util.concurrent.ScheduledFuture;
// Descomentar lo siguiente para que funcione
@Service
@Log4j2
@Component
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor="PT15M")
public class ScheduleService implements SchedulingConfigurer {
ScheduledTaskRegistrar scheduledTaskRegistrar;
......@@ -191,11 +202,6 @@ public class ScheduleService implements SchedulingConfigurer {
} else {
timeZone = TimeZone.getTimeZone(zoneDefault);
}
Trigger trigger = new CronTrigger(cronExpression, timeZone);
/* ETL de Dashboard */
String key = String.format("%s-%s", tenantIdentifier, identifier);
futureMap.put(key, taskRegistrar.getScheduler().schedule(() -> scheduleCron(createJob(tenantIdentifier), tenantIdentifier), trigger));
/* Extraer el id del proceso - Delete */
ProcessETL processDelete = processETLRepository.findByName(ProcessETLEnum.PROCESS_DELETE.getName());
......@@ -280,12 +286,12 @@ public class ScheduleService implements SchedulingConfigurer {
String keyDataSens = "";
for (DeleteDataSensBean data : deleteDataSensBeans) {
Agent agent = agentRepository.findById(data.getAgenId()).get();
Trigger trigger = new CronTrigger("0 0/1 * * * *", timeZone);
Trigger trigger = new CronTrigger(cronExpression, timeZone);
keyDataSens = String.format("deleteSensible-%s-%s", tenantIdentifier, data.getAgenId());
if (!futureMap.containsKey(keyDataSens)) {
keys.add(keyDataSens);
futureMap.put(keyDataSens, taskRegistrar.getScheduler()
.schedule(() -> scheduleCron(createJobDataSens(tenantIdentifier, data), tenantIdentifier), trigger));
.schedule(() -> scheduleCron(createJobDataSens(data), tenantIdentifier), trigger));
}
}
} else {
......@@ -338,12 +344,20 @@ public class ScheduleService implements SchedulingConfigurer {
}
/* Métodos utilizados para ETL de dashboard */
private Job createJob(String tenantIdentifier) {
ThreadLocalStorage.setTenantName(tenantIdentifier);
return jobBuilderFactory.get("processJob")
.incrementer(new RunIdIncrementer()).listener(listener)
.flow(createStep(tenantIdentifier)).end().build();
@Scheduled(cron = "${application.byte-bot.batch.cron}")
@SchedulerLock(name = "Dashboard-ETL", lockAtLeastFor = "PT40S", lockAtMostFor = "PT50S")
protected void createJob() {
List<Agent> agentDeployed = agentRepository.findByStatus(AgentStatusEnum.DEPLOYED);
log.info("INICIADO");
if (!agentDeployed.isEmpty()) {
ThreadLocalStorage.setTenantName(tenant);
Job job = jobBuilderFactory.get("processJob")
.incrementer(new RunIdIncrementer()).listener(listener)
.flow(createStep(tenant)).end().build();
scheduleCron(job, tenant);
} else {
log.info("No hay agentes deployados.");
}
}
private Step createStep(String tenantIdentifier) {
......@@ -374,13 +388,12 @@ public class ScheduleService implements SchedulingConfigurer {
}
/* Métodos ETL de data sensible */
private Job createJobDataSens(String tenantIdentifier, DeleteDataSensBean data) {
private Job createJobDataSens(DeleteDataSensBean data) {
System.out.println("ETL de eliminacion");
ThreadLocalStorage.setTenantName(tenantIdentifier);
return jobBuilderFactory.get( String.format("processDataSensible-%d",+data.getAgenId()))
ThreadLocalStorage.setTenantName(tenant);
return jobBuilderFactory.get( String.format("processDataSensible-%d",data.getAgenId()))
.incrementer(new RunIdIncrementer()).listener(listener)
.flow(createStepDataSens(tenantIdentifier, data)).end().build();
.flow(createStepDataSens(tenant, data)).end().build();
}
private Step createStepDataSens(String tenantIdentifier, DeleteDataSensBean data) {
......@@ -442,6 +455,7 @@ public class ScheduleService implements SchedulingConfigurer {
return properties.getTenants().stream().filter(x -> x.getId().equals(tenantId)).findFirst();
}
private void scheduleCron(Job job, String tenantId) {
UUID traceID = UUID.randomUUID();
Map<String, JobParameter> maps = new HashMap<>();
......@@ -451,13 +465,15 @@ public class ScheduleService implements SchedulingConfigurer {
JobParameters parameters = new JobParameters(maps);
Set<JobExecution> jobExecutions = new HashSet<>();
try {
jobExecutions = jobExplorer.findRunningJobExecutions(job.getName());
String JobInfo = String.format("Jobs {} en Ejecución: {}", job.getName(), jobExecutions.size());
log.info(JobInfo);
if (jobExecutions.isEmpty()) {
asyncJobLauncher.run(job, parameters);
} else {
log.info("El Job " + job.getName() + " no se ejecutará porque hay jobs pendientes: " + jobExecutions.size());
if (job != null) {
jobExecutions = jobExplorer.findRunningJobExecutions(job.getName());
String JobInfo = String.format("Jobs {} en Ejecución: {}", job.getName(), jobExecutions.size());
log.info(JobInfo);
if (jobExecutions.isEmpty()) {
asyncJobLauncher.run(job, parameters);
} else {
log.info("El Job " + job.getName() + " no se ejecutará porque hay jobs pendientes: " + jobExecutions.size());
}
}
} catch (Exception e) {
e.printStackTrace();
......
package com.bytesw.bytebot.etl.batch.service;
import com.bytesw.bytebot.etl.batch.beans.DBConfig;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class SchedulerConfiguration {
@Bean
public LockProvider lockProvider(DataSource dataSource) {
DBConfig dbConfig = new DBConfig();
dataSource = dbConfig.dataSource();
return new JdbcTemplateLockProvider(dataSource);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment