-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
86 lines (75 loc) · 2.72 KB
/
base.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
85
86
package ingest
import (
"fmt"
"github.com/APTrust/preservation-services/models/common"
"github.com/APTrust/preservation-services/models/service"
)
type BaseConstructor func(*common.Context, int64, *service.IngestObject) Runnable
type Runnable interface {
Run() (int, []*service.ProcessingError)
IngestObjectGet() *service.IngestObject
IngestObjectSave() error
}
// Base is the base type for workers in the ingest namespace.
type Base struct {
Context *common.Context
IngestObject *service.IngestObject
WorkItemID int64
}
// IngestObjectGet returns this struct's IngestObject. This satisfies part
// of the Runnable interface.
func (b *Base) IngestObjectGet() *service.IngestObject {
return b.IngestObject
}
// IngestFileGet returns an IngestFile record from Redis.
func (b *Base) IngestFileGet(identifier string) (*service.IngestFile, error) {
ingestFile, err := b.Context.RedisClient.IngestFileGet(b.WorkItemID, identifier)
if err != nil {
b.Context.Logger.Errorf(
"Failed to retrieve IngestFile from redis: WorkItem %d, %s: %s",
b.WorkItemID, identifier, err.Error())
}
return ingestFile, err
}
// IngestFileSave saves an IngestFile to Redis.
func (b *Base) IngestFileSave(ingestFile *service.IngestFile) error {
err := b.Context.RedisClient.IngestFileSave(b.WorkItemID, ingestFile)
if err != nil {
b.Context.Logger.Errorf(
"Failed to save IngestFile to redis: WorkItem %d, %s: %s",
b.WorkItemID, ingestFile.Identifier(), err.Error())
}
return err
}
// IngestObjectSave saves an IngestObject record to Redis.
func (b *Base) IngestObjectSave() error {
err := b.Context.RedisClient.IngestObjectSave(b.WorkItemID, b.IngestObject)
if err != nil {
b.Context.Logger.Errorf(
"Failed to save IngestObject to redis: WorkItem %d, %s: %s",
b.WorkItemID, b.IngestObject.Identifier(), err.Error())
} else {
b.Context.Logger.Infof(
"Saved IngestObject to redis: WorkItem %d, %s",
b.WorkItemID, b.IngestObject.Identifier())
}
return err
}
// S3KeyFor returns the S3 key for an ingest file in the staging bucket.
// Note that the staging bucket uses UUID keys, not file identifiers.
func (b *Base) S3KeyFor(ingestFile *service.IngestFile) string {
return fmt.Sprintf("%d/%s", b.WorkItemID, ingestFile.UUID)
}
// Error returns a ProcessingError describing something that went wrong
// during the ingest process for this object. Identifier can be either
// an IntellectualObect identifier, a GenericFile identifier, or in rare
// cases a WorkItem ID. Since each has a different format, you can discern
// the identifier type by looking at it.
func (b *Base) Error(identifier string, err error, isFatal bool) *service.ProcessingError {
return service.NewProcessingError(
b.WorkItemID,
identifier,
err.Error(),
isFatal,
)
}