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

fix(jdbc): ensure JdbcIndexer is only closed once #6781

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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
24 changes: 16 additions & 8 deletions jdbc/src/main/java/io/kestra/jdbc/runner/JdbcIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import io.micronaut.context.event.ApplicationEventPublisher;
Expand Down Expand Up @@ -47,6 +48,8 @@ public class JdbcIndexer implements IndexerInterface {
private final AtomicReference<ServiceState> state = new AtomicReference<>();
private final ApplicationEventPublisher<ServiceStateChangeEvent> eventPublisher;

private final AtomicBoolean closed = new AtomicBoolean(false);

@Inject
public JdbcIndexer(
LogRepositoryInterface logRepository,
Expand Down Expand Up @@ -122,14 +125,19 @@ public ServiceState getState() {
@PreDestroy
@Override
public void close() {
setState(ServiceState.TERMINATING);
this.receiveCancellations.forEach(Runnable::run);
try {
stopQueue();
setState(ServiceState.TERMINATED_GRACEFULLY);
} catch (IOException e) {
log.error("Failed to close the queue", e);
setState(ServiceState.TERMINATED_FORCED);
if (closed.compareAndSet(false, true)) {
setState(ServiceState.TERMINATING);
if (log.isDebugEnabled()) {
log.debug("Terminating");
}
this.receiveCancellations.forEach(Runnable::run);
try {
stopQueue();
setState(ServiceState.TERMINATED_GRACEFULLY);
} catch (IOException e) {
log.error("Failed to close the queue", e);
setState(ServiceState.TERMINATED_FORCED);
}
}
}

Expand Down
Loading