Second attempt at preventing zombies (#16326)

* Second attempt at preventing zombies

* Ensure that the pipes are closed in ssh.go
* Ensure that a cancellable context is passed up in cmd/* http requests
* Make cmd.fail return properly so defers are obeyed
* Ensure that something is sent to stdout in case of blocks here

Signed-off-by: Andrew Thornton <art27@cantab.net>

* placate lint

Signed-off-by: Andrew Thornton <art27@cantab.net>

* placate lint 2

Signed-off-by: Andrew Thornton <art27@cantab.net>

* placate lint 3

Signed-off-by: Andrew Thornton <art27@cantab.net>

* fixup

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Apply suggestions from code review

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
zeripath 2021-07-14 15:43:13 +01:00 committed by GitHub
parent ee43d70a0c
commit 3dcb3e9073
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 229 additions and 143 deletions

View file

@ -5,6 +5,7 @@
package private
import (
"context"
"fmt"
"net/http"
"net/url"
@ -15,10 +16,10 @@ import (
)
// Shutdown calls the internal shutdown function
func Shutdown() (int, string) {
func Shutdown(ctx context.Context) (int, string) {
reqURL := setting.LocalURL + "api/internal/manager/shutdown"
req := newInternalRequest(reqURL, "POST")
req := newInternalRequest(ctx, reqURL, "POST")
resp, err := req.Response()
if err != nil {
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
@ -33,10 +34,10 @@ func Shutdown() (int, string) {
}
// Restart calls the internal restart function
func Restart() (int, string) {
func Restart(ctx context.Context) (int, string) {
reqURL := setting.LocalURL + "api/internal/manager/restart"
req := newInternalRequest(reqURL, "POST")
req := newInternalRequest(ctx, reqURL, "POST")
resp, err := req.Response()
if err != nil {
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
@ -57,10 +58,10 @@ type FlushOptions struct {
}
// FlushQueues calls the internal flush-queues function
func FlushQueues(timeout time.Duration, nonBlocking bool) (int, string) {
func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) (int, string) {
reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
req := newInternalRequest(reqURL, "POST")
req := newInternalRequest(ctx, reqURL, "POST")
if timeout > 0 {
req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second)
}
@ -85,10 +86,10 @@ func FlushQueues(timeout time.Duration, nonBlocking bool) (int, string) {
}
// PauseLogging pauses logging
func PauseLogging() (int, string) {
func PauseLogging(ctx context.Context) (int, string) {
reqURL := setting.LocalURL + "api/internal/manager/pause-logging"
req := newInternalRequest(reqURL, "POST")
req := newInternalRequest(ctx, reqURL, "POST")
resp, err := req.Response()
if err != nil {
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
@ -103,10 +104,10 @@ func PauseLogging() (int, string) {
}
// ResumeLogging resumes logging
func ResumeLogging() (int, string) {
func ResumeLogging(ctx context.Context) (int, string) {
reqURL := setting.LocalURL + "api/internal/manager/resume-logging"
req := newInternalRequest(reqURL, "POST")
req := newInternalRequest(ctx, reqURL, "POST")
resp, err := req.Response()
if err != nil {
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
@ -121,10 +122,10 @@ func ResumeLogging() (int, string) {
}
// ReleaseReopenLogging releases and reopens logging files
func ReleaseReopenLogging() (int, string) {
func ReleaseReopenLogging(ctx context.Context) (int, string) {
reqURL := setting.LocalURL + "api/internal/manager/release-and-reopen-logging"
req := newInternalRequest(reqURL, "POST")
req := newInternalRequest(ctx, reqURL, "POST")
resp, err := req.Response()
if err != nil {
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
@ -147,10 +148,10 @@ type LoggerOptions struct {
}
// AddLogger adds a logger
func AddLogger(group, name, mode string, config map[string]interface{}) (int, string) {
func AddLogger(ctx context.Context, group, name, mode string, config map[string]interface{}) (int, string) {
reqURL := setting.LocalURL + "api/internal/manager/add-logger"
req := newInternalRequest(reqURL, "POST")
req := newInternalRequest(ctx, reqURL, "POST")
req = req.Header("Content-Type", "application/json")
json := jsoniter.ConfigCompatibleWithStandardLibrary
jsonBytes, _ := json.Marshal(LoggerOptions{
@ -175,10 +176,10 @@ func AddLogger(group, name, mode string, config map[string]interface{}) (int, st
}
// RemoveLogger removes a logger
func RemoveLogger(group, name string) (int, string) {
func RemoveLogger(ctx context.Context, group, name string) (int, string) {
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(group), url.PathEscape(name))
req := newInternalRequest(reqURL, "POST")
req := newInternalRequest(ctx, reqURL, "POST")
resp, err := req.Response()
if err != nil {
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())