Skip to content

Commit

Permalink
Merge pull request #8 from hellofresh/feature/checker-mongo
Browse files Browse the repository at this point in the history
Feature/checker mongo
  • Loading branch information
vgarvardt authored Sep 22, 2017
2 parents 207d4e0 + d96a4df commit be5e838
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* PostgreSQL
* Redis
* HTTP
* MongoDB

## Usage

Expand Down
14 changes: 7 additions & 7 deletions checks/http/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ type Config struct {
// - getting response status from defined URL
// - verifying that status code is less than 500
func New(config Config) func() error {
return func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

if config.RequestTimeout == 0 {
config.RequestTimeout = time.Second * 5
}
if config.RequestTimeout == 0 {
config.RequestTimeout = time.Second * 5
}

return func() error {
req, err := http.NewRequest(http.MethodGet, config.URL, nil)
if err != nil {
config.LogFunc(err, "Creating the request for the health check failed")
Expand Down
38 changes: 38 additions & 0 deletions checks/mongo/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package mongo

import "gopkg.in/mgo.v2"

// Config is the MongoDB checker configuration settings container.
type Config struct {
// DSN is the MongoDB instance connection DSN. Required.
DSN string
// LogFunc is the callback function for errors logging during check.
// If not set logging is skipped.
LogFunc func(err error, details string, extra ...interface{})
}

// New creates new MongoDB health check that verifies the following:
// - connection establishing
// - doing the ping command
func New(config Config) func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

return func() error {
session, err := mgo.Dial(config.DSN)
if err != nil {
config.LogFunc(err, "MongoDB health check failed during connect")
return err
}
defer session.Close()

err = session.Ping()
if err != nil {
config.LogFunc(err, "MongoDB health check failed during ping")
return err
}

return nil
}
}
8 changes: 4 additions & 4 deletions checks/postgres/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ type Config struct {
// - selecting inserted row
// - deleting inserted row
func New(config Config) func() error {
return func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

return func() error {
db, err := sql.Open("postgres", config.DSN)
if err != nil {
config.LogFunc(err, "PostgreSQL health check failed during connect")
Expand Down
39 changes: 19 additions & 20 deletions checks/rabbitmq/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,31 @@ type Config struct {
// - publishing a message to the exchange with the defined routing key
// - consuming published message
func New(config Config) func() error {
return func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

if config.Exchange == "" {
config.Exchange = defaultExchange
}
if config.Exchange == "" {
config.Exchange = defaultExchange
}

if config.RoutingKey == "" {
host, err := os.Hostname()
if nil != err {
config.LogFunc(err, "RabbitMQ health check failed on settings default value for unset routing key")
return err
}
config.RoutingKey = host
if config.RoutingKey == "" {
host, err := os.Hostname()
if nil != err {
config.RoutingKey = "-unknown-"
}
config.RoutingKey = host
}

if config.Queue == "" {
config.Queue = fmt.Sprintf("%s.%s", config.Exchange, config.RoutingKey)
}
if config.Queue == "" {
config.Queue = fmt.Sprintf("%s.%s", config.Exchange, config.RoutingKey)
}

if config.ConsumeTimeout == 0 {
config.ConsumeTimeout = time.Second * 3
}
if config.ConsumeTimeout == 0 {
config.ConsumeTimeout = time.Second * 3
}

return func() error {
conn, err := amqp.Dial(config.DSN)
if err != nil {
config.LogFunc(err, "RabbitMQ health check failed on dial phase")
Expand Down
8 changes: 4 additions & 4 deletions checks/redis/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type Config struct {
// - connection establishing
// - doing the PING command and verifying the response
func New(config Config) func() error {
return func() error {
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}
if config.LogFunc == nil {
config.LogFunc = func(err error, details string, extra ...interface{}) {}
}

return func() error {
pool := &redis.Pool{
MaxIdle: 1,
IdleTimeout: 10 * time.Second,
Expand Down

0 comments on commit be5e838

Please sign in to comment.