-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3005 from dgageot/extract-graceful
Extract code that handles graceful termination
- Loading branch information
Showing
4 changed files
with
127 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package misc | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func HandleGracefulTermination(ctx context.Context, cmd *exec.Cmd) error { | ||
done := make(chan struct{}) | ||
defer close(done) | ||
|
||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
// On windows we can't send specific signals to processes, so we kill the process immediately | ||
if runtime.GOOS == "windows" { | ||
cmd.Process.Kill() | ||
return | ||
} | ||
|
||
logrus.Debugf("Sending SIGINT to process %v\n", cmd.Process.Pid) | ||
if err := cmd.Process.Signal(os.Interrupt); err != nil { | ||
// kill process on error | ||
cmd.Process.Kill() | ||
} | ||
|
||
// wait 2 seconds or wait for the process to complete | ||
select { | ||
case <-time.After(2 * time.Second): | ||
logrus.Debugf("Killing process %v\n", cmd.Process.Pid) | ||
// forcefully kill process after 2 seconds grace period | ||
cmd.Process.Kill() | ||
case <-done: | ||
return | ||
} | ||
case <-done: | ||
return | ||
} | ||
}() | ||
|
||
return cmd.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package misc | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestGracefulBuildCancel(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("graceful cancel doesn't work on windows") | ||
} | ||
|
||
tests := []struct { | ||
description string | ||
command string | ||
shouldErr bool | ||
}{ | ||
{ | ||
description: "terminate gracefully and exit 0", | ||
command: "trap 'echo trap' INT; sleep 2", | ||
}, { | ||
description: "terminate gracefully and kill process", | ||
command: "trap 'echo trap' INT; sleep 5", | ||
shouldErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
testutil.Run(t, test.description, func(t *testutil.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
|
||
cmd := exec.Command("bash", "-c", test.command) | ||
t.CheckNoError(cmd.Start()) | ||
|
||
err := HandleGracefulTermination(ctx, cmd) | ||
t.CheckError(test.shouldErr, err) | ||
|
||
cancel() | ||
}) | ||
} | ||
} |