code.gitea.io/gitea@v1.22.3/modules/private/manager.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package private
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"io"
    10  	"net/http"
    11  	"net/url"
    12  	"strconv"
    13  	"time"
    14  
    15  	"code.gitea.io/gitea/modules/setting"
    16  )
    17  
    18  // Shutdown calls the internal shutdown function
    19  func Shutdown(ctx context.Context) ResponseExtra {
    20  	reqURL := setting.LocalURL + "api/internal/manager/shutdown"
    21  	req := newInternalRequest(ctx, reqURL, "POST")
    22  	return requestJSONClientMsg(req, "Shutting down")
    23  }
    24  
    25  // Restart calls the internal restart function
    26  func Restart(ctx context.Context) ResponseExtra {
    27  	reqURL := setting.LocalURL + "api/internal/manager/restart"
    28  	req := newInternalRequest(ctx, reqURL, "POST")
    29  	return requestJSONClientMsg(req, "Restarting")
    30  }
    31  
    32  // ReloadTemplates calls the internal reload-templates function
    33  func ReloadTemplates(ctx context.Context) ResponseExtra {
    34  	reqURL := setting.LocalURL + "api/internal/manager/reload-templates"
    35  	req := newInternalRequest(ctx, reqURL, "POST")
    36  	return requestJSONClientMsg(req, "Reloaded")
    37  }
    38  
    39  // FlushOptions represents the options for the flush call
    40  type FlushOptions struct {
    41  	Timeout     time.Duration
    42  	NonBlocking bool
    43  }
    44  
    45  // FlushQueues calls the internal flush-queues function
    46  func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) ResponseExtra {
    47  	reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
    48  	req := newInternalRequest(ctx, reqURL, "POST", FlushOptions{Timeout: timeout, NonBlocking: nonBlocking})
    49  	if timeout > 0 {
    50  		req.SetReadWriteTimeout(timeout + 10*time.Second)
    51  	}
    52  	return requestJSONClientMsg(req, "Flushed")
    53  }
    54  
    55  // PauseLogging pauses logging
    56  func PauseLogging(ctx context.Context) ResponseExtra {
    57  	reqURL := setting.LocalURL + "api/internal/manager/pause-logging"
    58  	req := newInternalRequest(ctx, reqURL, "POST")
    59  	return requestJSONClientMsg(req, "Logging Paused")
    60  }
    61  
    62  // ResumeLogging resumes logging
    63  func ResumeLogging(ctx context.Context) ResponseExtra {
    64  	reqURL := setting.LocalURL + "api/internal/manager/resume-logging"
    65  	req := newInternalRequest(ctx, reqURL, "POST")
    66  	return requestJSONClientMsg(req, "Logging Restarted")
    67  }
    68  
    69  // ReleaseReopenLogging releases and reopens logging files
    70  func ReleaseReopenLogging(ctx context.Context) ResponseExtra {
    71  	reqURL := setting.LocalURL + "api/internal/manager/release-and-reopen-logging"
    72  	req := newInternalRequest(ctx, reqURL, "POST")
    73  	return requestJSONClientMsg(req, "Logging Restarted")
    74  }
    75  
    76  // SetLogSQL sets database logging
    77  func SetLogSQL(ctx context.Context, on bool) ResponseExtra {
    78  	reqURL := setting.LocalURL + "api/internal/manager/set-log-sql?on=" + strconv.FormatBool(on)
    79  	req := newInternalRequest(ctx, reqURL, "POST")
    80  	return requestJSONClientMsg(req, "Log SQL setting set")
    81  }
    82  
    83  // LoggerOptions represents the options for the add logger call
    84  type LoggerOptions struct {
    85  	Logger string
    86  	Writer string
    87  	Mode   string
    88  	Config map[string]any
    89  }
    90  
    91  // AddLogger adds a logger
    92  func AddLogger(ctx context.Context, logger, writer, mode string, config map[string]any) ResponseExtra {
    93  	reqURL := setting.LocalURL + "api/internal/manager/add-logger"
    94  	req := newInternalRequest(ctx, reqURL, "POST", LoggerOptions{
    95  		Logger: logger,
    96  		Writer: writer,
    97  		Mode:   mode,
    98  		Config: config,
    99  	})
   100  	return requestJSONClientMsg(req, "Added")
   101  }
   102  
   103  // RemoveLogger removes a logger
   104  func RemoveLogger(ctx context.Context, logger, writer string) ResponseExtra {
   105  	reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(logger), url.PathEscape(writer))
   106  	req := newInternalRequest(ctx, reqURL, "POST")
   107  	return requestJSONClientMsg(req, "Removed")
   108  }
   109  
   110  // Processes return the current processes from this gitea instance
   111  func Processes(ctx context.Context, out io.Writer, flat, noSystem, stacktraces, json bool, cancel string) ResponseExtra {
   112  	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))
   113  
   114  	req := newInternalRequest(ctx, reqURL, "GET")
   115  	callback := func(resp *http.Response, extra *ResponseExtra) {
   116  		_, extra.Error = io.Copy(out, resp.Body)
   117  	}
   118  	_, extra := requestJSONResp(req, &responseCallback{callback})
   119  	return extra
   120  }