Skip to content

Commit d168241

Browse files
committed
fix deleted ref files
1 parent c25bb76 commit d168241

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

internal/file/file_manager_service.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (fms *FileManagerService) UpdateOverview(
178178
delta := files.ConvertToMapOfFiles(response.GetOverview().GetFiles())
179179

180180
if len(delta) != 0 {
181-
return fms.updateFiles(ctx, delta, instanceID, iteration)
181+
return fms.updateFiles(ctx, delta, request.GetOverview().GetFiles(), instanceID, iteration)
182182
}
183183

184184
return err
@@ -202,6 +202,7 @@ func (fms *FileManagerService) setupIdentifiers(ctx context.Context, iteration i
202202
func (fms *FileManagerService) updateFiles(
203203
ctx context.Context,
204204
delta map[string]*mpi.File,
205+
fileOverview []*mpi.File,
205206
instanceID string,
206207
iteration int,
207208
) error {
@@ -217,7 +218,7 @@ func (fms *FileManagerService) updateFiles(
217218
iteration++
218219
slog.Debug("Updating file overview", "attempt_number", iteration)
219220

220-
return fms.UpdateOverview(ctx, instanceID, diffFiles, iteration)
221+
return fms.UpdateOverview(ctx, instanceID, fileOverview, iteration)
221222
}
222223

223224
func (fms *FileManagerService) UpdateFile(
@@ -483,7 +484,7 @@ func (fms *FileManagerService) DetermineFileActions(
483484
fileDiff := make(map[string]*model.FileCache) // Files that have changed, key is file name
484485
fileContents := make(map[string][]byte) // contents of the file, key is file name
485486

486-
manifestFiles, filesMap, manifestFileErr := fms.manifestFile()
487+
_, filesMap, manifestFileErr := fms.manifestFile()
487488

488489
if manifestFileErr != nil {
489490
if errors.Is(manifestFileErr, os.ErrNotExist) {
@@ -524,14 +525,15 @@ func (fms *FileManagerService) DetermineFileActions(
524525
}
525526
// if file doesn't exist in the current files, file has been added
526527
// set file action
527-
if !ok {
528+
if _, statErr := os.Stat(modifiedFile.File.GetFileMeta().GetName()); errors.Is(statErr, os.ErrNotExist) {
529+
slog.Info("File is not present on disk", "file", modifiedFile.File.GetFileMeta().GetName())
528530
modifiedFile.Action = model.Add
529531
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
530532

531533
continue
532534
// if file currently exists and file hash is different, file has been updated
533535
// copy contents, set file action
534-
} else if modifiedFile.File.GetFileMeta().GetHash() != currentFile.GetFileMeta().GetHash() {
536+
} else if ok && modifiedFile.File.GetFileMeta().GetHash() != currentFile.GetFileMeta().GetHash() {
535537
fileContent, readErr := os.ReadFile(fileName)
536538
if readErr != nil {
537539
return nil, nil, fmt.Errorf("error reading file %s, error: %w", fileName, readErr)
@@ -540,24 +542,6 @@ func (fms *FileManagerService) DetermineFileActions(
540542
fileContents[fileName] = fileContent
541543
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
542544
}
543-
544-
// If the file is unreferenced we check if the file has been updated since the last time
545-
// Also check if the unreferenced file still exists
546-
if manifestFiles[modifiedFile.File.GetFileMeta().GetName()] != nil &&
547-
!manifestFiles[modifiedFile.File.GetFileMeta().GetName()].ManifestFileMeta.Referenced {
548-
if fileStats, err := os.Stat(modifiedFile.File.GetFileMeta().GetName()); errors.Is(err, os.ErrNotExist) {
549-
modifiedFile.Action = model.Add
550-
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
551-
} else if timestamppb.New(fileStats.ModTime()) != modifiedFile.File.GetFileMeta().GetModifiedTime() {
552-
fileContent, readErr := os.ReadFile(fileName)
553-
if readErr != nil {
554-
return nil, nil, fmt.Errorf("error reading file %s, error: %w", fileName, readErr)
555-
}
556-
modifiedFile.Action = model.Update
557-
fileContents[fileName] = fileContent
558-
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
559-
}
560-
}
561545
}
562546

563547
return fileDiff, fileContents, nil

internal/file/file_manager_service_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,21 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
490490
updateErr := os.WriteFile(updateTestFile.Name(), updatedFileContent, 0o600)
491491
require.NoError(t, updateErr)
492492

493-
addTestFileName := tempDir + "/nginx_add.conf"
493+
addTestFileName := tempDir + "nginx_add.conf"
494494

495495
unmanagedFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx_unmanaged.conf")
496496
defer helpers.RemoveFileWithErrorCheck(t, unmanagedFile.Name())
497497
unmanagedFileContent := []byte("test unmanaged file")
498498
unmanagedErr := os.WriteFile(unmanagedFile.Name(), unmanagedFileContent, 0o600)
499499
require.NoError(t, unmanagedErr)
500500

501+
addTestFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx_add.conf")
502+
defer helpers.RemoveFileWithErrorCheck(t, addTestFile.Name())
503+
t.Logf("Adding file: %s", addTestFile.Name())
504+
addFileContent := []byte("test add file")
505+
addErr := os.WriteFile(addTestFile.Name(), addFileContent, 0o600)
506+
require.NoError(t, addErr)
507+
501508
tests := []struct {
502509
expectedError error
503510
modifiedFiles map[string]*model.FileCache
@@ -572,9 +579,9 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
572579
{
573580
name: "Test 2: Files same as on disk",
574581
modifiedFiles: map[string]*model.FileCache{
575-
addTestFileName: {
582+
addTestFile.Name(): {
576583
File: &mpi.File{
577-
FileMeta: protos.FileMeta(addTestFileName, files.GenerateHash(fileContent)),
584+
FileMeta: protos.FileMeta(addTestFile.Name(), files.GenerateHash(fileContent)),
578585
},
579586
},
580587
updateTestFile.Name(): {
@@ -595,8 +602,8 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
595602
updateTestFile.Name(): {
596603
FileMeta: protos.FileMeta(updateTestFile.Name(), files.GenerateHash(fileContent)),
597604
},
598-
addTestFileName: {
599-
FileMeta: protos.FileMeta(addTestFileName, files.GenerateHash(fileContent)),
605+
addTestFile.Name(): {
606+
FileMeta: protos.FileMeta(addTestFile.Name(), files.GenerateHash(fileContent)),
600607
},
601608
},
602609
expectedCache: make(map[string]*model.FileCache),
@@ -612,10 +619,11 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
612619
defer manifestFile.Close()
613620
manifestDirPath = tempDir
614621
manifestFilePath = manifestFile.Name()
615-
t.Logf("path: %s", manifestFilePath)
622+
616623
fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
617624
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig())
618625
require.NoError(tt, err)
626+
619627
diff, contents, fileActionErr := fileManagerService.DetermineFileActions(test.currentFiles,
620628
test.modifiedFiles)
621629
require.NoError(tt, fileActionErr)

internal/watcher/watcher_plugin.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,9 @@ func (w *Watcher) handleConfigApplySuccess(ctx context.Context, msg *bus.Message
198198

199199
// If the config apply had no changes to any files, it is results in a ConfigApplySuccessfulTopic with an empty
200200
// configContext being sent, there is no need to reparse the config as no change has occurred.
201-
if successMessage.ConfigContext.InstanceID == "" {
202-
slog.DebugContext(ctx, "NginxConfigContext is empty, no need to reparse config")
203-
return
201+
if successMessage.ConfigContext.InstanceID != "" {
202+
w.instanceWatcherService.HandleNginxConfigContextUpdate(ctx, instanceID, successMessage.ConfigContext)
204203
}
205-
w.instanceWatcherService.HandleNginxConfigContextUpdate(ctx, instanceID, successMessage.ConfigContext)
206204

207205
w.watcherMutex.Lock()
208206
w.instancesWithConfigApplyInProgress = slices.DeleteFunc(
@@ -213,8 +211,8 @@ func (w *Watcher) handleConfigApplySuccess(ctx context.Context, msg *bus.Message
213211
)
214212

215213
w.fileWatcherService.SetEnabled(true)
216-
w.watcherMutex.Unlock()
217214
w.instanceWatcherService.SetEnabled(true)
215+
w.watcherMutex.Unlock()
218216
}
219217

220218
func (w *Watcher) handleHealthRequest(ctx context.Context) {

0 commit comments

Comments
 (0)