|
| 1 | +// Copyright 2020 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package private |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | + |
| 13 | + "code.gitea.io/gitea/modules/setting" |
| 14 | +) |
| 15 | + |
| 16 | +// Shutdown calls the internal shutdown function |
| 17 | +func Shutdown() (int, string) { |
| 18 | + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/shutdown") |
| 19 | + |
| 20 | + req := newInternalRequest(reqURL, "POST") |
| 21 | + resp, err := req.Response() |
| 22 | + if err != nil { |
| 23 | + return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error()) |
| 24 | + } |
| 25 | + defer resp.Body.Close() |
| 26 | + |
| 27 | + if resp.StatusCode != http.StatusOK { |
| 28 | + return resp.StatusCode, decodeJSONError(resp).Err |
| 29 | + } |
| 30 | + |
| 31 | + return http.StatusOK, "Shutting down" |
| 32 | +} |
| 33 | + |
| 34 | +// Restart calls the internal restart function |
| 35 | +func Restart() (int, string) { |
| 36 | + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/restart") |
| 37 | + |
| 38 | + req := newInternalRequest(reqURL, "POST") |
| 39 | + resp, err := req.Response() |
| 40 | + if err != nil { |
| 41 | + return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error()) |
| 42 | + } |
| 43 | + defer resp.Body.Close() |
| 44 | + |
| 45 | + if resp.StatusCode != http.StatusOK { |
| 46 | + return resp.StatusCode, decodeJSONError(resp).Err |
| 47 | + } |
| 48 | + |
| 49 | + return http.StatusOK, "Restarting" |
| 50 | +} |
| 51 | + |
| 52 | +// FlushOptions represents the options for the flush call |
| 53 | +type FlushOptions struct { |
| 54 | + Timeout time.Duration |
| 55 | + NonBlocking bool |
| 56 | +} |
| 57 | + |
| 58 | +// FlushQueues calls the internal flush-queues function |
| 59 | +func FlushQueues(timeout time.Duration, nonBlocking bool) (int, string) { |
| 60 | + reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/flush-queues") |
| 61 | + |
| 62 | + req := newInternalRequest(reqURL, "POST") |
| 63 | + if timeout > 0 { |
| 64 | + req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second) |
| 65 | + } |
| 66 | + req = req.Header("Content-Type", "application/json") |
| 67 | + jsonBytes, _ := json.Marshal(FlushOptions{ |
| 68 | + Timeout: timeout, |
| 69 | + NonBlocking: nonBlocking, |
| 70 | + }) |
| 71 | + req.Body(jsonBytes) |
| 72 | + resp, err := req.Response() |
| 73 | + if err != nil { |
| 74 | + return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error()) |
| 75 | + } |
| 76 | + defer resp.Body.Close() |
| 77 | + |
| 78 | + if resp.StatusCode != http.StatusOK { |
| 79 | + return resp.StatusCode, decodeJSONError(resp).Err |
| 80 | + } |
| 81 | + |
| 82 | + return http.StatusOK, "Flushed" |
| 83 | +} |
0 commit comments