-
Notifications
You must be signed in to change notification settings - Fork 5
/
job.go
104 lines (88 loc) · 2.71 KB
/
job.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package database
import (
"time"
"cloud.google.com/go/datastore"
)
// ProviderJob holds data coming from a Provider
type ProviderJob struct {
ID string
Status string
Details string
Params map[string]string
Cancellable bool
}
// Job representation of a captions job
type Job struct {
ID string `json:"id"`
ParentID string `json:"parent_id"`
MediaURL string `json:"media_url"`
Status string `json:"status"`
Provider string `json:"provider"`
ProviderParams ProviderParams `json:"provider_params"`
CreatedAt time.Time `json:"created_at"`
Outputs []JobOutput `json:"outputs"`
Done bool `json:"done"`
Language string `json:"language"`
Details string `json:"details,omitempty"`
CaptionFile UploadedFile `json:"caption_file,omitempty"`
JobType string `json:"job_type"`
}
// UploadedFile contains the uploaded file and its name
type UploadedFile struct {
File []byte `json:"file" datastore:",noindex"`
Name string `json:"name"`
}
// JobOutput output associated with a Job
type JobOutput struct {
URL string `json:"url"`
Type string `json:"type"`
Filename string `json:"filename"`
}
// JobSummary minimal information about a Job
type JobSummary struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
}
// ProviderParams is a set of parameters for providers
type ProviderParams map[string]string
// ByCreatedAt implements sort.Interface for []Job by CreatedAt field.
type ByCreatedAt []Job
func (b ByCreatedAt) Len() int { return len(b) }
func (b ByCreatedAt) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b ByCreatedAt) Less(i, j int) bool { return b[i].CreatedAt.Before(b[j].CreatedAt) }
// UpdateStatus update Job status and mark as done if needed
func (j *Job) UpdateStatus(status, details string) bool {
if j.Status == status {
return false
}
if status == "error" && !j.Done {
j.Done = true
}
j.Status = status
j.Details = details
return true
}
func (j *Job) GetProviderID() string {
return j.ProviderParams["ProviderID"]
}
// Load makes ProviderParams implement datastore.PropertyLoadSaver interface
func (p *ProviderParams) Load(ps []datastore.Property) error {
if *p == nil {
*p = make(ProviderParams)
}
for _, v := range ps {
(*p)[v.Name] = v.Value.(string)
}
return nil
}
// Save makes ProviderParams implement datastore.PropertyLoadSaver interface
func (p *ProviderParams) Save() ([]datastore.Property, error) {
result := make([]datastore.Property, 0, len(*p))
for k, v := range *p {
result = append(result, datastore.Property{
Name: k,
Value: v,
})
}
return result, nil
}