loadlogs.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package executor
  2. import (
  3. "os"
  4. "path/filepath"
  5. "sort"
  6. "strings"
  7. "time"
  8. log "github.com/sirupsen/logrus"
  9. "gopkg.in/yaml.v3"
  10. )
  11. // LoadLogsFromDisk loads persisted logs from YAML files on disk and restores them to the executor.
  12. // This should be called during startup if saveLogs is configured.
  13. func (e *Executor) LoadLogsFromDisk() {
  14. resultsDir := e.Cfg.SaveLogs.ResultsDirectory
  15. if resultsDir == "" {
  16. return
  17. }
  18. entries := e.readLogDirectory(resultsDir)
  19. if entries == nil {
  20. return
  21. }
  22. loadedLogs, skippedCount := e.parseLogFiles(resultsDir, entries, 0)
  23. sort.Slice(loadedLogs, func(i, j int) bool {
  24. return loadedLogs[i].DatetimeStarted.Before(loadedLogs[j].DatetimeStarted)
  25. })
  26. skippedCount = e.restoreLogsToExecutor(loadedLogs, skippedCount)
  27. log.WithFields(log.Fields{
  28. "loaded": len(loadedLogs),
  29. "skipped": skippedCount,
  30. }).Info("Finished loading persisted logs from disk")
  31. }
  32. func (e *Executor) readLogDirectory(resultsDir string) []os.DirEntry {
  33. if _, err := os.Stat(resultsDir); os.IsNotExist(err) {
  34. log.WithFields(log.Fields{
  35. "directory": resultsDir,
  36. }).Debug("Logs directory does not exist, skipping log loading")
  37. return nil
  38. }
  39. log.WithFields(log.Fields{
  40. "directory": resultsDir,
  41. }).Info("Loading persisted logs from disk")
  42. entries, err := os.ReadDir(resultsDir)
  43. if err != nil {
  44. log.WithFields(log.Fields{
  45. "directory": resultsDir,
  46. "error": err,
  47. }).Warnf("Failed to read logs directory")
  48. return nil
  49. }
  50. return entries
  51. }
  52. func (e *Executor) parseLogFiles(resultsDir string, entries []os.DirEntry, skippedCount int) ([]*InternalLogEntry, int) {
  53. loadedLogs := make([]*InternalLogEntry, 0)
  54. for _, entry := range entries {
  55. if !e.shouldProcessLogEntry(entry) {
  56. continue
  57. }
  58. logEntry, newSkippedCount := e.processLogFileEntry(resultsDir, entry.Name())
  59. skippedCount += newSkippedCount
  60. if logEntry != nil {
  61. loadedLogs = append(loadedLogs, logEntry)
  62. }
  63. }
  64. return loadedLogs, skippedCount
  65. }
  66. func (e *Executor) shouldProcessLogEntry(entry os.DirEntry) bool {
  67. return !entry.IsDir() && strings.HasSuffix(entry.Name(), ".yaml")
  68. }
  69. func (e *Executor) processLogFileEntry(resultsDir, filename string) (*InternalLogEntry, int) {
  70. logEntry, ok := e.loadLogFileFromPath(resultsDir, filename)
  71. if !ok {
  72. return nil, 1
  73. }
  74. if logEntry.ExecutionTrackingID == "" {
  75. log.WithFields(log.Fields{
  76. "file": filepath.Join(resultsDir, filename),
  77. }).Warnf("Log file missing execution tracking ID, skipping")
  78. return nil, 1
  79. }
  80. e.restoreBindingForLogEntry(logEntry, filepath.Join(resultsDir, filename))
  81. return logEntry, 0
  82. }
  83. func (e *Executor) loadLogFileFromPath(resultsDir, filename string) (*InternalLogEntry, bool) {
  84. filepath := filepath.Join(resultsDir, filename)
  85. data, err := os.ReadFile(filepath)
  86. if err != nil {
  87. log.WithFields(log.Fields{
  88. "file": filepath,
  89. "error": err,
  90. }).Warnf("Failed to read log file")
  91. return nil, false
  92. }
  93. var logEntry InternalLogEntry
  94. if err := yaml.Unmarshal(data, &logEntry); err != nil {
  95. log.WithFields(log.Fields{
  96. "file": filepath,
  97. "error": err,
  98. }).Warnf("Failed to unmarshal log file")
  99. return nil, false
  100. }
  101. return &logEntry, true
  102. }
  103. // Skipped when the entry already has a valid binding or has no ActionConfigTitle (e.g. action/entity removed from config).
  104. func (e *Executor) restoreBindingForLogEntry(logEntry *InternalLogEntry, filepath string) {
  105. if e.hasValidBinding(logEntry) || logEntry.ActionConfigTitle == "" {
  106. return
  107. }
  108. binding := e.findBindingByActionTitle(logEntry.ActionConfigTitle, logEntry.EntityPrefix)
  109. if binding != nil {
  110. logEntry.Binding = binding
  111. return
  112. }
  113. e.logBindingNotFound(logEntry, filepath)
  114. logEntry.Binding = nil
  115. }
  116. func (e *Executor) hasValidBinding(logEntry *InternalLogEntry) bool {
  117. return logEntry.Binding != nil && logEntry.Binding.Action != nil
  118. }
  119. func (e *Executor) logBindingNotFound(logEntry *InternalLogEntry, filepath string) {
  120. log.WithFields(log.Fields{
  121. "file": filepath,
  122. "actionTitle": logEntry.ActionConfigTitle,
  123. "entityPrefix": logEntry.EntityPrefix,
  124. "trackingId": logEntry.ExecutionTrackingID,
  125. }).Debug("Could not find binding for log entry, loading without binding")
  126. }
  127. func (e *Executor) restoreLogsToExecutor(loadedLogs []*InternalLogEntry, skippedCount int) int {
  128. e.logmutex.Lock()
  129. defer e.logmutex.Unlock()
  130. for _, logEntry := range loadedLogs {
  131. if _, exists := e.logs[logEntry.ExecutionTrackingID]; exists {
  132. log.WithFields(log.Fields{
  133. "trackingId": logEntry.ExecutionTrackingID,
  134. }).Debug("Log entry already exists, skipping")
  135. skippedCount++
  136. continue
  137. }
  138. logEntry.Index = e.takeNextLogIndexLocked()
  139. e.logs[logEntry.ExecutionTrackingID] = logEntry
  140. e.logsTrackingIdsByDate = append(e.logsTrackingIdsByDate, logEntry.ExecutionTrackingID)
  141. if logEntry.Binding != nil {
  142. e.addLogToBindingMap(logEntry)
  143. }
  144. }
  145. e.evictOldLogsLocked(time.Now())
  146. return skippedCount
  147. }
  148. func (e *Executor) addLogToBindingMap(logEntry *InternalLogEntry) {
  149. if _, containsKey := e.LogsByBindingId[logEntry.Binding.ID]; !containsKey {
  150. e.LogsByBindingId[logEntry.Binding.ID] = make([]*InternalLogEntry, 0)
  151. }
  152. e.LogsByBindingId[logEntry.Binding.ID] = append(e.LogsByBindingId[logEntry.Binding.ID], logEntry)
  153. }
  154. func (e *Executor) findBindingByActionTitle(actionConfigTitle string, entityPrefix string) *ActionBinding {
  155. e.MapActionBindingsLock.RLock()
  156. defer e.MapActionBindingsLock.RUnlock()
  157. for _, binding := range e.MapActionBindings {
  158. if binding.Action.Title == actionConfigTitle && e.matchesEntityPrefix(binding, entityPrefix) {
  159. return binding
  160. }
  161. }
  162. return nil
  163. }
  164. func (e *Executor) matchesEntityPrefix(binding *ActionBinding, entityPrefix string) bool {
  165. if entityPrefix == "" {
  166. return binding.Entity == nil
  167. }
  168. return binding.Entity != nil && binding.Entity.UniqueKey == entityPrefix
  169. }