Skip to content

Commit

Permalink
Gracefully shutdown netty server (#5541)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohansong committed May 4, 2023
1 parent 1650117 commit 8c65c50
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.server;

import io.micronaut.context.annotation.Value;
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.runtime.event.ApplicationShutdownEvent;
import jakarta.inject.Singleton;
import java.lang.invoke.MethodHandles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Listens for shutdown signal and keeps server alive for 20 seconds to process requests on the fly.
*/
@Singleton
public class ShutdownEventListener implements
ApplicationEventListener<ApplicationShutdownEvent> {

private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

@Value("${airbyte.shutdown.delay_ms}")
private int shutdownDelayMillis;

@Override
public void onApplicationEvent(ApplicationShutdownEvent event) {
logger.info("ShutdownEvent before wait");
try {
// Sleep 20 seconds to make sure server is wrapping up last remaining requests before
// closing the connections.
Thread.sleep(shutdownDelayMillis);
} catch (final Exception ex) {
// silently fail at this stage because server is terminating.
logger.warn("exception: " + ex);
}
logger.info("ShutdownEvent after wait");
}

}
2 changes: 2 additions & 0 deletions airbyte-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ micronaut:
idle-timeout: ${HTTP_IDLE_TIMEOUT:5m}

airbyte:
shutdown:
delay_ms: 20000
cloud:
storage:
logs:
Expand Down

0 comments on commit 8c65c50

Please sign in to comment.