Repository navigation
Expand file tree
/
Copy pathjobs.go
More file actions
126 lines (106 loc) · 3.15 KB
/
Copy pathjobs.go
File metadata and controls
126 lines (106 loc) · 3.15 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package lm15
import (
"context"
"time"
)
// Job handles: the ticket ergonomics over the pure batch and video ops.
// Reading a property never contacts the provider; Wait is the only thing
// that waits, and it never cancels the provider-side job.
// BatchJob is a live handle on one provider-side batch job.
type BatchJob struct {
lm LM
info BatchJobInfo
}
// Info is the snapshot from the last provider contact.
func (j *BatchJob) Info() BatchJobInfo { return j.info }
// ID is the ticket.
func (j *BatchJob) ID() string { return j.info.ID }
// Status is the last known status.
func (j *BatchJob) Status() string { return j.info.Status }
// Done reports a terminal status.
func (j *BatchJob) Done() bool { return j.info.Done() }
// Refresh re-reads the job.
func (j *BatchJob) Refresh(ctx context.Context) (*BatchJob, error) {
info, err := j.lm.BatchStatus(ctx, j.info.ID)
if err != nil {
return j, err
}
j.info = info
return j, nil
}
// Wait polls until the job is terminal; the context bounds the wait
// (context.DeadlineExceeded past its deadline).
func (j *BatchJob) Wait(ctx context.Context, pollEvery time.Duration) (*BatchJob, error) {
if pollEvery <= 0 {
pollEvery = 30 * time.Second
}
for !j.info.Done() {
select {
case <-ctx.Done():
return j, ctx.Err()
case <-time.After(pollEvery):
}
if _, err := j.Refresh(ctx); err != nil {
return j, err
}
}
return j, nil
}
// Results returns the entries in submission order.
func (j *BatchJob) Results(ctx context.Context) ([]BatchEntry, error) {
return j.lm.BatchResults(ctx, j.info.ID)
}
// Cancel requests cancellation.
func (j *BatchJob) Cancel(ctx context.Context) (*BatchJob, error) {
info, err := j.lm.BatchCancel(ctx, j.info.ID)
if err != nil {
return j, err
}
j.info = info
return j, nil
}
// VideoJob is a live handle on one provider-side video job.
type VideoJob struct {
lm LM
info VideoJobInfo
}
// Info is the snapshot from the last provider contact.
func (j *VideoJob) Info() VideoJobInfo { return j.info }
// ID is the ticket.
func (j *VideoJob) ID() string { return j.info.ID }
// Status is the last known status.
func (j *VideoJob) Status() string { return j.info.Status }
// Progress is the last known percentage, if reported.
func (j *VideoJob) Progress() *int { return j.info.Progress }
// Done reports a terminal status.
func (j *VideoJob) Done() bool { return j.info.Done() }
// Refresh re-reads the job.
func (j *VideoJob) Refresh(ctx context.Context) (*VideoJob, error) {
info, err := j.lm.VideoStatus(ctx, j.info.ID)
if err != nil {
return j, err
}
j.info = info
return j, nil
}
// Wait polls until a terminal status (including failure); ctx bounds it.
func (j *VideoJob) Wait(ctx context.Context, pollEvery time.Duration) (*VideoJob, error) {
if pollEvery <= 0 {
pollEvery = 5 * time.Second
}
for !j.info.Done() {
select {
case <-ctx.Done():
return j, ctx.Err()
case <-time.After(pollEvery):
}
if _, err := j.Refresh(ctx); err != nil {
return j, err
}
}
return j, nil
}
// Result fetches the finished video; it never implicitly waits.
func (j *VideoJob) Result(ctx context.Context) (VideoPart, error) {
return j.lm.VideoResult(ctx, j.info.ID)
}