131 lines
4.6 KiB
Java
131 lines
4.6 KiB
Java
package eu.eudat.service.metrics;
|
|
|
|
import eu.eudat.commons.enums.IsActive;
|
|
import eu.eudat.commons.fake.FakeRequestScope;
|
|
import eu.eudat.commons.metrics.MetricNames;
|
|
import eu.eudat.commons.scope.tenant.TenantScope;
|
|
import eu.eudat.data.QueueInboxEntity;
|
|
import eu.eudat.data.StorageFileEntity;
|
|
import eu.eudat.data.TenantEntity;
|
|
import eu.eudat.data.TenantEntityManager;
|
|
import eu.eudat.integrationevent.inbox.EventProcessingStatus;
|
|
import eu.eudat.model.StorageFile;
|
|
import eu.eudat.model.Tenant;
|
|
import eu.eudat.query.QueueInboxQuery;
|
|
import eu.eudat.query.StorageFileQuery;
|
|
import eu.eudat.query.TenantQuery;
|
|
import eu.eudat.service.storage.StorageFileService;
|
|
import gr.cite.queueinbox.entity.QueueInboxStatus;
|
|
import gr.cite.queueinbox.repository.CandidateInfo;
|
|
import gr.cite.tools.data.query.Ordering;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import gr.cite.tools.logging.LoggerService;
|
|
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
|
import io.prometheus.client.Gauge;
|
|
import jakarta.persistence.EntityManager;
|
|
import jakarta.persistence.EntityManagerFactory;
|
|
import jakarta.persistence.EntityTransaction;
|
|
import jakarta.persistence.OptimisticLockException;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationListener;
|
|
import org.springframework.context.annotation.Scope;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.io.Closeable;
|
|
import java.io.IOException;
|
|
import java.time.Instant;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
|
|
@Service
|
|
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
|
|
public class UpdateMetricsTask implements Closeable, ApplicationListener<ApplicationReadyEvent> {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UpdateMetricsTask.class));
|
|
private final UpdateMetricsTaskProperties _config;
|
|
private final ApplicationContext applicationContext;
|
|
private ScheduledExecutorService scheduler;
|
|
private Map<String, Gauge> gauges;
|
|
|
|
public UpdateMetricsTask(
|
|
UpdateMetricsTaskProperties config,
|
|
ApplicationContext applicationContext) {
|
|
this._config = config;
|
|
this.applicationContext = applicationContext;
|
|
this.gauges = null;
|
|
}
|
|
|
|
@Override
|
|
public void onApplicationEvent(ApplicationReadyEvent event) {
|
|
long intervalSeconds = this._config .getIntervalSeconds();
|
|
if (this._config .getEnable() && intervalSeconds > 0) {
|
|
logger.info("File clean up run in {} seconds", intervalSeconds);
|
|
|
|
scheduler = Executors.newScheduledThreadPool(1);
|
|
scheduler.scheduleAtFixedRate(this::process, 10, intervalSeconds, TimeUnit.SECONDS);
|
|
} else {
|
|
scheduler = null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void close() throws IOException {
|
|
if (scheduler != null) this.scheduler.close();
|
|
}
|
|
|
|
protected void process() {
|
|
if (!this._config.getEnable()) return;
|
|
try {
|
|
this.ensureGauges();
|
|
this.calculate();
|
|
} catch (Exception ex) {
|
|
logger.error("Problem processing file cleanups. Breaking for next interval", ex);
|
|
}
|
|
}
|
|
|
|
private void ensureGauges() {
|
|
if (this.gauges != null) return;
|
|
try (FakeRequestScope ignored = new FakeRequestScope()) {
|
|
MetricsService metricsService = this.applicationContext.getBean(MetricsService.class);
|
|
this.gauges = metricsService.gaugesBuild();
|
|
} catch (Exception ex) {
|
|
logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
|
|
}
|
|
}
|
|
|
|
private void calculate() {
|
|
EntityManager entityManager = null;
|
|
|
|
try (FakeRequestScope ignored = new FakeRequestScope()) {
|
|
try {
|
|
|
|
EntityManagerFactory entityManagerFactory = this.applicationContext.getBean(EntityManagerFactory.class);
|
|
|
|
entityManager = entityManagerFactory.createEntityManager();
|
|
|
|
TenantEntityManager tenantEntityManager = this.applicationContext.getBean(TenantEntityManager.class);
|
|
tenantEntityManager.setEntityManager(entityManager);
|
|
TenantScope tenantScope = this.applicationContext.getBean(TenantScope.class);
|
|
tenantScope.setTenant(null, tenantScope.getDefaultTenantCode());
|
|
|
|
MetricsService metricsService = this.applicationContext.getBean(MetricsService.class);
|
|
|
|
metricsService.calculate(this.gauges);
|
|
} finally {
|
|
if (entityManager != null) entityManager.close();
|
|
}
|
|
} catch (Exception ex) {
|
|
logger.error("Problem executing purge. rolling back any db changes and marking error. Continuing...", ex);
|
|
}
|
|
}
|
|
|
|
}
|