code.gitea.io/gitea@v1.21.7/cmd/manager.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cmd
     5  
     6  import (
     7  	"os"
     8  	"time"
     9  
    10  	"code.gitea.io/gitea/modules/private"
    11  
    12  	"github.com/urfave/cli/v2"
    13  )
    14  
    15  var (
    16  	// CmdManager represents the manager command
    17  	CmdManager = &cli.Command{
    18  		Name:        "manager",
    19  		Usage:       "Manage the running gitea process",
    20  		Description: "This is a command for managing the running gitea process",
    21  		Subcommands: []*cli.Command{
    22  			subcmdShutdown,
    23  			subcmdRestart,
    24  			subcmdReloadTemplates,
    25  			subcmdFlushQueues,
    26  			subcmdLogging,
    27  			subCmdProcesses,
    28  		},
    29  	}
    30  	subcmdShutdown = &cli.Command{
    31  		Name:  "shutdown",
    32  		Usage: "Gracefully shutdown the running process",
    33  		Flags: []cli.Flag{
    34  			&cli.BoolFlag{
    35  				Name: "debug",
    36  			},
    37  		},
    38  		Action: runShutdown,
    39  	}
    40  	subcmdRestart = &cli.Command{
    41  		Name:  "restart",
    42  		Usage: "Gracefully restart the running process - (not implemented for windows servers)",
    43  		Flags: []cli.Flag{
    44  			&cli.BoolFlag{
    45  				Name: "debug",
    46  			},
    47  		},
    48  		Action: runRestart,
    49  	}
    50  	subcmdReloadTemplates = &cli.Command{
    51  		Name:  "reload-templates",
    52  		Usage: "Reload template files in the running process",
    53  		Flags: []cli.Flag{
    54  			&cli.BoolFlag{
    55  				Name: "debug",
    56  			},
    57  		},
    58  		Action: runReloadTemplates,
    59  	}
    60  	subcmdFlushQueues = &cli.Command{
    61  		Name:   "flush-queues",
    62  		Usage:  "Flush queues in the running process",
    63  		Action: runFlushQueues,
    64  		Flags: []cli.Flag{
    65  			&cli.DurationFlag{
    66  				Name:  "timeout",
    67  				Value: 60 * time.Second,
    68  				Usage: "Timeout for the flushing process",
    69  			},
    70  			&cli.BoolFlag{
    71  				Name:  "non-blocking",
    72  				Usage: "Set to true to not wait for flush to complete before returning",
    73  			},
    74  			&cli.BoolFlag{
    75  				Name: "debug",
    76  			},
    77  		},
    78  	}
    79  	subCmdProcesses = &cli.Command{
    80  		Name:   "processes",
    81  		Usage:  "Display running processes within the current process",
    82  		Action: runProcesses,
    83  		Flags: []cli.Flag{
    84  			&cli.BoolFlag{
    85  				Name: "debug",
    86  			},
    87  			&cli.BoolFlag{
    88  				Name:  "flat",
    89  				Usage: "Show processes as flat table rather than as tree",
    90  			},
    91  			&cli.BoolFlag{
    92  				Name:  "no-system",
    93  				Usage: "Do not show system processes",
    94  			},
    95  			&cli.BoolFlag{
    96  				Name:  "stacktraces",
    97  				Usage: "Show stacktraces",
    98  			},
    99  			&cli.BoolFlag{
   100  				Name:  "json",
   101  				Usage: "Output as json",
   102  			},
   103  			&cli.StringFlag{
   104  				Name:  "cancel",
   105  				Usage: "Process PID to cancel. (Only available for non-system processes.)",
   106  			},
   107  		},
   108  	}
   109  )
   110  
   111  func runShutdown(c *cli.Context) error {
   112  	ctx, cancel := installSignals()
   113  	defer cancel()
   114  
   115  	setup(ctx, c.Bool("debug"))
   116  	extra := private.Shutdown(ctx)
   117  	return handleCliResponseExtra(extra)
   118  }
   119  
   120  func runRestart(c *cli.Context) error {
   121  	ctx, cancel := installSignals()
   122  	defer cancel()
   123  
   124  	setup(ctx, c.Bool("debug"))
   125  	extra := private.Restart(ctx)
   126  	return handleCliResponseExtra(extra)
   127  }
   128  
   129  func runReloadTemplates(c *cli.Context) error {
   130  	ctx, cancel := installSignals()
   131  	defer cancel()
   132  
   133  	setup(ctx, c.Bool("debug"))
   134  	extra := private.ReloadTemplates(ctx)
   135  	return handleCliResponseExtra(extra)
   136  }
   137  
   138  func runFlushQueues(c *cli.Context) error {
   139  	ctx, cancel := installSignals()
   140  	defer cancel()
   141  
   142  	setup(ctx, c.Bool("debug"))
   143  	extra := private.FlushQueues(ctx, c.Duration("timeout"), c.Bool("non-blocking"))
   144  	return handleCliResponseExtra(extra)
   145  }
   146  
   147  func runProcesses(c *cli.Context) error {
   148  	ctx, cancel := installSignals()
   149  	defer cancel()
   150  
   151  	setup(ctx, c.Bool("debug"))
   152  	extra := private.Processes(ctx, os.Stdout, c.Bool("flat"), c.Bool("no-system"), c.Bool("stacktraces"), c.Bool("json"), c.String("cancel"))
   153  	return handleCliResponseExtra(extra)
   154  }