indexing_status.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package nginx_log
  2. import (
  3. "sync"
  4. "github.com/0xJacky/Nginx-UI/internal/event"
  5. "github.com/uozi-tech/cosy/logger"
  6. )
  7. // IndexingStatusManager manages the global indexing status
  8. type IndexingStatusManager struct {
  9. mu sync.RWMutex
  10. indexing bool
  11. }
  12. var (
  13. statusManager *IndexingStatusManager
  14. statusOnce sync.Once
  15. )
  16. // GetIndexingStatusManager returns the singleton instance of IndexingStatusManager
  17. func GetIndexingStatusManager() *IndexingStatusManager {
  18. statusOnce.Do(func() {
  19. statusManager = &IndexingStatusManager{
  20. indexing: false,
  21. }
  22. })
  23. return statusManager
  24. }
  25. // UpdateIndexingStatus updates the global indexing status based on current file states
  26. func (m *IndexingStatusManager) UpdateIndexingStatus() {
  27. m.mu.Lock()
  28. defer m.mu.Unlock()
  29. // Check if any files are currently being indexed
  30. indexingFiles := GetIndexingFiles()
  31. newIndexingStatus := len(indexingFiles) > 0
  32. // Only publish event if status changed
  33. if m.indexing != newIndexingStatus {
  34. m.indexing = newIndexingStatus
  35. logger.Infof("Global indexing status changed to: %t (active files: %d)",
  36. newIndexingStatus, len(indexingFiles))
  37. // Update global processing status
  38. processingManager := event.GetProcessingStatusManager()
  39. processingManager.UpdateNginxLogIndexing(newIndexingStatus)
  40. }
  41. }
  42. // IsIndexing returns the current global indexing status
  43. func (m *IndexingStatusManager) IsIndexing() bool {
  44. m.mu.RLock()
  45. defer m.mu.RUnlock()
  46. return m.indexing
  47. }
  48. // NotifyFileIndexingStarted should be called when a file starts indexing
  49. func (m *IndexingStatusManager) NotifyFileIndexingStarted(filePath string) {
  50. logger.Infof("File indexing started: %s", filePath)
  51. SetIndexingStatus(filePath, true)
  52. m.UpdateIndexingStatus()
  53. }
  54. // NotifyFileIndexingCompleted should be called when a file finishes indexing
  55. func (m *IndexingStatusManager) NotifyFileIndexingCompleted(filePath string) {
  56. logger.Infof("File indexing completed: %s", filePath)
  57. SetIndexingStatus(filePath, false)
  58. m.UpdateIndexingStatus()
  59. }