Skip to content

Commit 99e6d33

Browse files
stop running other profiles in group after receiving interrupt signal (#539)
1 parent adf90cb commit 99e6d33

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

run_profile.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"os/signal"
@@ -26,11 +27,23 @@ import (
2627
// Returns:
2728
// - error: An error if the profile or group run fails, or if the requested profile is not found.
2829
func startProfileOrGroup(ctx *Context, runProfile func(ctx *Context) error) error {
29-
if ctx.config.HasProfile(ctx.request.profile) {
30-
// if running as a systemd timer
31-
notifyStart()
32-
defer notifyStop()
30+
// Catch CTR-C keypress, or other signal sent by a service manager (systemd)
31+
sigChan := make(chan os.Signal, 1)
32+
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGABRT)
33+
// remove signal catch before leaving
34+
defer signal.Stop(sigChan)
35+
36+
ctx.sigChan = sigChan
37+
38+
// also use a go context to stop the loop of group profiles
39+
goCtx, cancelGoCtx := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGABRT)
40+
defer cancelGoCtx()
41+
42+
// if running as a systemd timer
43+
notifyStart()
44+
defer notifyStop()
3345

46+
if ctx.config.HasProfile(ctx.request.profile) {
3447
// Single profile run
3548
err := runProfile(ctx)
3649
if err != nil {
@@ -44,14 +57,14 @@ func startProfileOrGroup(ctx *Context, runProfile func(ctx *Context) error) erro
4457
clog.Errorf("cannot load group '%s': %v", ctx.request.profile, err)
4558
}
4659
if group != nil && len(group.Profiles) > 0 {
47-
// if running as a systemd timer
48-
notifyStart()
49-
defer notifyStop()
50-
5160
// profile name is the group name
5261
groupName := ctx.request.profile
5362

5463
for i, profileName := range group.Profiles {
64+
if goCtx.Err() != nil {
65+
clog.Warningf("interrupting group '%s' run", groupName)
66+
return nil
67+
}
5568
clog.Debugf("[%d/%d] starting profile '%s' from group '%s'", i+1, len(group.Profiles), profileName, groupName)
5669
ctx = ctx.WithProfile(profileName).WithGroup(groupName)
5770
err = runProfile(ctx)
@@ -173,13 +186,6 @@ func runProfile(ctx *Context) error {
173186
loadScheduledProfile(ctx)
174187
}
175188

176-
// Catch CTR-C keypress, or other signal sent by a service manager (systemd)
177-
sigChan := make(chan os.Signal, 1)
178-
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGABRT)
179-
// remove signal catch before leaving
180-
defer signal.Stop(sigChan)
181-
182-
ctx.sigChan = sigChan
183189
wrapper := newResticWrapper(ctx)
184190

185191
if ctx.noLock {

0 commit comments

Comments
 (0)