fix: serialize concurrent property updates
Concurrent commands for the same app could each read a setting, change it, and write it back, so all of them exited successfully while silently dropping changes made by the others. Updates to app settings are now serialized so every change is kept, and readers never see a partially written value.
This commit is contained in:
42 files changed
+1698
-642
No files matched your search
@@ -67,7 +67,7 @@ var (
|
||||
)
|
||||
|
||||
// Entry is the source of truth for a storage volume. One file per entry
|
||||
// lives at $DOKKU_LIB_ROOT/config/storage/entries/<name>.json.
|
||||
// lives at $DOKKU_LIB_ROOT/data/storage-registry/entries/<name>.json.
|
||||
type Entry struct {
|
||||
Name string `json:"name"`
|
||||
Scheduler string `json:"scheduler"`
|
||||
@@ -162,6 +162,39 @@ func SaveEntry(entry *Entry) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateEntry loads a registered entry, passes it to fn and saves it, holding
|
||||
// the entries directory lock across the read and the write so concurrent
|
||||
// edits to one entry cannot drop each other's changes. Nothing is saved when
|
||||
// fn returns an error. The saved entry is returned.
|
||||
func UpdateEntry(name string, fn func(entry *Entry) error) (*Entry, error) {
|
||||
if err := EnsureEntriesDirectory(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var updated *Entry
|
||||
err := common.LockDirectory(EntriesDirectory(), func() error {
|
||||
entry, err := LoadEntry(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := fn(entry); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := SaveEntry(entry); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
updated = entry
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
// DeleteEntry removes the on-disk record for an entry.
|
||||
func DeleteEntry(name string) error {
|
||||
if err := os.Remove(entryPath(name)); err != nil && !os.IsNotExist(err) {
|
||||
|
||||
Reference in new issue
Block a user