Skip to content

Add gitea manager reload-templates command #24843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 22, 2023
20 changes: 20 additions & 0 deletions cmd/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
Subcommands: []cli.Command{
subcmdShutdown,
subcmdRestart,
subcmdReloadTemplates,
subcmdFlushQueues,
subcmdLogging,
subCmdProcesses,
Expand All @@ -46,6 +47,16 @@ var (
},
Action: runRestart,
}
subcmdReloadTemplates = cli.Command{
Name: "reload-templates",
Usage: "Reload template files in the running process",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "debug",
},
},
Action: runReloadTemplates,
}
subcmdFlushQueues = cli.Command{
Name: "flush-queues",
Usage: "Flush queues in the running process",
Expand Down Expand Up @@ -115,6 +126,15 @@ func runRestart(c *cli.Context) error {
return handleCliResponseExtra(extra)
}

func runReloadTemplates(c *cli.Context) error {
ctx, cancel := installSignals()
defer cancel()

setup(ctx, c.Bool("debug"))
extra := private.ReloadTemplates(ctx)
return handleCliResponseExtra(extra)
}

func runFlushQueues(c *cli.Context) error {
ctx, cancel := installSignals()
defer cancel()
Expand Down
7 changes: 7 additions & 0 deletions modules/private/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func Restart(ctx context.Context) ResponseExtra {
return requestJSONClientMsg(req, "Restarting")
}

// ReloadTemplates calls the internal reload-templates function
func ReloadTemplates(ctx context.Context) ResponseExtra {
reqURL := setting.LocalURL + "api/internal/manager/reload-templates"
req := newInternalRequest(ctx, reqURL, "POST")
return requestJSONClientMsg(req, "Reloaded")
}

// FlushOptions represents the options for the flush call
type FlushOptions struct {
Timeout time.Duration
Expand Down
12 changes: 9 additions & 3 deletions modules/templates/htmlrenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ func HTMLRenderer() *HTMLRender {
return htmlRender
}

func ReloadHTMLTemplates() error {
if err := htmlRender.CompileTemplates(); err != nil {
log.Error("Template error: %v\n%s", err, log.Stack(2))
return err
}
return nil
}

func initHTMLRenderer() {
rendererType := "static"
if !setting.IsProd {
Expand All @@ -115,9 +123,7 @@ func initHTMLRenderer() {

if !setting.IsProd {
go AssetFS().WatchLocalChanges(graceful.GetManager().ShutdownContext(), func() {
if err := htmlRender.CompileTemplates(); err != nil {
log.Error("Template error: %v\n%s", err, log.Stack(2))
}
_ = ReloadHTMLTemplates()
})
}
}
Expand Down
1 change: 1 addition & 0 deletions routers/private/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func Routes() *web.Route {
r.Get("/serv/command/{keyid}/{owner}/{repo}", ServCommand)
r.Post("/manager/shutdown", Shutdown)
r.Post("/manager/restart", Restart)
r.Post("/manager/reload-templates", ReloadTemplates)
r.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues)
r.Post("/manager/pause-logging", PauseLogging)
r.Post("/manager/resume-logging", ResumeLogging)
Expand Down
13 changes: 13 additions & 0 deletions routers/private/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@ import (
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/web"
)

// ReloadTemplates reloads all the templates
func ReloadTemplates(ctx *context.PrivateContext) {
err := templates.ReloadHTMLTemplates()
if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
UserMsg: fmt.Sprintf("Template error: %v", err),
})
return
}
ctx.PlainText(http.StatusOK, "success")
}

// FlushQueues flushes all the Queues
func FlushQueues(ctx *context.PrivateContext) {
opts := web.GetForm(ctx).(*private.FlushOptions)
Expand Down