Skip to content

Commit fb53af6

Browse files
Jack EvansGitHub Enterprise
authored andcommitted
Merge pull request #284 from mq-cloudpak/add-timeout-to-chk-calls
update chkmq* cmds to use context to cancel when taking too long
2 parents b04ef21 + 65a36fd commit fb53af6

File tree

7 files changed

+76
-41
lines changed

7 files changed

+76
-41
lines changed

cmd/chkmqhealthy/main.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
© Copyright IBM Corporation 2017, 2020
2+
© Copyright IBM Corporation 2017, 2022
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,22 +18,24 @@ limitations under the License.
1818
package main
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"os"
2324
"os/exec"
25+
"os/signal"
2426
"strings"
2527

2628
"github.com/ibm-messaging/mq-container/pkg/name"
2729
)
2830

29-
func queueManagerHealthy() (bool, error) {
31+
func queueManagerHealthy(ctx context.Context) (bool, error) {
3032
name, err := name.GetQueueManagerName()
3133
if err != nil {
3234
return false, err
3335
}
3436
// Specify the queue manager name, just in case someone's created a second queue manager
3537
// #nosec G204
36-
cmd := exec.Command("dspmq", "-n", "-m", name)
38+
cmd := exec.CommandContext(ctx, "dspmq", "-n", "-m", name)
3739
// Run the command and wait for completion
3840
out, err := cmd.CombinedOutput()
3941
fmt.Printf("%s", out)
@@ -47,13 +49,20 @@ func queueManagerHealthy() (bool, error) {
4749
return true, nil
4850
}
4951

50-
func main() {
51-
healthy, err := queueManagerHealthy()
52+
func doMain() int {
53+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
54+
defer cancel()
55+
56+
healthy, err := queueManagerHealthy(ctx)
5257
if err != nil {
53-
os.Exit(2)
58+
return 2
5459
}
5560
if !healthy {
56-
os.Exit(1)
61+
return 1
5762
}
58-
os.Exit(0)
63+
return 0
64+
}
65+
66+
func main() {
67+
os.Exit(doMain())
5968
}

cmd/chkmqready/main.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
© Copyright IBM Corporation 2017, 2019
2+
© Copyright IBM Corporation 2017, 2022
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,44 +18,54 @@ limitations under the License.
1818
package main
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"net"
2324
"os"
25+
"os/signal"
2426

2527
"github.com/ibm-messaging/mq-container/internal/ready"
2628
"github.com/ibm-messaging/mq-container/pkg/name"
2729
)
2830

29-
func main() {
31+
func doMain() int {
32+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
33+
defer cancel()
34+
3035
// Check if runmqserver has indicated that it's finished configuration
3136
r, err := ready.Check()
3237
if !r || err != nil {
33-
os.Exit(1)
38+
return 1
3439
}
3540
name, err := name.GetQueueManagerName()
3641
if err != nil {
3742
fmt.Println(err)
38-
os.Exit(1)
43+
return 1
3944
}
4045

4146
// Check if the queue manager has a running listener
42-
if active, _ := ready.IsRunningAsActiveQM(name); active {
47+
if active, _ := ready.IsRunningAsActiveQM(ctx, name); active {
4348
conn, err := net.Dial("tcp", "127.0.0.1:1414")
4449
if err != nil {
4550
fmt.Println(err)
46-
os.Exit(1)
51+
return 1
4752
}
4853
err = conn.Close()
4954
if err != nil {
5055
fmt.Println(err)
5156
}
52-
} else if standby, _ := ready.IsRunningAsStandbyQM(name); standby {
57+
} else if standby, _ := ready.IsRunningAsStandbyQM(ctx, name); standby {
5358
fmt.Printf("Detected queue manager running in standby mode")
54-
os.Exit(10)
55-
} else if replica, _ := ready.IsRunningAsReplicaQM(name); replica {
59+
return 10
60+
} else if replica, _ := ready.IsRunningAsReplicaQM(ctx, name); replica {
5661
fmt.Printf("Detected queue manager running in replica mode")
57-
os.Exit(20)
62+
return 20
5863
} else {
59-
os.Exit(1)
64+
return 1
6065
}
66+
return 0
67+
}
68+
69+
func main() {
70+
os.Exit(doMain())
6171
}

cmd/chkmqstarted/main.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
© Copyright IBM Corporation 2021
2+
© Copyright IBM Corporation 2021, 2022
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,22 +18,24 @@ limitations under the License.
1818
package main
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"os"
2324
"os/exec"
25+
"os/signal"
2426
"strings"
2527

2628
"github.com/ibm-messaging/mq-container/pkg/name"
2729
)
2830

29-
func queueManagerStarted() (bool, error) {
31+
func queueManagerStarted(ctx context.Context) (bool, error) {
3032
name, err := name.GetQueueManagerName()
3133
if err != nil {
3234
return false, err
3335
}
3436
// Specify the queue manager name, just in case someone's created a second queue manager
3537
// #nosec G204
36-
cmd := exec.Command("dspmq", "-n", "-m", name)
38+
cmd := exec.CommandContext(ctx, "dspmq", "-n", "-m", name)
3739
// Run the command and wait for completion
3840
out, err := cmd.CombinedOutput()
3941
if err != nil {
@@ -46,13 +48,20 @@ func queueManagerStarted() (bool, error) {
4648
return true, nil
4749
}
4850

49-
func main() {
50-
started, err := queueManagerStarted()
51+
func doMain() int {
52+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
53+
defer cancel()
54+
55+
started, err := queueManagerStarted(ctx)
5156
if err != nil {
52-
os.Exit(2)
57+
return 2
5358
}
5459
if !started {
55-
os.Exit(1)
60+
return 1
5661
}
57-
os.Exit(0)
62+
return 0
63+
}
64+
65+
func main() {
66+
os.Exit(doMain())
5867
}

cmd/runmqserver/qmgr.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package main
1717

1818
import (
19+
"context"
1920
"fmt"
2021
"io/ioutil"
2122
"os"
@@ -131,7 +132,7 @@ func startQueueManager(name string) error {
131132
func stopQueueManager(name string) error {
132133
log.Println("Stopping queue manager")
133134
qmGracePeriod := os.Getenv("MQ_GRACE_PERIOD")
134-
isStandby, err := ready.IsRunningAsStandbyQM(name)
135+
isStandby, err := ready.IsRunningAsStandbyQM(context.Background(), name)
135136
if err != nil {
136137
log.Printf("Error getting status for queue manager %v: %v", name, err.Error())
137138
return err

internal/command/command.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
© Copyright IBM Corporation 2017, 2020
2+
© Copyright IBM Corporation 2017, 2022
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ limitations under the License.
1818
package command
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"os/exec"
2324
)
@@ -27,9 +28,13 @@ import (
2728
// Do not use this function to run shell built-ins (like "cd"), because
2829
// the error handling works differently
2930
func Run(name string, arg ...string) (string, int, error) {
31+
return RunContext(context.Background(), name, arg...)
32+
}
33+
34+
func RunContext(ctx context.Context, name string, arg ...string) (string, int, error) {
3035
// Run the command and wait for completion
3136
// #nosec G204
32-
cmd := exec.Command(name, arg...)
37+
cmd := exec.CommandContext(ctx, name, arg...)
3338
out, err := cmd.CombinedOutput()
3439
rc := cmd.ProcessState.ExitCode()
3540
if err != nil {

internal/metrics/metrics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
© Copyright IBM Corporation 2018, 2019
2+
© Copyright IBM Corporation 2018, 2022
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ func GatherMetrics(qmName string, log *logger.Logger) {
4343

4444
// If running in standby mode - wait until the queue manager becomes active
4545
for {
46-
active, _ := ready.IsRunningAsActiveQM(qmName)
46+
active, _ := ready.IsRunningAsActiveQM(context.Background(), qmName)
4747
if active {
4848
break
4949
}

internal/ready/ready.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
© Copyright IBM Corporation 2018, 2019
2+
© Copyright IBM Corporation 2018, 2022
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ limitations under the License.
1818
package ready
1919

2020
import (
21+
"context"
2122
"io/ioutil"
2223
"os"
2324
"strings"
@@ -67,22 +68,22 @@ func Check() (bool, error) {
6768
}
6869

6970
// IsRunningAsActiveQM returns true if the queue manager is running in active mode
70-
func IsRunningAsActiveQM(name string) (bool, error) {
71-
return isRunningQM(name, "(RUNNING)")
71+
func IsRunningAsActiveQM(ctx context.Context, name string) (bool, error) {
72+
return isRunningQM(ctx, name, "(RUNNING)")
7273
}
7374

7475
// IsRunningAsStandbyQM returns true if the queue manager is running in standby mode
75-
func IsRunningAsStandbyQM(name string) (bool, error) {
76-
return isRunningQM(name, "(RUNNING AS STANDBY)")
76+
func IsRunningAsStandbyQM(ctx context.Context, name string) (bool, error) {
77+
return isRunningQM(ctx, name, "(RUNNING AS STANDBY)")
7778
}
7879

7980
// IsRunningAsReplicaQM returns true if the queue manager is running in replica mode
80-
func IsRunningAsReplicaQM(name string) (bool, error) {
81-
return isRunningQM(name, "(REPLICA)")
81+
func IsRunningAsReplicaQM(ctx context.Context, name string) (bool, error) {
82+
return isRunningQM(ctx, name, "(REPLICA)")
8283
}
8384

84-
func isRunningQM(name string, status string) (bool, error) {
85-
out, _, err := command.Run("dspmq", "-n", "-m", name)
85+
func isRunningQM(ctx context.Context, name string, status string) (bool, error) {
86+
out, _, err := command.RunContext(ctx, "dspmq", "-n", "-m", name)
8687
if err != nil {
8788
return false, err
8889
}

0 commit comments

Comments
 (0)