Skip to content
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

Improve RunMode / dev mode #24886

Merged
merged 4 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ func loadRunModeFrom(rootCfg ConfigProvider) {
if RunMode == "" {
RunMode = rootSec.Key("RUN_MODE").MustString("prod")
}
IsProd = strings.EqualFold(RunMode, "prod")

// non-dev mode is treated as prod mode, to protect users from accidentally running in dev mode if there is a typo in this value.
RunMode = strings.ToLower(RunMode)
if RunMode != "dev" {
RunMode = "prod"
}
IsProd = RunMode != "dev"
silverwind marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +255 to +260
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this warrants a log message if it's not within the "enum" we expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do no think it is necessary or must.

  • Each time Gitea starts, there is already a "RunMode: prod" message. If it doesn't match user's expectation, they could go to the manual and config to check.
  • There was no such warning before, no user complains.
  • There are already a lot of messages during startup, I doubt few people really read them if there is no error.

So , I think we do not need an extra warning message here.


// check if we run as root
if os.Getuid() == 0 {
Expand Down
21 changes: 14 additions & 7 deletions modules/templates/htmlrenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func HTMLRenderer() *HTMLRender {
}

func ReloadHTMLTemplates() error {
log.Trace("Reloading HTML templates")
if err := htmlRender.CompileTemplates(); err != nil {
log.Error("Template error: %v\n%s", err, log.Stack(2))
return err
Expand All @@ -114,11 +115,11 @@ func initHTMLRenderer() {
htmlRender = &HTMLRender{}
if err := htmlRender.CompileTemplates(); err != nil {
p := &templateErrorPrettier{assets: AssetFS()}
wrapFatal(p.handleFuncNotDefinedError(err))
wrapFatal(p.handleUnexpectedOperandError(err))
wrapFatal(p.handleExpectedEndError(err))
wrapFatal(p.handleGenericTemplateError(err))
log.Fatal("HTMLRenderer CompileTemplates error: %v", err)
wrapTmplErrMsg(p.handleFuncNotDefinedError(err))
wrapTmplErrMsg(p.handleUnexpectedOperandError(err))
wrapTmplErrMsg(p.handleExpectedEndError(err))
wrapTmplErrMsg(p.handleGenericTemplateError(err))
wrapTmplErrMsg(fmt.Sprintf("CompileTemplates error: %v", err))
}

if !setting.IsProd {
Expand All @@ -128,11 +129,17 @@ func initHTMLRenderer() {
}
}

func wrapFatal(msg string) {
func wrapTmplErrMsg(msg string) {
if msg == "" {
return
}
log.Fatal("Unable to compile templates, %s", msg)
if setting.IsProd {
// in prod mode, Gitea must have correct templates to run
log.Fatal("Gitea can't run with template errors: %s", msg)
} else {
// in dev mode, do not need to really exit, because the template errors could be fixed by developer soon and the templates get reloaded
log.Error("There are template errors but Gitea continues to run in dev mode: %s", msg)
}
}

type templateErrorPrettier struct {
Expand Down
15 changes: 11 additions & 4 deletions modules/templates/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
bodyTemplates.Funcs(NewFuncMap())

assetFS := AssetFS()
refreshTemplates := func() {
refreshTemplates := func(firstRun bool) {
if !firstRun {
log.Trace("Reloading mail templates")
}
assetPaths, err := ListMailTemplateAssetNames(assetFS)
if err != nil {
log.Error("Failed to list mail templates: %v", err)
Expand All @@ -75,17 +78,21 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
continue
}
tmplName := strings.TrimPrefix(strings.TrimSuffix(assetPath, ".tmpl"), "mail/")
log.Trace("Adding mail template %s: %s by %s", tmplName, assetPath, layerName)
if firstRun {
log.Trace("Adding mail template %s: %s by %s", tmplName, assetPath, layerName)
}
buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, tmplName, content)
}
}

refreshTemplates()
refreshTemplates(true)

if !setting.IsProd {
// Now subjectTemplates and bodyTemplates are both synchronized
// thus it is safe to call refresh from a different goroutine
go assetFS.WatchLocalChanges(ctx, refreshTemplates)
go assetFS.WatchLocalChanges(ctx, func() {
refreshTemplates(false)
})
}

return subjectTemplates, bodyTemplates
Expand Down