Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
add ping to module preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Nov 22, 2019
1 parent c82c7f5 commit 26c5101
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
18 changes: 15 additions & 3 deletions pkg/typredis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os/exec"
"time"

log "github.com/sirupsen/logrus"

"github.com/go-redis/redis"
"github.com/typical-go/typical-go/pkg/typcfg"
"github.com/typical-go/typical-go/pkg/typcli"
Expand Down Expand Up @@ -64,6 +66,13 @@ func (r redisModule) Provide() []interface{} {
}
}

// Prepare the module
func (r redisModule) Prepare() []interface{} {
return []interface{}{
r.ping,
}
}

// Destroy dependencies
func (r redisModule) Destroy() []interface{} {
return []interface{}{
Expand Down Expand Up @@ -96,7 +105,7 @@ func (r redisModule) loadConfig(loader typcfg.Loader) (cfg Config, err error) {
return
}

func (redisModule) connect(cfg Config) (client *redis.Client, err error) {
func (redisModule) connect(cfg Config) (client *redis.Client) {
client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
Password: cfg.Password,
Expand All @@ -109,12 +118,15 @@ func (redisModule) connect(cfg Config) (client *redis.Client, err error) {
IdleCheckFrequency: cfg.IdleCheckFrequency,
MaxConnAge: cfg.MaxConnAge,
})
err = client.Ping().Err()
return
}

func (redisModule) ping(client *redis.Client) error {
log.Info("Ping to Redis")
return client.Ping().Err()
}

func (redisModule) disconnect(client *redis.Client) (err error) {
fmt.Println("Redis Client close")
return client.Close()
}

Expand Down
1 change: 1 addition & 0 deletions pkg/typredis/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestModule(t *testing.T) {
m := typredis.Module()
require.True(t, typmodule.IsProvider(m))
require.True(t, typmodule.IsDestroyer(m))
require.True(t, typmodule.IsPreparer(m))
require.True(t, typcli.IsCommander(m))
require.True(t, typcfg.IsConfigurer(m))
}

0 comments on commit 26c5101

Please sign in to comment.