1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-12-01 02:56:49 +02:00

eager update app settings and added isServe check for the auto backups

This commit is contained in:
Gani Georgiev 2023-05-21 11:47:05 +03:00
parent a56a04ed0e
commit 5551f8f5aa
2 changed files with 18 additions and 7 deletions

View File

@ -251,12 +251,13 @@ func (app *BaseApp) RestoreBackup(ctx context.Context, name string) error {
// @todo add tests
func (app *BaseApp) initAutobackupHooks() error {
c := cron.New()
isServe := false
loadJob := func() {
c.Stop()
rawSchedule := app.Settings().Backups.Cron
if rawSchedule == "" || !app.IsBootstrapped() {
if rawSchedule == "" || !isServe || !app.IsBootstrapped() {
return
}
@ -321,6 +322,7 @@ func (app *BaseApp) initAutobackupHooks() error {
// load on app serve
app.OnBeforeServe().Add(func(e *ServeEvent) error {
isServe = true
loadJob()
return nil
})
@ -333,10 +335,6 @@ func (app *BaseApp) initAutobackupHooks() error {
// reload on app settings change
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)
if p == nil || p.Key != models.ParamAppSettings {
return nil

View File

@ -58,8 +58,22 @@ func (form *SettingsUpsert) Submit(interceptors ...InterceptorFunc[*settings.Set
return runInterceptors(form.Settings, func(s *settings.Settings) error {
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())
if err := form.dao.SaveSettings(form.Settings, encryptionKey); err != nil {
// try to revert app settings
form.app.Settings().Merge(oldSettings)
return err
}
@ -73,7 +87,6 @@ func (form *SettingsUpsert) Submit(interceptors ...InterceptorFunc[*settings.Set
form.app.LogsDao().Vacuum()
}
// merge the application settings with the form ones
return form.app.Settings().Merge(form.Settings)
return nil
}, interceptors...)
}