summaryrefslogtreecommitdiffstats
path: root/modules/private/manager.go
blob: 5853db34e4b740e8e0539c0ad0cd000bb94b2a8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package private

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"strconv"
	"time"

	"code.gitea.io/gitea/modules/setting"
)

// Shutdown calls the internal shutdown function
func Shutdown(ctx context.Context) ResponseExtra {
	reqURL := setting.LocalURL + "api/internal/manager/shutdown"
	req := newInternalRequest(ctx, reqURL, "POST")
	return requestJSONUserMsg(req, "Shutting down")
}

// Restart calls the internal restart function
func Restart(ctx context.Context) ResponseExtra {
	reqURL := setting.LocalURL + "api/internal/manager/restart"
	req := newInternalRequest(ctx, reqURL, "POST")
	return requestJSONUserMsg(req, "Restarting")
}

// FlushOptions represents the options for the flush call
type FlushOptions struct {
	Timeout     time.Duration
	NonBlocking bool
}

// FlushQueues calls the internal flush-queues function
func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) ResponseExtra {
	reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
	req := newInternalRequest(ctx, reqURL, "POST", FlushOptions{Timeout: timeout, NonBlocking: nonBlocking})
	if timeout > 0 {
		req.SetReadWriteTimeout(timeout + 10*time.Second)
	}
	return requestJSONUserMsg(req, "Flushed")
}

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

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

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

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

// LoggerOptions represents the options for the add logger call
type LoggerOptions struct {
	Group  string
	Name   string
	Mode   string
	Config map[string]interface{}
}

// AddLogger adds a logger
func AddLogger(ctx context.Context, group, name, mode string, config map[string]interface{}) ResponseExtra {
	reqURL := setting.LocalURL + "api/internal/manager/add-logger"
	req := newInternalRequest(ctx, reqURL, "POST", LoggerOptions{
		Group:  group,
		Name:   name,
		Mode:   mode,
		Config: config,
	})
	return requestJSONUserMsg(req, "Added")
}

// RemoveLogger removes a logger
func RemoveLogger(ctx context.Context, group, name string) ResponseExtra {
	reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(group), url.PathEscape(name))
	req := newInternalRequest(ctx, reqURL, "POST")
	return requestJSONUserMsg(req, "Removed")
}

// Processes return the current processes from this gitea instance
func Processes(ctx context.Context, out io.Writer, flat, noSystem, stacktraces, json bool, cancel string) ResponseExtra {
	reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/processes?flat=%t&no-system=%t&stacktraces=%t&json=%t&cancel-pid=%s", flat, noSystem, stacktraces, json, url.QueryEscape(cancel))

	req := newInternalRequest(ctx, reqURL, "GET")
	callback := func(resp *http.Response, extra *ResponseExtra) {
		_, extra.Error = io.Copy(out, resp.Body)
	}
	_, extra := requestJSONResp(req, &callback)
	return extra
}