Skip to content

Commit 3588edb

Browse files
authored
Add gitea manager reload-templates command (#24843)
This can be useful to update custom templates in production mode, when they are updated frequently and a full Gitea restart each time is disruptive.
1 parent 922c83e commit 3588edb

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

cmd/manager.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
Subcommands: []cli.Command{
2222
subcmdShutdown,
2323
subcmdRestart,
24+
subcmdReloadTemplates,
2425
subcmdFlushQueues,
2526
subcmdLogging,
2627
subCmdProcesses,
@@ -46,6 +47,16 @@ var (
4647
},
4748
Action: runRestart,
4849
}
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+
}
4960
subcmdFlushQueues = cli.Command{
5061
Name: "flush-queues",
5162
Usage: "Flush queues in the running process",
@@ -115,6 +126,15 @@ func runRestart(c *cli.Context) error {
115126
return handleCliResponseExtra(extra)
116127
}
117128

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+
118138
func runFlushQueues(c *cli.Context) error {
119139
ctx, cancel := installSignals()
120140
defer cancel()

modules/private/manager.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ func Restart(ctx context.Context) ResponseExtra {
2929
return requestJSONClientMsg(req, "Restarting")
3030
}
3131

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+
3239
// FlushOptions represents the options for the flush call
3340
type FlushOptions struct {
3441
Timeout time.Duration

modules/templates/htmlrenderer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func HTMLRenderer() *HTMLRender {
9696
return htmlRender
9797
}
9898

99+
func ReloadHTMLTemplates() error {
100+
if err := htmlRender.CompileTemplates(); err != nil {
101+
log.Error("Template error: %v\n%s", err, log.Stack(2))
102+
return err
103+
}
104+
return nil
105+
}
106+
99107
func initHTMLRenderer() {
100108
rendererType := "static"
101109
if !setting.IsProd {
@@ -115,9 +123,7 @@ func initHTMLRenderer() {
115123

116124
if !setting.IsProd {
117125
go AssetFS().WatchLocalChanges(graceful.GetManager().ShutdownContext(), func() {
118-
if err := htmlRender.CompileTemplates(); err != nil {
119-
log.Error("Template error: %v\n%s", err, log.Stack(2))
120-
}
126+
_ = ReloadHTMLTemplates()
121127
})
122128
}
123129
}

routers/private/internal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func Routes() *web.Route {
6767
r.Get("/serv/command/{keyid}/{owner}/{repo}", ServCommand)
6868
r.Post("/manager/shutdown", Shutdown)
6969
r.Post("/manager/restart", Restart)
70+
r.Post("/manager/reload-templates", ReloadTemplates)
7071
r.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues)
7172
r.Post("/manager/pause-logging", PauseLogging)
7273
r.Post("/manager/resume-logging", ResumeLogging)

routers/private/manager.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,22 @@ import (
1515
"code.gitea.io/gitea/modules/private"
1616
"code.gitea.io/gitea/modules/queue"
1717
"code.gitea.io/gitea/modules/setting"
18+
"code.gitea.io/gitea/modules/templates"
1819
"code.gitea.io/gitea/modules/web"
1920
)
2021

22+
// ReloadTemplates reloads all the templates
23+
func ReloadTemplates(ctx *context.PrivateContext) {
24+
err := templates.ReloadHTMLTemplates()
25+
if err != nil {
26+
ctx.JSON(http.StatusInternalServerError, private.Response{
27+
UserMsg: fmt.Sprintf("Template error: %v", err),
28+
})
29+
return
30+
}
31+
ctx.PlainText(http.StatusOK, "success")
32+
}
33+
2134
// FlushQueues flushes all the Queues
2235
func FlushQueues(ctx *context.PrivateContext) {
2336
opts := web.GetForm(ctx).(*private.FlushOptions)

0 commit comments

Comments
 (0)