Skip to content

Commit ec2a01d

Browse files
authored
1 parent 2d3ebe8 commit ec2a01d

File tree

9 files changed

+36
-34
lines changed

9 files changed

+36
-34
lines changed

modules/context/access_log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func AccessLogger() func(http.Handler) http.Handler {
9292
RequestID: &requestID,
9393
})
9494
if err != nil {
95-
log.Error("Could not set up chi access logger: %v", err.Error())
95+
log.Error("Could not execute access logger template: %v", err.Error())
9696
}
9797

9898
logger.Info("%s", buf.String())

modules/context/response.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type ResponseWriter interface {
1313
http.Flusher
1414
Status() int
1515
Before(func(ResponseWriter))
16+
Size() int // used by access logger template
1617
}
1718

1819
var _ ResponseWriter = &Response{}
@@ -45,6 +46,10 @@ func (r *Response) Write(bs []byte) (int, error) {
4546
return size, nil
4647
}
4748

49+
func (r *Response) Size() int {
50+
return r.written
51+
}
52+
4853
// WriteHeader write status code
4954
func (r *Response) WriteHeader(statusCode int) {
5055
if !r.beforeExecuted {

modules/graceful/manager_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ func (g *Manager) DoGracefulRestart() {
244244
log.Error("Error whilst forking from PID: %d : %v", os.Getpid(), err)
245245
}
246246
}
247+
// doFork calls RestartProcess which starts a new Gitea process, so this parent process needs to exit
248+
// Otherwise some resources (eg: leveldb lock) will be held by this parent process and the new process will fail to start
249+
log.Info("PID: %d. Shutting down after forking ...", os.Getpid())
250+
g.doShutdown()
247251
} else {
248252
log.Info("PID: %d. Not set restartable. Shutting down...", os.Getpid())
249253
g.notify(stoppingMsg)

modules/graceful/restart_unix.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,7 @@ func RestartProcess() (int, error) {
109109
if err != nil {
110110
return 0, err
111111
}
112-
return process.Pid, nil
112+
processPid := process.Pid
113+
_ = process.Release() // no wait, so release
114+
return processPid, nil
113115
}

modules/private/actions.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"code.gitea.io/gitea/modules/setting"
1010
)
1111

12-
// Email structure holds a data for sending general emails
1312
type GenerateTokenRequest struct {
1413
Scope string
1514
}

modules/private/manager.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import (
1919
func Shutdown(ctx context.Context) ResponseExtra {
2020
reqURL := setting.LocalURL + "api/internal/manager/shutdown"
2121
req := newInternalRequest(ctx, reqURL, "POST")
22-
return requestJSONUserMsg(req, "Shutting down")
22+
return requestJSONClientMsg(req, "Shutting down")
2323
}
2424

2525
// Restart calls the internal restart function
2626
func Restart(ctx context.Context) ResponseExtra {
2727
reqURL := setting.LocalURL + "api/internal/manager/restart"
2828
req := newInternalRequest(ctx, reqURL, "POST")
29-
return requestJSONUserMsg(req, "Restarting")
29+
return requestJSONClientMsg(req, "Restarting")
3030
}
3131

3232
// FlushOptions represents the options for the flush call
@@ -42,35 +42,35 @@ func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) R
4242
if timeout > 0 {
4343
req.SetReadWriteTimeout(timeout + 10*time.Second)
4444
}
45-
return requestJSONUserMsg(req, "Flushed")
45+
return requestJSONClientMsg(req, "Flushed")
4646
}
4747

4848
// PauseLogging pauses logging
4949
func PauseLogging(ctx context.Context) ResponseExtra {
5050
reqURL := setting.LocalURL + "api/internal/manager/pause-logging"
5151
req := newInternalRequest(ctx, reqURL, "POST")
52-
return requestJSONUserMsg(req, "Logging Paused")
52+
return requestJSONClientMsg(req, "Logging Paused")
5353
}
5454

5555
// ResumeLogging resumes logging
5656
func ResumeLogging(ctx context.Context) ResponseExtra {
5757
reqURL := setting.LocalURL + "api/internal/manager/resume-logging"
5858
req := newInternalRequest(ctx, reqURL, "POST")
59-
return requestJSONUserMsg(req, "Logging Restarted")
59+
return requestJSONClientMsg(req, "Logging Restarted")
6060
}
6161

6262
// ReleaseReopenLogging releases and reopens logging files
6363
func ReleaseReopenLogging(ctx context.Context) ResponseExtra {
6464
reqURL := setting.LocalURL + "api/internal/manager/release-and-reopen-logging"
6565
req := newInternalRequest(ctx, reqURL, "POST")
66-
return requestJSONUserMsg(req, "Logging Restarted")
66+
return requestJSONClientMsg(req, "Logging Restarted")
6767
}
6868

6969
// SetLogSQL sets database logging
7070
func SetLogSQL(ctx context.Context, on bool) ResponseExtra {
7171
reqURL := setting.LocalURL + "api/internal/manager/set-log-sql?on=" + strconv.FormatBool(on)
7272
req := newInternalRequest(ctx, reqURL, "POST")
73-
return requestJSONUserMsg(req, "Log SQL setting set")
73+
return requestJSONClientMsg(req, "Log SQL setting set")
7474
}
7575

7676
// LoggerOptions represents the options for the add logger call
@@ -90,14 +90,14 @@ func AddLogger(ctx context.Context, logger, writer, mode string, config map[stri
9090
Mode: mode,
9191
Config: config,
9292
})
93-
return requestJSONUserMsg(req, "Added")
93+
return requestJSONClientMsg(req, "Added")
9494
}
9595

9696
// RemoveLogger removes a logger
9797
func RemoveLogger(ctx context.Context, logger, writer string) ResponseExtra {
9898
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(logger), url.PathEscape(writer))
9999
req := newInternalRequest(ctx, reqURL, "POST")
100-
return requestJSONUserMsg(req, "Removed")
100+
return requestJSONClientMsg(req, "Removed")
101101
}
102102

103103
// Processes return the current processes from this gitea instance
@@ -108,6 +108,6 @@ func Processes(ctx context.Context, out io.Writer, flat, noSystem, stacktraces,
108108
callback := func(resp *http.Response, extra *ResponseExtra) {
109109
_, extra.Error = io.Copy(out, resp.Body)
110110
}
111-
_, extra := requestJSONResp(req, &callback)
111+
_, extra := requestJSONResp(req, &responseCallback{callback})
112112
return extra
113113
}

modules/private/request.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10-
"unicode"
1110

1211
"code.gitea.io/gitea/modules/httplib"
1312
"code.gitea.io/gitea/modules/json"
@@ -25,7 +24,9 @@ type ResponseExtra struct {
2524
Error error
2625
}
2726

28-
type responseCallback func(resp *http.Response, extra *ResponseExtra)
27+
type responseCallback struct {
28+
Callback func(resp *http.Response, extra *ResponseExtra)
29+
}
2930

3031
func (re *ResponseExtra) HasError() bool {
3132
return re.Error != nil
@@ -43,7 +44,7 @@ func (re responseError) Error() string {
4344
return fmt.Sprintf("internal API error response, status=%d, err=%s", re.statusCode, re.errorString)
4445
}
4546

46-
// requestJSONUserMsg sends a request to the gitea server and then parses the response.
47+
// requestJSONResp sends a request to the gitea server and then parses the response.
4748
// If the status code is not 2xx, or any error occurs, the ResponseExtra.Error field is guaranteed to be non-nil,
4849
// and the ResponseExtra.UserMsg field will be set to a message for the end user.
4950
//
@@ -89,10 +90,10 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
8990
}
9091
respText.Text = string(bs)
9192
return res, extra
92-
} else if callback, ok := v.(*responseCallback); ok {
93+
} else if cb, ok := v.(*responseCallback); ok {
9394
// pass the response to callback, and let the callback update the ResponseExtra
9495
extra.StatusCode = resp.StatusCode
95-
(*callback)(resp, &extra)
96+
cb.Callback(resp, &extra)
9697
return nil, extra
9798
} else if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
9899
// decode the response into the given struct
@@ -114,22 +115,13 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
114115
return res, extra
115116
}
116117

117-
// requestJSONUserMsg sends a request to the gitea server and then parses the response as private.Response
118-
// If the request succeeds, the successMsg will be used as part of ResponseExtra.UserMsg.
119-
func requestJSONUserMsg(req *httplib.Request, successMsg string) ResponseExtra {
120-
resp, extra := requestJSONResp(req, &Response{})
118+
// requestJSONClientMsg sends a request to the gitea server, server only responds text message status=200 with "success" body
119+
// If the request succeeds (200), the argument clientSuccessMsg will be used as ResponseExtra.UserMsg.
120+
func requestJSONClientMsg(req *httplib.Request, clientSuccessMsg string) ResponseExtra {
121+
_, extra := requestJSONResp(req, &responseText{})
121122
if extra.HasError() {
122123
return extra
123124
}
124-
if resp.UserMsg == "" {
125-
extra.UserMsg = successMsg // if UserMsg is empty, then use successMsg as userMsg
126-
} else if successMsg != "" {
127-
// else, now UserMsg is not empty, if successMsg is not empty, then append successMsg to UserMsg
128-
if unicode.IsPunct(rune(extra.UserMsg[len(extra.UserMsg)-1])) {
129-
extra.UserMsg = extra.UserMsg + " " + successMsg
130-
} else {
131-
extra.UserMsg = extra.UserMsg + ". " + successMsg
132-
}
133-
}
125+
extra.UserMsg = clientSuccessMsg
134126
return extra
135127
}

modules/private/restore_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ func RestoreRepo(ctx context.Context, repoDir, ownerName, repoName string, units
3232
Validation: validation,
3333
})
3434
req.SetTimeout(3*time.Second, 0) // since the request will spend much time, don't timeout
35-
return requestJSONUserMsg(req, fmt.Sprintf("Restore repo %s/%s successfully", ownerName, repoName))
35+
return requestJSONClientMsg(req, fmt.Sprintf("Restore repo %s/%s successfully", ownerName, repoName))
3636
}

routers/private/restore_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
4848
Err: err.Error(),
4949
})
5050
} else {
51-
ctx.Status(http.StatusOK)
51+
ctx.PlainText(http.StatusOK, "success")
5252
}
5353
}

0 commit comments

Comments
 (0)