Merge pull request #284 from mq-cloudpak/add-timeout-to-chk-calls

update chkmq* cmds to use context to cancel when taking too long
This commit is contained in:
Jack Evans
2022-08-03 13:03:19 +01:00
committed by GitHub Enterprise
7 changed files with 76 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2017, 2020 © Copyright IBM Corporation 2017, 2022
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -18,22 +18,24 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"strings" "strings"
"github.com/ibm-messaging/mq-container/pkg/name" "github.com/ibm-messaging/mq-container/pkg/name"
) )
func queueManagerHealthy() (bool, error) { func queueManagerHealthy(ctx context.Context) (bool, error) {
name, err := name.GetQueueManagerName() name, err := name.GetQueueManagerName()
if err != nil { if err != nil {
return false, err return false, err
} }
// Specify the queue manager name, just in case someone's created a second queue manager // Specify the queue manager name, just in case someone's created a second queue manager
// #nosec G204 // #nosec G204
cmd := exec.Command("dspmq", "-n", "-m", name) cmd := exec.CommandContext(ctx, "dspmq", "-n", "-m", name)
// Run the command and wait for completion // Run the command and wait for completion
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
fmt.Printf("%s", out) fmt.Printf("%s", out)
@@ -47,13 +49,20 @@ func queueManagerHealthy() (bool, error) {
return true, nil return true, nil
} }
func main() { func doMain() int {
healthy, err := queueManagerHealthy() ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
healthy, err := queueManagerHealthy(ctx)
if err != nil { if err != nil {
os.Exit(2) return 2
} }
if !healthy { if !healthy {
os.Exit(1) return 1
} }
os.Exit(0) return 0
}
func main() {
os.Exit(doMain())
} }

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2017, 2019 © Copyright IBM Corporation 2017, 2022
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -18,44 +18,54 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"os" "os"
"os/signal"
"github.com/ibm-messaging/mq-container/internal/ready" "github.com/ibm-messaging/mq-container/internal/ready"
"github.com/ibm-messaging/mq-container/pkg/name" "github.com/ibm-messaging/mq-container/pkg/name"
) )
func main() { func doMain() int {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
// Check if runmqserver has indicated that it's finished configuration // Check if runmqserver has indicated that it's finished configuration
r, err := ready.Check() r, err := ready.Check()
if !r || err != nil { if !r || err != nil {
os.Exit(1) return 1
} }
name, err := name.GetQueueManagerName() name, err := name.GetQueueManagerName()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) return 1
} }
// Check if the queue manager has a running listener // Check if the queue manager has a running listener
if active, _ := ready.IsRunningAsActiveQM(name); active { if active, _ := ready.IsRunningAsActiveQM(ctx, name); active {
conn, err := net.Dial("tcp", "127.0.0.1:1414") conn, err := net.Dial("tcp", "127.0.0.1:1414")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) return 1
} }
err = conn.Close() err = conn.Close()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
} else if standby, _ := ready.IsRunningAsStandbyQM(name); standby { } else if standby, _ := ready.IsRunningAsStandbyQM(ctx, name); standby {
fmt.Printf("Detected queue manager running in standby mode") fmt.Printf("Detected queue manager running in standby mode")
os.Exit(10) return 10
} else if replica, _ := ready.IsRunningAsReplicaQM(name); replica { } else if replica, _ := ready.IsRunningAsReplicaQM(ctx, name); replica {
fmt.Printf("Detected queue manager running in replica mode") fmt.Printf("Detected queue manager running in replica mode")
os.Exit(20) return 20
} else { } else {
os.Exit(1) return 1
} }
return 0
}
func main() {
os.Exit(doMain())
} }

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2021 © Copyright IBM Corporation 2021, 2022
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -18,22 +18,24 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"strings" "strings"
"github.com/ibm-messaging/mq-container/pkg/name" "github.com/ibm-messaging/mq-container/pkg/name"
) )
func queueManagerStarted() (bool, error) { func queueManagerStarted(ctx context.Context) (bool, error) {
name, err := name.GetQueueManagerName() name, err := name.GetQueueManagerName()
if err != nil { if err != nil {
return false, err return false, err
} }
// Specify the queue manager name, just in case someone's created a second queue manager // Specify the queue manager name, just in case someone's created a second queue manager
// #nosec G204 // #nosec G204
cmd := exec.Command("dspmq", "-n", "-m", name) cmd := exec.CommandContext(ctx, "dspmq", "-n", "-m", name)
// Run the command and wait for completion // Run the command and wait for completion
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
@@ -46,13 +48,20 @@ func queueManagerStarted() (bool, error) {
return true, nil return true, nil
} }
func main() { func doMain() int {
started, err := queueManagerStarted() ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
started, err := queueManagerStarted(ctx)
if err != nil { if err != nil {
os.Exit(2) return 2
} }
if !started { if !started {
os.Exit(1) return 1
} }
os.Exit(0) return 0
}
func main() {
os.Exit(doMain())
} }

View File

@@ -16,6 +16,7 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@@ -131,7 +132,7 @@ func startQueueManager(name string) error {
func stopQueueManager(name string) error { func stopQueueManager(name string) error {
log.Println("Stopping queue manager") log.Println("Stopping queue manager")
qmGracePeriod := os.Getenv("MQ_GRACE_PERIOD") qmGracePeriod := os.Getenv("MQ_GRACE_PERIOD")
isStandby, err := ready.IsRunningAsStandbyQM(name) isStandby, err := ready.IsRunningAsStandbyQM(context.Background(), name)
if err != nil { if err != nil {
log.Printf("Error getting status for queue manager %v: %v", name, err.Error()) log.Printf("Error getting status for queue manager %v: %v", name, err.Error())
return err return err

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2017, 2020 © Copyright IBM Corporation 2017, 2022
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ limitations under the License.
package command package command
import ( import (
"context"
"fmt" "fmt"
"os/exec" "os/exec"
) )
@@ -27,9 +28,13 @@ import (
// Do not use this function to run shell built-ins (like "cd"), because // Do not use this function to run shell built-ins (like "cd"), because
// the error handling works differently // the error handling works differently
func Run(name string, arg ...string) (string, int, error) { func Run(name string, arg ...string) (string, int, error) {
return RunContext(context.Background(), name, arg...)
}
func RunContext(ctx context.Context, name string, arg ...string) (string, int, error) {
// Run the command and wait for completion // Run the command and wait for completion
// #nosec G204 // #nosec G204
cmd := exec.Command(name, arg...) cmd := exec.CommandContext(ctx, name, arg...)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
rc := cmd.ProcessState.ExitCode() rc := cmd.ProcessState.ExitCode()
if err != nil { if err != nil {

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2018, 2019 © Copyright IBM Corporation 2018, 2022
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ func GatherMetrics(qmName string, log *logger.Logger) {
// If running in standby mode - wait until the queue manager becomes active // If running in standby mode - wait until the queue manager becomes active
for { for {
active, _ := ready.IsRunningAsActiveQM(qmName) active, _ := ready.IsRunningAsActiveQM(context.Background(), qmName)
if active { if active {
break break
} }

View File

@@ -1,5 +1,5 @@
/* /*
© Copyright IBM Corporation 2018, 2019 © Copyright IBM Corporation 2018, 2022
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ limitations under the License.
package ready package ready
import ( import (
"context"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
@@ -67,22 +68,22 @@ func Check() (bool, error) {
} }
// IsRunningAsActiveQM returns true if the queue manager is running in active mode // IsRunningAsActiveQM returns true if the queue manager is running in active mode
func IsRunningAsActiveQM(name string) (bool, error) { func IsRunningAsActiveQM(ctx context.Context, name string) (bool, error) {
return isRunningQM(name, "(RUNNING)") return isRunningQM(ctx, name, "(RUNNING)")
} }
// IsRunningAsStandbyQM returns true if the queue manager is running in standby mode // IsRunningAsStandbyQM returns true if the queue manager is running in standby mode
func IsRunningAsStandbyQM(name string) (bool, error) { func IsRunningAsStandbyQM(ctx context.Context, name string) (bool, error) {
return isRunningQM(name, "(RUNNING AS STANDBY)") return isRunningQM(ctx, name, "(RUNNING AS STANDBY)")
} }
// IsRunningAsReplicaQM returns true if the queue manager is running in replica mode // IsRunningAsReplicaQM returns true if the queue manager is running in replica mode
func IsRunningAsReplicaQM(name string) (bool, error) { func IsRunningAsReplicaQM(ctx context.Context, name string) (bool, error) {
return isRunningQM(name, "(REPLICA)") return isRunningQM(ctx, name, "(REPLICA)")
} }
func isRunningQM(name string, status string) (bool, error) { func isRunningQM(ctx context.Context, name string, status string) (bool, error) {
out, _, err := command.Run("dspmq", "-n", "-m", name) out, _, err := command.RunContext(ctx, "dspmq", "-n", "-m", name)
if err != nil { if err != nil {
return false, err return false, err
} }