123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- // Copyright 2020 The Gitea Authors. All rights reserved.
- // SPDX-License-Identifier: MIT
-
- package cmd
-
- import (
- "fmt"
- "net/http"
- "os"
- "time"
-
- "code.gitea.io/gitea/modules/private"
-
- "github.com/urfave/cli"
- )
-
- var (
- // CmdManager represents the manager command
- CmdManager = cli.Command{
- Name: "manager",
- Usage: "Manage the running gitea process",
- Description: "This is a command for managing the running gitea process",
- Subcommands: []cli.Command{
- subcmdShutdown,
- subcmdRestart,
- subcmdFlushQueues,
- subcmdLogging,
- subCmdProcesses,
- },
- }
- subcmdShutdown = cli.Command{
- Name: "shutdown",
- Usage: "Gracefully shutdown the running process",
- Flags: []cli.Flag{
- cli.BoolFlag{
- Name: "debug",
- },
- },
- Action: runShutdown,
- }
- subcmdRestart = cli.Command{
- Name: "restart",
- Usage: "Gracefully restart the running process - (not implemented for windows servers)",
- Flags: []cli.Flag{
- cli.BoolFlag{
- Name: "debug",
- },
- },
- Action: runRestart,
- }
- subcmdFlushQueues = cli.Command{
- Name: "flush-queues",
- Usage: "Flush queues in the running process",
- Action: runFlushQueues,
- Flags: []cli.Flag{
- cli.DurationFlag{
- Name: "timeout",
- Value: 60 * time.Second,
- Usage: "Timeout for the flushing process",
- },
- cli.BoolFlag{
- Name: "non-blocking",
- Usage: "Set to true to not wait for flush to complete before returning",
- },
- cli.BoolFlag{
- Name: "debug",
- },
- },
- }
- subCmdProcesses = cli.Command{
- Name: "processes",
- Usage: "Display running processes within the current process",
- Action: runProcesses,
- Flags: []cli.Flag{
- cli.BoolFlag{
- Name: "debug",
- },
- cli.BoolFlag{
- Name: "flat",
- Usage: "Show processes as flat table rather than as tree",
- },
- cli.BoolFlag{
- Name: "no-system",
- Usage: "Do not show system processes",
- },
- cli.BoolFlag{
- Name: "stacktraces",
- Usage: "Show stacktraces",
- },
- cli.BoolFlag{
- Name: "json",
- Usage: "Output as json",
- },
- cli.StringFlag{
- Name: "cancel",
- Usage: "Process PID to cancel. (Only available for non-system processes.)",
- },
- },
- }
- )
-
- func runShutdown(c *cli.Context) error {
- ctx, cancel := installSignals()
- defer cancel()
-
- setup("manager", c.Bool("debug"))
- statusCode, msg := private.Shutdown(ctx)
- switch statusCode {
- case http.StatusInternalServerError:
- return fail("InternalServerError", msg)
- }
-
- fmt.Fprintln(os.Stdout, msg)
- return nil
- }
-
- func runRestart(c *cli.Context) error {
- ctx, cancel := installSignals()
- defer cancel()
-
- setup("manager", c.Bool("debug"))
- statusCode, msg := private.Restart(ctx)
- switch statusCode {
- case http.StatusInternalServerError:
- return fail("InternalServerError", msg)
- }
-
- fmt.Fprintln(os.Stdout, msg)
- return nil
- }
-
- func runFlushQueues(c *cli.Context) error {
- ctx, cancel := installSignals()
- defer cancel()
-
- setup("manager", c.Bool("debug"))
- statusCode, msg := private.FlushQueues(ctx, c.Duration("timeout"), c.Bool("non-blocking"))
- switch statusCode {
- case http.StatusInternalServerError:
- return fail("InternalServerError", msg)
- }
-
- fmt.Fprintln(os.Stdout, msg)
- return nil
- }
-
- func runProcesses(c *cli.Context) error {
- ctx, cancel := installSignals()
- defer cancel()
-
- setup("manager", c.Bool("debug"))
- statusCode, msg := private.Processes(ctx, os.Stdout, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel"))
- switch statusCode {
- case http.StatusInternalServerError:
- return fail("InternalServerError", msg)
- }
-
- return nil
- }
|