diff --git a/cmd/chkmqready/main.go b/cmd/chkmqready/main.go index d3c1e5a..c06b4a3 100644 --- a/cmd/chkmqready/main.go +++ b/cmd/chkmqready/main.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2017, 2022 +© Copyright IBM Corporation 2017, 2023 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -44,7 +44,12 @@ func doMain() int { } // Check if the queue manager has a running listener - if active, _ := ready.IsRunningAsActiveQM(ctx, name); active { + status, err := ready.Status(ctx, name) + if err != nil { + return 1 + } + switch status { + case ready.StatusActiveQM: conn, err := net.Dial("tcp", "127.0.0.1:1414") if err != nil { fmt.Println(err) @@ -54,16 +59,16 @@ func doMain() int { if err != nil { fmt.Println(err) } - } else if standby, _ := ready.IsRunningAsStandbyQM(ctx, name); standby { + return 0 + case ready.StatusStandbyQM: fmt.Printf("Detected queue manager running in standby mode") return 10 - } else if replica, _ := ready.IsRunningAsReplicaQM(ctx, name); replica { + case ready.StatusReplicaQM: fmt.Printf("Detected queue manager running in replica mode") return 20 - } else { + default: return 1 } - return 0 } func main() { diff --git a/cmd/runmqserver/qmgr.go b/cmd/runmqserver/qmgr.go index 23ed699..93e42f4 100644 --- a/cmd/runmqserver/qmgr.go +++ b/cmd/runmqserver/qmgr.go @@ -164,11 +164,12 @@ func startQueueManager(name string) error { func stopQueueManager(name string) error { log.Println("Stopping queue manager") qmGracePeriod := os.Getenv("MQ_GRACE_PERIOD") - isStandby, err := ready.IsRunningAsStandbyQM(context.Background(), name) + status, err := ready.Status(context.Background(), name) if err != nil { log.Printf("Error getting status for queue manager %v: %v", name, err.Error()) return err } + isStandby := status.StandbyQM() args := []string{"-w", "-r", "-tp", qmGracePeriod, name} if os.Getenv("MQ_MULTI_INSTANCE") == "true" { if isStandby { diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index dad3c3e..0b261e7 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2018, 2022 +© Copyright IBM Corporation 2018, 2023 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -43,8 +43,8 @@ func GatherMetrics(qmName string, log *logger.Logger) { // If running in standby mode - wait until the queue manager becomes active for { - active, _ := ready.IsRunningAsActiveQM(context.Background(), qmName) - if active { + status, _ := ready.Status(context.Background(), qmName) + if status.ActiveQM() { break } time.Sleep(requestTimeout * time.Second) diff --git a/internal/ready/ready.go b/internal/ready/ready.go index cc036d5..8c77a42 100644 --- a/internal/ready/ready.go +++ b/internal/ready/ready.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2018, 2022 +© Copyright IBM Corporation 2018, 2023 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -67,28 +67,38 @@ func Check() (bool, error) { return exists, nil } -// IsRunningAsActiveQM returns true if the queue manager is running in active mode -func IsRunningAsActiveQM(ctx context.Context, name string) (bool, error) { - return isRunningQM(ctx, name, "(RUNNING)") -} - -// IsRunningAsStandbyQM returns true if the queue manager is running in standby mode -func IsRunningAsStandbyQM(ctx context.Context, name string) (bool, error) { - return isRunningQM(ctx, name, "(RUNNING AS STANDBY)") -} - -// IsRunningAsReplicaQM returns true if the queue manager is running in replica mode -func IsRunningAsReplicaQM(ctx context.Context, name string) (bool, error) { - return isRunningQM(ctx, name, "(REPLICA)") -} - -func isRunningQM(ctx context.Context, name string, status string) (bool, error) { +// Status returns an enum representing the current running status of the queue manager +func Status(ctx context.Context, name string) (QMStatus, error) { out, _, err := command.RunContext(ctx, "dspmq", "-n", "-m", name) if err != nil { - return false, err + return StatusUnknown, err } - if strings.Contains(string(out), status) { - return true, nil + if strings.Contains(string(out), "(RUNNING)") { + return StatusActiveQM, nil } - return false, nil + if strings.Contains(string(out), "(RUNNING AS STANDBY)") { + return StatusStandbyQM, nil + } + if strings.Contains(string(out), "(REPLICA)") { + return StatusStandbyQM, nil + } + return StatusUnknown, nil } + +type QMStatus int + +const ( + StatusUnknown QMStatus = iota + StatusActiveQM + StatusStandbyQM + StatusReplicaQM +) + +// ActiveQM returns true if the queue manager is running in active mode +func (s QMStatus) ActiveQM() bool { return s == StatusActiveQM } + +// StandbyQM returns true if the queue manager is running in standby mode +func (s QMStatus) StandbyQM() bool { return s == StatusStandbyQM } + +// ReplicaQM returns true if the queue manager is running in replica mode +func (s QMStatus) ReplicaQM() bool { return s == StatusReplicaQM }