loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

authored by

Brecht Van Lommel and committed by
GitHub
3588edbb 922c83ee

+50 -3
+20
cmd/manager.go
··· 21 21 Subcommands: []cli.Command{ 22 22 subcmdShutdown, 23 23 subcmdRestart, 24 + subcmdReloadTemplates, 24 25 subcmdFlushQueues, 25 26 subcmdLogging, 26 27 subCmdProcesses, ··· 45 46 }, 46 47 }, 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, 48 59 } 49 60 subcmdFlushQueues = cli.Command{ 50 61 Name: "flush-queues", ··· 112 123 113 124 setup(ctx, c.Bool("debug")) 114 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) 115 135 return handleCliResponseExtra(extra) 116 136 } 117 137
+7
modules/private/manager.go
··· 29 29 return requestJSONClientMsg(req, "Restarting") 30 30 } 31 31 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 + 32 39 // FlushOptions represents the options for the flush call 33 40 type FlushOptions struct { 34 41 Timeout time.Duration
+9 -3
modules/templates/htmlrenderer.go
··· 96 96 return htmlRender 97 97 } 98 98 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 + 99 107 func initHTMLRenderer() { 100 108 rendererType := "static" 101 109 if !setting.IsProd { ··· 115 123 116 124 if !setting.IsProd { 117 125 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() 121 127 }) 122 128 } 123 129 }
+1
routers/private/internal.go
··· 67 67 r.Get("/serv/command/{keyid}/{owner}/{repo}", ServCommand) 68 68 r.Post("/manager/shutdown", Shutdown) 69 69 r.Post("/manager/restart", Restart) 70 + r.Post("/manager/reload-templates", ReloadTemplates) 70 71 r.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues) 71 72 r.Post("/manager/pause-logging", PauseLogging) 72 73 r.Post("/manager/resume-logging", ResumeLogging)
+13
routers/private/manager.go
··· 15 15 "code.gitea.io/gitea/modules/private" 16 16 "code.gitea.io/gitea/modules/queue" 17 17 "code.gitea.io/gitea/modules/setting" 18 + "code.gitea.io/gitea/modules/templates" 18 19 "code.gitea.io/gitea/modules/web" 19 20 ) 21 + 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 + } 20 33 21 34 // FlushQueues flushes all the Queues 22 35 func FlushQueues(ctx *context.PrivateContext) {