forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_logs.go
More file actions
52 lines (45 loc) · 1.09 KB
/
api_logs.go
File metadata and controls
52 lines (45 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package api
import (
"context"
"fmt"
"github.com/ipfs/go-cid"
"github.com/textileio/powergate/ffs"
)
// WatchLogs pushes human-friendly messages about Cid executions. The method is blocking
// and will continue to send messages until the context is canceled.
func (i *API) WatchLogs(ctx context.Context, ch chan<- ffs.LogEntry, c cid.Cid, opts ...GetLogsOption) error {
_, err := i.is.getStorageConfig(c)
if err == ErrNotFound {
return ErrNotFound
}
if err != nil {
return fmt.Errorf("validating cid: %s", err)
}
config := &GetLogsConfig{}
for _, o := range opts {
o(config)
}
if config.history {
lgs, err := i.sched.GetLogsByCid(ctx, c)
if err != nil {
return fmt.Errorf("getting history logs of %s: %s", c, err)
}
for _, l := range lgs {
ch <- l
}
}
ichan := make(chan ffs.LogEntry, cap(ch))
go func() {
err = i.sched.WatchLogs(ctx, ichan)
close(ichan)
}()
for le := range ichan {
if c == le.Cid && (config.jid == ffs.EmptyJobID || config.jid == le.Jid) {
ch <- le
}
}
if err != nil {
return fmt.Errorf("listening to cid logs: %s", err)
}
return nil
}