eager update app settings and added isServe check for the auto backups
This commit is contained in:
parent
a56a04ed0e
commit
5551f8f5aa
|
@ -251,12 +251,13 @@ func (app *BaseApp) RestoreBackup(ctx context.Context, name string) error {
|
||||||
// @todo add tests
|
// @todo add tests
|
||||||
func (app *BaseApp) initAutobackupHooks() error {
|
func (app *BaseApp) initAutobackupHooks() error {
|
||||||
c := cron.New()
|
c := cron.New()
|
||||||
|
isServe := false
|
||||||
|
|
||||||
loadJob := func() {
|
loadJob := func() {
|
||||||
c.Stop()
|
c.Stop()
|
||||||
|
|
||||||
rawSchedule := app.Settings().Backups.Cron
|
rawSchedule := app.Settings().Backups.Cron
|
||||||
if rawSchedule == "" || !app.IsBootstrapped() {
|
if rawSchedule == "" || !isServe || !app.IsBootstrapped() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,6 +322,7 @@ func (app *BaseApp) initAutobackupHooks() error {
|
||||||
|
|
||||||
// load on app serve
|
// load on app serve
|
||||||
app.OnBeforeServe().Add(func(e *ServeEvent) error {
|
app.OnBeforeServe().Add(func(e *ServeEvent) error {
|
||||||
|
isServe = true
|
||||||
loadJob()
|
loadJob()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -333,10 +335,6 @@ func (app *BaseApp) initAutobackupHooks() error {
|
||||||
|
|
||||||
// reload on app settings change
|
// reload on app settings change
|
||||||
app.OnModelAfterUpdate((&models.Param{}).TableName()).Add(func(e *ModelEvent) error {
|
app.OnModelAfterUpdate((&models.Param{}).TableName()).Add(func(e *ModelEvent) error {
|
||||||
if !c.HasStarted() {
|
|
||||||
return nil // no need to reload as it hasn't been started yet
|
|
||||||
}
|
|
||||||
|
|
||||||
p := e.Model.(*models.Param)
|
p := e.Model.(*models.Param)
|
||||||
if p == nil || p.Key != models.ParamAppSettings {
|
if p == nil || p.Key != models.ParamAppSettings {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -58,8 +58,22 @@ func (form *SettingsUpsert) Submit(interceptors ...InterceptorFunc[*settings.Set
|
||||||
return runInterceptors(form.Settings, func(s *settings.Settings) error {
|
return runInterceptors(form.Settings, func(s *settings.Settings) error {
|
||||||
form.Settings = s
|
form.Settings = s
|
||||||
|
|
||||||
|
oldSettings, err := form.app.Settings().Clone();
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// eagerly merge the application settings with the form ones
|
||||||
|
if err := form.app.Settings().Merge(form.Settings); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// persists settings change
|
||||||
encryptionKey := os.Getenv(form.app.EncryptionEnv())
|
encryptionKey := os.Getenv(form.app.EncryptionEnv())
|
||||||
if err := form.dao.SaveSettings(form.Settings, encryptionKey); err != nil {
|
if err := form.dao.SaveSettings(form.Settings, encryptionKey); err != nil {
|
||||||
|
// try to revert app settings
|
||||||
|
form.app.Settings().Merge(oldSettings)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +87,6 @@ func (form *SettingsUpsert) Submit(interceptors ...InterceptorFunc[*settings.Set
|
||||||
form.app.LogsDao().Vacuum()
|
form.app.LogsDao().Vacuum()
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge the application settings with the form ones
|
return nil
|
||||||
return form.app.Settings().Merge(form.Settings)
|
|
||||||
}, interceptors...)
|
}, interceptors...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue