Skip to content

Commit

Permalink
Update InitializeEdgesWorker.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Sn0w3y authored Oct 1, 2024
1 parent 8c8cba5 commit c384cfc
Showing 1 changed file with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,6 +23,8 @@ public class InitializeEdgesWorker {
protected final PostgresHandler parent;
private final HikariDataSource dataSource;
private final Runnable onFinished;
private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
private boolean isMarkAllEdgesAsOfflineCalled = false;

/**
* Executor for subscriptions task.
Expand All @@ -39,24 +43,42 @@ public InitializeEdgesWorker(PostgresHandler parent, HikariDataSource dataSource
public synchronized void start() {
this.executor.execute(() -> {
try (var con = this.dataSource.getConnection()) {
this.parent.logInfo(this.log, "Caching Edges from Postgres [started]");
this.markAllEdgesAsOffline(con);
this.readAllEdgesFromPostgres(con);
this.parent.logInfo(this.log, "Caching Edges from Postgres [finished]");
// Erste Ausführung sofort
runCachingEdgesTask(con);

Check warning on line 47 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/postgres/InitializeEdgesWorker.java

View workflow job for this annotation

GitHub Actions / build-java

Method call to 'runCachingEdgesTask' needs "this.".

// Planen der die periodische Ausführung
scheduledExecutor.scheduleAtFixedRate(() -> {

Check warning on line 50 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/postgres/InitializeEdgesWorker.java

View workflow job for this annotation

GitHub Actions / build-java

Reference to instance variable 'scheduledExecutor' needs "this.".
try (var newCon = this.dataSource.getConnection()) {
runCachingEdgesTask(newCon);

Check warning on line 52 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/postgres/InitializeEdgesWorker.java

View workflow job for this annotation

GitHub Actions / build-java

Method call to 'runCachingEdgesTask' needs "this.".
} catch (SQLException e) {
logError("Fehler beim erneuten Verbinden mit Postgres.", e);

Check warning on line 54 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/postgres/InitializeEdgesWorker.java

View workflow job for this annotation

GitHub Actions / build-java

Method call to 'logError' needs "this.".
}
}, 20, 20, TimeUnit.MINUTES);
} catch (SQLException e) {
this.parent.logWarn(this.log, "Caching Edges from Postgres [canceled]");
this.logError("Unable to connect do dataSource. ", e);
logError("Unable to connect do dataSource. ", e);

Check warning on line 58 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/postgres/InitializeEdgesWorker.java

View workflow job for this annotation

GitHub Actions / build-java

Method call to 'logError' needs "this.".
}
this.onFinished.run();
});
}

private void runCachingEdgesTask(Connection con) {
this.parent.logInfo(this.log, "Caching Edges from Postgres [started]");
// Überprüfen, ob markAllEdgesAsOffline bereits aufgerufen wurde
if (!isMarkAllEdgesAsOfflineCalled) {

Check warning on line 67 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/postgres/InitializeEdgesWorker.java

View workflow job for this annotation

GitHub Actions / build-java

Reference to instance variable 'isMarkAllEdgesAsOfflineCalled' needs "this.".
this.markAllEdgesAsOffline(con);
isMarkAllEdgesAsOfflineCalled = true;

Check warning on line 69 in io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/postgres/InitializeEdgesWorker.java

View workflow job for this annotation

GitHub Actions / build-java

Reference to instance variable 'isMarkAllEdgesAsOfflineCalled' needs "this.".
}
this.readAllEdgesFromPostgres(con);
this.parent.logInfo(this.log, "Caching Edges from Postgres [finished]");
}

/**
* Stops the {@link InitializeEdgesWorker}.
*/
public synchronized void stop() {
// Shutdown executor
ThreadPoolUtils.shutdownAndAwaitTermination(this.executor, 5);
ThreadPoolUtils.shutdownAndAwaitTermination(this.scheduledExecutor, 5);
}

private void markAllEdgesAsOffline(Connection con) {
Expand Down

0 comments on commit c384cfc

Please sign in to comment.