forked from flyteorg/flyteplugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_processor.go
84 lines (67 loc) · 2.17 KB
/
reader_processor.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package catalog
import (
"context"
"fmt"
"reflect"
"github.com/lyft/flyteplugins/go/tasks/errors"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/lyft/flytestdlib/logger"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/workqueue"
)
type ReaderWorkItem struct {
// ReaderWorkItem outputs:
cached bool
// ReaderWorkItem Inputs:
outputsWriter io.OutputWriter
// Inputs to query data catalog
key Key
}
func (item ReaderWorkItem) IsCached() bool {
return item.cached
}
func NewReaderWorkItem(key Key, outputsWriter io.OutputWriter) *ReaderWorkItem {
return &ReaderWorkItem{
key: key,
outputsWriter: outputsWriter,
}
}
type ReaderProcessor struct {
catalogClient Client
}
func (p ReaderProcessor) Process(ctx context.Context, workItem workqueue.WorkItem) (workqueue.WorkStatus, error) {
wi, casted := workItem.(*ReaderWorkItem)
if !casted {
return workqueue.WorkStatusNotDone, fmt.Errorf("wrong work item type. Received: %v", reflect.TypeOf(workItem))
}
op, err := p.catalogClient.Get(ctx, wi.key)
if err != nil {
if IsNotFound(err) {
logger.Infof(ctx, "Artifact not found in Catalog. Key: %v", wi.key)
wi.cached = false
return workqueue.WorkStatusSucceeded, nil
}
err = errors.Wrapf("CausedBy", err, "Failed to call catalog for Key: %v.", wi.key)
logger.Warnf(ctx, "Cache call failed: %v", err)
return workqueue.WorkStatusFailed, err
}
if op == nil {
wi.cached = false
return workqueue.WorkStatusSucceeded, nil
}
// TODO: Check task interface, if it has outputs but literalmap is empty (or not matching output), error.
logger.Debugf(ctx, "Persisting output to %v", wi.outputsWriter.GetOutputPath())
err = wi.outputsWriter.Put(ctx, op)
if err != nil {
err = errors.Wrapf("CausedBy", err, "Failed to persist cached output for Key: %v.", wi.key)
logger.Warnf(ctx, "Cache write to output writer failed: %v", err)
return workqueue.WorkStatusFailed, err
}
wi.cached = true
logger.Debugf(ctx, "Successfully wrote to catalog. Key [%v]", wi.key)
return workqueue.WorkStatusSucceeded, nil
}
func NewReaderProcessor(catalogClient Client) ReaderProcessor {
return ReaderProcessor{
catalogClient: catalogClient,
}
}