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

Add fs.Close method to save the database when the application exits #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func main() {
log.Fatalf("error initializing store: %v", err)
}
store = s
defer s.Close()

} else {
logger.Fatal("app.storage must be one of redis|memory|fs")
Expand Down
19 changes: 12 additions & 7 deletions store/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (m *File) load() error {
}

// save the data to the file system.
func (m *File) save() {
func (m *File) save() error {
m.mu.Lock()
defer m.mu.Unlock()
if m.dirty {
Expand All @@ -108,14 +108,19 @@ func (m *File) save() {
})
if err == nil {
m.dirty = false
go func() {
err := ioutil.WriteFile(m.cfg.Path, data, os.ModePerm)
if err != nil {
m.log.Printf("error writing file %q: %v", m.cfg.Path, err)
}
}()
err = ioutil.WriteFile(m.cfg.Path, data, os.ModePerm)
if err != nil {
m.log.Printf("error writing file %q: %v", m.cfg.Path, err)
}
}
return err
}
return nil
}

// Close the database and save it on the file system.
func (m *File) Close() error {
return m.save()
}

// AddRoom adds a room to the store.
Expand Down