Skip to content

Commit

Permalink
[#7] .Update implemented IsHealthy
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasObenaus committed Feb 12, 2020
1 parent b65c23a commit f0bb2cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
5 changes: 5 additions & 0 deletions cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,8 @@ func (c *Cosmos) Stop() error {
func (c *Cosmos) String() string {
return fmt.Sprintf("CosmosDB (connected=%t, target=%s, user=%s)", c.IsConnected(), c.host, c.username)
}

// IsHealthy returns true if the Cosmos DB connection is alive.
func (c *Cosmos) IsHealthy() bool {
return c.IsConnected()
}
54 changes: 35 additions & 19 deletions examples/local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,48 @@ func main() {
logger.Fatal().Err(err).Msg("Failed to create the cosmos connector")
}

signal_chan := make(chan os.Signal, 1)
signal.Notify(signal_chan, syscall.SIGINT, syscall.SIGTERM)
exit_chan := make(chan struct{})
go func() {
for {

ticker := time.NewTicker(time.Millisecond * 10)
defer ticker.Stop()

select {
case <-signal_chan:
close(exit_chan)
return
case <-ticker.C:
queryCosmos(cosmos, logger)
}
}
}()
exitChannel := make(chan struct{})
go processLoop(cosmos, logger, exitChannel)

<-exit_chan
<-exitChannel
if err := cosmos.Stop(); err != nil {
logger.Error().Err(err).Msg("Failed to stop")
}
logger.Info().Msg("Teared down")
}

func processLoop(cosmos *gremtune.Cosmos, logger zerolog.Logger, exitChannel chan<- struct{}) {
// register for common exit signals (e.g. ctrl-c)
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)

// create tickers for doing health check and queries
queryTicker := time.NewTicker(time.Second * 2)
healthCheckTicker := time.NewTicker(time.Second * 1)

// ensure to clean up as soon as the processLoop has been left
defer func() {
queryTicker.Stop()
healthCheckTicker.Stop()
}()

stopProcessing := false
logger.Info().Msg("Process loop entered")
for !stopProcessing {
select {
case <-signalChannel:
close(exitChannel)
stopProcessing = true
case <-queryTicker.C:
queryCosmos(cosmos, logger)
case <-healthCheckTicker.C:
logger.Debug().Bool("healthy", cosmos.IsHealthy()).Msg("Health Check")
}
}

logger.Info().Msg("Process loop left")
}

func queryCosmos(cosmos *gremtune.Cosmos, logger zerolog.Logger) {
res, err := cosmos.Execute("g.addV('Phil')")
if err != nil {
Expand Down

0 comments on commit f0bb2cd

Please sign in to comment.