Skip to content

Commit

Permalink
Refactor casandra, ibmmq
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Oct 9, 2021
1 parent 495d351 commit 70cc212
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cassandra/health_checker.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cql
package cassandra

import (
"context"
Expand Down
10 changes: 4 additions & 6 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ func NewHandler(checkers ...Checker) *Handler {
func (c *Handler) Check(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
h := Check(ctx, c.Checkers)
bytes, err := json.Marshal(h)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if h.Status == StatusDown {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
w.Write(bytes)
err := json.NewEncoder(w).Encode(h)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
13 changes: 4 additions & 9 deletions ibm-mq/health_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type HealthChecker struct {
queue *QueueConfig
auth *MQAuth
qObject *ibmmq.MQObject
init bool
}

func NewHealthCheckerByConfig(queue *QueueConfig, auth *MQAuth, topic string, options ...string) *HealthChecker {
Expand Down Expand Up @@ -53,7 +52,7 @@ func NewIBMMQHealthCheckerByConfig(queue *QueueConfig, auth *MQAuth, topic strin
} else {
timeout = 4 * time.Second
}
return &HealthChecker{name: name, queue: queue, auth: auth, topic: topic, timeout: timeout, init: false}
return &HealthChecker{name: name, queue: queue, auth: auth, topic: topic, timeout: timeout}
}

func (s *HealthChecker) Name() string {
Expand All @@ -63,7 +62,7 @@ func (s *HealthChecker) Name() string {
func (s *HealthChecker) Check(ctx context.Context) (map[string]interface{}, error) {
res := make(map[string]interface{})
if s.queueManager == nil {
conn, err := newQueueManagerByConfig(*s.queue, *s.auth)
conn, err := NewQueueManagerByConfig(*s.queue, *s.auth)
if err != nil {
return res, err
}
Expand All @@ -74,15 +73,11 @@ func (s *HealthChecker) Check(ctx context.Context) (map[string]interface{}, erro
ibmmq.MQSO_NON_DURABLE |
ibmmq.MQSO_MANAGED
sd.ObjectString = s.topic
var subscriptionObject ibmmq.MQObject
var err error
if s.init == false {
if s.qObject == nil {
var qObject ibmmq.MQObject
s.qObject = &qObject
s.init = true
}
subscriptionObject, err = s.queueManager.Sub(sd, s.qObject)

subscriptionObject, err := s.queueManager.Sub(sd, s.qObject)
if err != nil {
return res, err
}
Expand Down
5 changes: 2 additions & 3 deletions ibm-mq/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ type QueueConfig struct {
ChannelName string `mapstructure:"channel_name" json:"channelName,omitempty" gorm:"column:channelname" bson:"channelName,omitempty" dynamodbav:"channelName,omitempty" firestore:"channelName,omitempty"`
ConnectionName string `mapstructure:"connection_name" json:"connectionName,omitempty" gorm:"column:connectionname" bson:"connectionName,omitempty" dynamodbav:"connectionName,omitempty" firestore:"connectionName,omitempty"`
QueueName string `mapstructure:"queue_name" json:"queueName,omitempty" gorm:"column:queuename" bson:"queueName,omitempty" dynamodbav:"queueName,omitempty" firestore:"queueName,omitempty"`
Put bool `mapstructure:"put" json:"put,omitempty" gorm:"column:queuename" bson:"put,omitempty" dynamodbav:"put,omitempty" firestore:"put,omitempty"`
Retry RetryConfig `mapstructure:"retry" json:"retry,omitempty" gorm:"column:retry" bson:"retry,omitempty" dynamodbav:"retry,omitempty" firestore:"retry,omitempty"`
}

type MQAuth struct {
UserId string `mapstructure:"user_id" json:"userId,omitempty" gorm:"column:userid" bson:"userId,omitempty" dynamodbav:"userId,omitempty" firestore:"userId,omitempty"`
Password string `mapstructure:"password" json:"password,omitempty" gorm:"column:password" bson:"password,omitempty" dynamodbav:"password,omitempty" firestore:"password,omitempty"`
}

type RetryConfig struct {
Retry1 int64 `mapstructure:"1" json:"retry1,omitempty" gorm:"column:retry1" bson:"retry1,omitempty" dynamodbav:"retry1,omitempty" firestore:"retry1,omitempty"`
Retry2 int64 `mapstructure:"2" json:"retry2,omitempty" gorm:"column:retry2" bson:"retry2,omitempty" dynamodbav:"retry2,omitempty" firestore:"retry2,omitempty"`
Expand All @@ -27,7 +26,7 @@ type RetryConfig struct {
Retry9 int64 `mapstructure:"9" json:"retry9,omitempty" gorm:"column:retry9" bson:"retry9,omitempty" dynamodbav:"retry9,omitempty" firestore:"retry9,omitempty"`
}

func newQueueManagerByConfig(c QueueConfig, auth MQAuth) (*ibmmq.MQQueueManager, error) {
func NewQueueManagerByConfig(c QueueConfig, auth MQAuth) (*ibmmq.MQQueueManager, error) {
cd := NewMQCDByChannelAndConnection(c.ChannelName, c.ConnectionName)
csp := NewMQCSPByConfig(auth)

Expand Down

0 comments on commit 70cc212

Please sign in to comment.