Skip to content

Commit 2dbab58

Browse files
committed
as per delvh
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 950c474 commit 2dbab58

File tree

2 files changed

+40
-89
lines changed

2 files changed

+40
-89
lines changed

cmd/manager.go

Lines changed: 40 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
89
"io"
910
"net/http"
1011
"os"
12+
"reflect"
1113
"time"
1214

1315
"code.gitea.io/gitea/modules/private"
@@ -201,11 +203,17 @@ var (
201203
}
202204
)
203205

204-
func runShutdown(c *cli.Context) error {
206+
func setupManager(c *cli.Context) (context.Context, context.CancelFunc) {
205207
ctx, cancel := installSignals()
206-
defer cancel()
207208

208209
setup("manager", c.Bool("debug"))
210+
return ctx, cancel
211+
}
212+
213+
func runShutdown(c *cli.Context) error {
214+
ctx, cancel := setupManager(c)
215+
defer cancel()
216+
209217
statusCode, msg := private.Shutdown(ctx)
210218
switch statusCode {
211219
case http.StatusInternalServerError:
@@ -217,10 +225,9 @@ func runShutdown(c *cli.Context) error {
217225
}
218226

219227
func runRestart(c *cli.Context) error {
220-
ctx, cancel := installSignals()
228+
ctx, cancel := setupManager(c)
221229
defer cancel()
222230

223-
setup("manager", c.Bool("debug"))
224231
statusCode, msg := private.Restart(ctx)
225232
switch statusCode {
226233
case http.StatusInternalServerError:
@@ -232,10 +239,9 @@ func runRestart(c *cli.Context) error {
232239
}
233240

234241
func runFlushQueues(c *cli.Context) error {
235-
ctx, cancel := installSignals()
242+
ctx, cancel := setupManager(c)
236243
defer cancel()
237244

238-
setup("manager", c.Bool("debug"))
239245
statusCode, msg := private.FlushQueues(ctx, c.Duration("timeout"), c.Bool("non-blocking"))
240246
switch statusCode {
241247
case http.StatusInternalServerError:
@@ -263,18 +269,32 @@ func determineOutput(c *cli.Context, defaultFilename string) (io.WriteCloser, er
263269
return out, nil
264270
}
265271

266-
func runProcesses(c *cli.Context) error {
267-
ctx, cancel := installSignals()
272+
// runManagerPrivateFunc will requires that a provided fn has an interface:
273+
//
274+
// func(context.Context, io.Writer, ...argsTypes) (int, string) {
275+
//
276+
// but this cann't quite easily be expressed as a generic type
277+
func runManagerPrivateFunc(c *cli.Context, defaultOutput string, fn interface{}, args ...any) error {
278+
ctx, cancel := setupManager(c)
268279
defer cancel()
269280

270-
setup("manager", c.Bool("debug"))
271-
out, err := determineOutput(c, "-")
281+
out, err := determineOutput(c, defaultOutput)
272282
if err != nil {
273283
return err
274284
}
275285
defer out.Close()
276286

277-
statusCode, msg := private.Processes(ctx, out, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel"))
287+
valFn := reflect.ValueOf(fn)
288+
callArgs := []reflect.Value{
289+
reflect.ValueOf(ctx),
290+
reflect.ValueOf(out),
291+
}
292+
for _, arg := range args {
293+
callArgs = append(callArgs, reflect.ValueOf(arg))
294+
}
295+
outArgs := valFn.Call(callArgs)
296+
297+
statusCode, msg := outArgs[0].Interface().(int), outArgs[1].Interface().(string)
278298
switch statusCode {
279299
case http.StatusInternalServerError:
280300
return fail("InternalServerError", msg)
@@ -283,94 +303,26 @@ func runProcesses(c *cli.Context) error {
283303
return nil
284304
}
285305

286-
func runCPUProfile(c *cli.Context) error {
287-
ctx, cancel := installSignals()
288-
defer cancel()
289-
setup("manager", c.Bool("debug"))
290-
291-
out, err := determineOutput(c, "cpu-profile")
292-
if err != nil {
293-
return err
294-
}
295-
defer out.Close()
306+
func runProcesses(c *cli.Context) error {
307+
return runManagerPrivateFunc(c, "-", private.Processes, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel"))
308+
}
296309

297-
statusCode, msg := private.CPUProfile(ctx, out, c.Duration("duration"))
298-
switch statusCode {
299-
case http.StatusInternalServerError:
300-
return fail("InternalServerError", msg)
301-
}
302-
return nil
310+
func runCPUProfile(c *cli.Context) error {
311+
return runManagerPrivateFunc(c, "cpu-profile", private.CPUProfile, c.Duration("duration"))
303312
}
304313

305314
func runFGProfile(c *cli.Context) error {
306-
ctx, cancel := installSignals()
307-
defer cancel()
308-
setup("manager", c.Bool("debug"))
309-
out, err := determineOutput(c, "fg-profile")
310-
if err != nil {
311-
return err
312-
}
313-
defer out.Close()
314-
315-
statusCode, msg := private.FGProfile(ctx, out, c.Duration("duration"), c.String("format"))
316-
switch statusCode {
317-
case http.StatusInternalServerError:
318-
return fail("InternalServerError", msg)
319-
}
320-
return nil
315+
return runManagerPrivateFunc(c, "fg-profile", private.FGProfile, c.Duration("duration"), c.String("format"))
321316
}
322317

323318
func runNamedProfile(c *cli.Context) error {
324-
ctx, cancel := installSignals()
325-
defer cancel()
326-
setup("manager", c.Bool("debug"))
327-
out, err := determineOutput(c, c.String("name")+"-profile")
328-
if err != nil {
329-
return err
330-
}
331-
defer out.Close()
332-
statusCode, msg := private.NamedProfile(ctx, out, c.String("name"), c.Int("debug-level"))
333-
switch statusCode {
334-
case http.StatusInternalServerError:
335-
return fail("InternalServerError", msg)
336-
}
337-
return nil
319+
return runManagerPrivateFunc(c, c.String("name")+"-profile", private.NamedProfile, c.String("name"), c.Int("debug-level"))
338320
}
339321

340322
func runListNamedProfile(c *cli.Context) error {
341-
ctx, cancel := installSignals()
342-
defer cancel()
343-
setup("manager", c.Bool("debug"))
344-
345-
out, err := determineOutput(c, "-")
346-
if err != nil {
347-
return err
348-
}
349-
defer out.Close()
350-
351-
statusCode, msg := private.ListNamedProfiles(ctx, out, c.Bool("json"))
352-
switch statusCode {
353-
case http.StatusInternalServerError:
354-
return fail("InternalServerError", msg)
355-
}
356-
return nil
323+
return runManagerPrivateFunc(c, "-", private.ListNamedProfiles, c.Bool("json"))
357324
}
358325

359326
func runTrace(c *cli.Context) error {
360-
ctx, cancel := installSignals()
361-
defer cancel()
362-
setup("manager", c.Bool("debug"))
363-
364-
out, err := determineOutput(c, "trace")
365-
if err != nil {
366-
return err
367-
}
368-
defer out.Close()
369-
370-
statusCode, msg := private.Trace(ctx, out, c.Duration("duration"))
371-
switch statusCode {
372-
case http.StatusInternalServerError:
373-
return fail("InternalServerError", msg)
374-
}
375-
return nil
327+
return runManagerPrivateFunc(c, "trace", private.Trace, c.Duration("duration"))
376328
}

routers/web/admin/admin.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ func Monitor(ctx *context.Context) {
165165
ctx.Data["Entries"] = cron.ListTasks()
166166
ctx.Data["Queues"] = queue.GetManager().ManagedQueues()
167167

168-
169168
ctx.Data["Profiles"] = pprof.Profiles()
170169

171170
ctx.HTML(http.StatusOK, tplMonitor)

0 commit comments

Comments
 (0)