X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/522d7fd117285e4e4ce3ea5ec9b5a1722521b681..f226928c3a074ac4856ba07e8fd7107aba5444a3:/subagent.go?ds=inline diff --git a/subagent.go b/subagent.go index 132ca47..07bfa45 100644 --- a/subagent.go +++ b/subagent.go @@ -124,6 +124,30 @@ func (s *Subagents) Get(ctx context.Context, name string) (*Subagent, error) { return subagent, nil } +// Snapshot flushes and locks the open logs while snapshot reads the subagent +// files. snapshot must not call Subagents or an open Subagent. +func (s *Subagents) Snapshot(ctx context.Context, snapshot func() error) error { + s.mutex.Lock() + defer s.mutex.Unlock() + + for _, subagent := range s.open { + subagent.mutex.Lock() + defer subagent.mutex.Unlock() + + err := subagent.Flush(ctx) + if err != nil { + return fmt.Errorf("flush subagent snapshot: %w", err) + } + } + + err := snapshot() + if err != nil { + return fmt.Errorf("copy subagent snapshot: %w", err) + } + + return nil +} + // Close makes the writes of each open log durable and closes it. func (s *Subagents) Close() error { s.mutex.Lock()