Skip to content

Commit 4cbb71a

Browse files
Update backup manager for consistency in variable naming and improved logging format
1 parent 4264560 commit 4cbb71a

4 files changed

Lines changed: 32 additions & 33 deletions

File tree

src/managers/backupmgr/backupinterface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func RegisterHTTPHandler(handler *HTTPHandler) {
5959
// GetBackupConfig returns a properly configured BackupConfig
6060
func GetBackupConfig() BackupConfig {
6161

62-
uuid := uuid.New()
63-
bmIdentifier := "[BM" + uuid.String()[:6] + "]:"
62+
id := uuid.New()
63+
bmIdentifier := "[BM" + id.String()[:6] + "]:"
6464
return BackupConfig{
6565
WorldName: config.GetSaveName(),
6666
BackupDir: config.GetConfiguredBackupDir(),

src/managers/backupmgr/manager.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ can coexist but may conflict if configured with overlapping directories.
2323

2424
// Initialize checks for BackupDir and waits until it exists, then ensures SafeBackupDir exists.
2525
// It returns a channel that signals when initialization is complete or an error occurs.
26-
func (m *BackupManager) Initialize(bmIdentifier string) <-chan error {
26+
func (m *BackupManager) Initialize(identifier string) <-chan error {
2727
m.mu.Lock()
2828
defer m.mu.Unlock()
2929

@@ -40,38 +40,38 @@ func (m *BackupManager) Initialize(bmIdentifier string) <-chan error {
4040
if stat, err := os.Stat(m.config.BackupDir); err == nil {
4141
if stat.IsDir() {
4242
// Directory exists, proceed
43-
logger.Backup.Debugf("%s found backup directory: %s", bmIdentifier, m.config.BackupDir)
43+
logger.Backup.Debugf("%s found backup directory: %s", identifier, m.config.BackupDir)
4444
break
4545
}
46-
result <- fmt.Errorf("%s backup path %s is not a directory", bmIdentifier, m.config.BackupDir)
46+
result <- fmt.Errorf("%s backup path %s is not a directory", identifier, m.config.BackupDir)
4747
return
4848
} else if !os.IsNotExist(err) {
4949
// An error other than "not exists" occurred
50-
result <- fmt.Errorf("%s error checking backup directory %s: %v", bmIdentifier, m.config.BackupDir, err)
50+
result <- fmt.Errorf("%s error checking backup directory %s: %v", identifier, m.config.BackupDir, err)
5151
return
5252
}
5353

54-
logger.Backup.Debugf("%s waiting for save folder "+m.config.BackupDir+" to be created by Stationeers...", bmIdentifier)
54+
logger.Backup.Debugf("%s waiting for save folder "+m.config.BackupDir+" to be created by Stationeers...", identifier)
5555
select {
5656
case <-m.ctx.Done():
57-
result <- fmt.Errorf("%s I have to go, the config was likely changed: %s", bmIdentifier, m.ctx.Err())
57+
result <- fmt.Errorf("%s I have to go, the config was likely changed: %s", identifier, m.ctx.Err())
5858
return
5959
case <-time.After(pollInterval):
6060
// Continue polling
6161
}
6262
}
6363

6464
if time.Now().After(deadline) {
65-
result <- fmt.Errorf("%s timeout waiting for backup directory %s to be created", bmIdentifier, m.config.BackupDir)
65+
result <- fmt.Errorf("%s timeout waiting for backup directory %s to be created", identifier, m.config.BackupDir)
6666
return
6767
}
6868

6969
// Ensure SafeBackupDir exists, create it if it doesn't
7070
if err := os.MkdirAll(m.config.SafeBackupDir, os.ModePerm); err != nil {
71-
result <- fmt.Errorf("%s error creating safe backup directory %s: %v", bmIdentifier, m.config.SafeBackupDir, err)
71+
result <- fmt.Errorf("%s error creating safe backup directory %s: %v", identifier, m.config.SafeBackupDir, err)
7272
return
7373
}
74-
logger.Backup.Debugf("%s created safebackups at %s", bmIdentifier, m.config.SafeBackupDir)
74+
logger.Backup.Debugf("%s created safebackups at %s", identifier, m.config.SafeBackupDir)
7575

7676
result <- nil
7777
}()
@@ -80,24 +80,23 @@ func (m *BackupManager) Initialize(bmIdentifier string) <-chan error {
8080
}
8181

8282
// Start begins the backup monitoring and cleanup routines
83-
func (m *BackupManager) Start(Identifier string) error {
83+
func (m *BackupManager) Start(identifier string) error {
8484
// Wait for initialization to complete
85-
bmIdentifier := Identifier
8685

87-
logger.Backup.Debugf("%s is waiting for save folder initialization...", bmIdentifier)
88-
initResult := <-m.Initialize(bmIdentifier)
86+
logger.Backup.Debugf("%s is waiting for save folder initialization...", identifier)
87+
initResult := <-m.Initialize(identifier)
8988
if initResult != nil {
90-
return fmt.Errorf("%s failed to initialize backup manager : %w", bmIdentifier, initResult)
89+
return fmt.Errorf("%s failed to initialize backup manager : %w", identifier, initResult)
9190
}
92-
logger.Backup.Infof("%s Backup manager instance started", bmIdentifier)
91+
logger.Backup.Infof("%s Backup manager instance started", identifier)
9392

9493
// Start file watcher
95-
watcher, err := newFsWatcher(m.config.BackupDir, bmIdentifier)
94+
watcher, err := newFsWatcher(m.config.BackupDir, identifier)
9695
if err != nil {
9796
return fmt.Errorf("failed to create autosave watcher: %w", err)
9897
}
9998
m.watcher = watcher
100-
go m.watchBackups(Identifier)
99+
go m.watchBackups(identifier)
101100

102101
if config.GetIsCleanupEnabled() {
103102
go m.startCleanupRoutine()
@@ -107,31 +106,31 @@ func (m *BackupManager) Start(Identifier string) error {
107106
}
108107

109108
// watchBackups monitors the backup directory for new files
110-
func (m *BackupManager) watchBackups(Identifier string) {
109+
func (m *BackupManager) watchBackups(identifier string) {
111110
m.wg.Add(1)
112111
defer m.wg.Done()
113112

114-
logger.Backup.Debugf("%s Starting backup file watcher...", Identifier)
115-
defer logger.Backup.Debugf("%s Backup file watcher stopped", Identifier)
113+
logger.Backup.Debugf("%s Starting backup file watcher...", identifier)
114+
defer logger.Backup.Debugf("%s Backup file watcher stopped", identifier)
116115

117116
for {
118117
select {
119118
case <-m.ctx.Done():
120-
logger.Backup.Debugf("%s WatchBackups stopped due to context cancellation", Identifier)
119+
logger.Backup.Debugf("%s WatchBackups stopped due to context cancellation", identifier)
121120
return
122121
case event, ok := <-m.watcher.events:
123122
if !ok {
124123
return
125124
}
126125
if event.Op&fsnotify.Create == fsnotify.Create {
127-
logger.Backup.Infof("%s New backup file detected: %s", Identifier, event.Name)
126+
logger.Backup.Infof("%s New backup file detected: %s", identifier, event.Name)
128127
m.handleNewBackup(event.Name)
129128
}
130129
case err, ok := <-m.watcher.errors:
131130
if !ok {
132131
return
133132
}
134-
logger.Backup.Errorf("%s Backup watcher error: %s", Identifier, err.Error())
133+
logger.Backup.Errorf("%s Backup watcher error: %s", identifier, err.Error())
135134
}
136135
}
137136
}

src/managers/backupmgr/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func (m *BackupManager) RestoreBackup(index int) error {
1818
m.mu.Lock()
1919
defer m.mu.Unlock()
20-
logger.Backup.Infof("Restoring backup with index %s", fmt.Sprintf("%d", index))
20+
logger.Backup.Infof("Restoring backup with index %d", index)
2121

2222
groups, err := m.getBackupGroups()
2323
if err != nil {

src/managers/backupmgr/watcher.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ type fsWatcher struct {
1919
}
2020

2121
// newFsWatcher creates a new file system watcher
22-
func newFsWatcher(path string, Identifier string) (*fsWatcher, error) {
22+
func newFsWatcher(path string, identifier string) (*fsWatcher, error) {
2323
// Normalize path
2424
normalizedPath := filepath.Clean(path)
25-
logger.Backup.Debugf("%s Creating watcher for path: %s ", Identifier, normalizedPath)
25+
logger.Backup.Debugf("%s Creating watcher for path: %s", identifier, normalizedPath)
2626

2727
watcher, err := fsnotify.NewWatcher()
2828
if err != nil {
29-
return nil, fmt.Errorf("%s failed to create watcher: %w", Identifier, err)
29+
return nil, fmt.Errorf("%s failed to create watcher: %w", identifier, err)
3030
}
31-
logger.Backup.Debugf("%s Watcher created successfully", Identifier)
31+
logger.Backup.Debugf("%s Watcher created successfully", identifier)
3232

3333
// Watch the root save path and all subdirectories
3434
err = filepath.WalkDir(normalizedPath, func(subPath string, d os.DirEntry, err error) error {
@@ -37,16 +37,16 @@ func newFsWatcher(path string, Identifier string) (*fsWatcher, error) {
3737
}
3838
if d.IsDir() {
3939
if err := watcher.Add(subPath); err != nil {
40-
logger.Backup.Errorf("%s Failed to add subdir %s to watcher: %s ", Identifier, subPath, err.Error())
40+
logger.Backup.Errorf("%s Failed to add subdir %s to watcher: %s", identifier, subPath, err.Error())
4141
} else {
42-
logger.Backup.Debugf("%s Added subdir %s to watcher", Identifier, subPath)
42+
logger.Backup.Debugf("%s Added subdir %s to watcher", identifier, subPath)
4343
}
4444
}
4545
return nil
4646
})
4747
if err != nil {
4848
watcher.Close()
49-
return nil, fmt.Errorf("%s failed to add subdirectories to watcher: %w", Identifier, err)
49+
return nil, fmt.Errorf("%s failed to add subdirectories to watcher: %w", identifier, err)
5050
}
5151

5252
w := &fsWatcher{

0 commit comments

Comments
 (0)