( ctx context.Context, req contract.MemorySessionsPruneRequest, )
| 1094 | } |
| 1095 | |
| 1096 | func (s *daemonMemorySessionLedgerService) Prune( |
| 1097 | ctx context.Context, |
| 1098 | req contract.MemorySessionsPruneRequest, |
| 1099 | ) (contract.MemorySessionsPruneResponse, error) { |
| 1100 | if req.OlderThanHours <= 0 { |
| 1101 | return contract.MemorySessionsPruneResponse{}, errors.New("older_than_hours must be positive") |
| 1102 | } |
| 1103 | paths, err := s.listPaths(ctx) |
| 1104 | if err != nil { |
| 1105 | return contract.MemorySessionsPruneResponse{}, err |
| 1106 | } |
| 1107 | cutoff := s.now().UTC().Add(-time.Duration(req.OlderThanHours) * time.Hour) |
| 1108 | response := contract.MemorySessionsPruneResponse{DryRun: req.DryRun} |
| 1109 | for _, path := range paths { |
| 1110 | if err := ctx.Err(); err != nil { |
| 1111 | return response, fmt.Errorf("daemon: prune memory session ledgers: %w", err) |
| 1112 | } |
| 1113 | ledger, err := readSessionLedger(path) |
| 1114 | if err != nil { |
| 1115 | return response, err |
| 1116 | } |
| 1117 | stopTime := ledger.Meta.CreatedAt |
| 1118 | if ledger.Meta.StoppedAt != nil { |
| 1119 | stopTime = *ledger.Meta.StoppedAt |
| 1120 | } |
| 1121 | if !stopTime.Before(cutoff) { |
| 1122 | continue |
| 1123 | } |
| 1124 | response.PrunedSessions++ |
| 1125 | response.PrunedEvents += len(ledger.Events) |
| 1126 | if req.DryRun { |
| 1127 | continue |
| 1128 | } |
| 1129 | if err := os.RemoveAll(filepath.Dir(path)); err != nil { |
| 1130 | return response, fmt.Errorf("daemon: prune ledger %q: %w", path, err) |
| 1131 | } |
| 1132 | } |
| 1133 | return response, nil |
| 1134 | } |
| 1135 | |
| 1136 | func (s *daemonMemorySessionLedgerService) Repair( |
| 1137 | context.Context, |
nothing calls this directly
no test coverage detected