Skip to content

Commit

Permalink
Sending signals by closing the channel
Browse files Browse the repository at this point in the history
Signed-off-by: Iceber Gu <[email protected]>
  • Loading branch information
Iceber authored and chlins committed Apr 15, 2024
1 parent 7465a29 commit 5cc3d91
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/cmd/migrate-patch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,23 @@ func main() {
log.Fatalf("Failed to connect to Database, error: %v\n", err)
}
defer db.Close()
c := make(chan struct{}, 1)

c := make(chan struct{})

Check warning on line 52 in src/cmd/migrate-patch/main.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/migrate-patch/main.go#L51-L52

Added lines #L51 - L52 were not covered by tests
go func() {
defer close(c)

Check warning on line 55 in src/cmd/migrate-patch/main.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/migrate-patch/main.go#L54-L55

Added lines #L54 - L55 were not covered by tests
err := db.Ping()
for ; err != nil; err = db.Ping() {
log.Println("Failed to Ping DB, sleep for 1 second.")
time.Sleep(1 * time.Second)
}
c <- struct{}{}
}()
select {
case <-c:
case <-time.After(30 * time.Second):
log.Fatal("Failed to connect DB after 30 seconds, time out. \n")
}

row := db.QueryRow(pgSQLCheckColStmt)
var tblCount, colCount int
if err := row.Scan(&tblCount, &colCount); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions src/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ func gracefulShutdown(closing, done chan struct{}, shutdowns ...func()) {
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
log.Infof("capture system signal %s, to close \"closing\" channel", <-signals)
close(closing)
shutdownChan := make(chan struct{}, 1)
shutdownChan := make(chan struct{})

Check warning on line 107 in src/core/main.go

View check run for this annotation

Codecov / codecov/patch

src/core/main.go#L107

Added line #L107 was not covered by tests
go func() {
defer close(shutdownChan)

Check warning on line 109 in src/core/main.go

View check run for this annotation

Codecov / codecov/patch

src/core/main.go#L109

Added line #L109 was not covered by tests
for _, s := range shutdowns {
s()
}
<-done
log.Infof("Goroutines exited normally")
shutdownChan <- struct{}{}
}()
select {
case <-shutdownChan:
Expand Down

0 comments on commit 5cc3d91

Please sign in to comment.