Skip to content

Commit

Permalink
we should block in shutdown after shutdown was called
Browse files Browse the repository at this point in the history
I O U  a proper message
  • Loading branch information
ifindlay-cci authored and nouseforaname committed Sep 5, 2024
1 parent cc63582 commit 7d80274
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
10 changes: 4 additions & 6 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,17 @@ func startServer(registry pakBroker.BrokerRegistry, db *sql.DB, brokerapi http.H
switch signalReceived {

case syscall.SIGTERM:
logger.Info("received SIGTERM, server is shutting down gracefully allowing for in flight work to finish")

shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), shutdownTimeout)
if err := httpServer.Shutdown(shutdownCtx); err != nil {
logger.Fatal("shutdown error: %v", err)
}
logger.Info("received SIGTERM, server is shutting down gracefully allowing for in flight work to finish")
defer shutdownRelease()
for store.LockFilesExist() {
logger.Info("draining csb instance")
time.Sleep(time.Second * 1)
}
logger.Info("draining complete")
if err := httpServer.Shutdown(shutdownCtx); err != nil {
logger.Fatal("shutdown error: %v", err)
}

logger.Info("shutdown complete")
case syscall.SIGKILL:
logger.Info("received SIGKILL, server is shutting down immediately. In flight operations will not finish and their state is potentially lost.")
Expand Down
15 changes: 9 additions & 6 deletions integrationtest/termination_recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ var _ = Describe("Recovery From Broker Termination", func() {
Expect(response.StatusCode).To(Equal(http.StatusAccepted))
Eventually(stdout, time.Second*5).Should(gbytes.Say(`tofu","apply","-auto-approve"`))
By("gracefully stopping the broker")
// Stop seems to be blocking, so run it in a routine so we can check that the broker actually rejects requests until it's fully stopped.
go func() {
Expect(broker.Stop()).To(Succeed())
}()

Expect(broker.Stop()).To(Succeed())
By("logging a message")
Eventually(stdout).Should(gbytes.Say("received SIGTERM"))

By("ensuring that the broker rejects requests")
Expect(broker.Client.LastOperation(instanceGUID, uuid.NewString()).Error).To(HaveOccurred())

By("logging a message")
Expect(string(stdout.Contents())).To(ContainSubstring("received SIGTERM"))
Expect(string(stdout.Contents())).To(ContainSubstring("draining csb"))
Expect(string(stdout.Contents())).To(ContainSubstring("draining complete"))
Expect(string(stdout.Contents())).ToNot(ContainSubstring("shutdown error"))
Eventually(stdout, time.Minute).Should(Say("draining csb"))
Eventually(stdout, time.Minute).Should(Say("draining complete"))
Eventually(stdout, time.Minute).ShouldNot(Say("shutdown error"))

broker = must(testdrive.StartBroker(csb, brokerpak, database, testdrive.WithOutputs(stdout, stderr)))

Expand Down

0 comments on commit 7d80274

Please sign in to comment.