Newer
Older
import (
"fmt"
"github.com/pkg/errors"
"io"
"log"
"sync"
)
type Job struct {
// Function to call on Collect. This is the function
// that will (1) update the state of this job, (2)
// do the actual importing, (3) write to the Job-internal
// log and eventually (4) finish either with success
// or in failure.
collectfunc func(self *Job, c *Collector) error
// Waiting room for people to wait on completion of this
// job.
wr waitingroom
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
// Global lock for all following members.
mu sync.Mutex
// Unique identifier of this job.
id JobId
// The state the job is currently in.
state JobState
// When state is set to Failed, contains the reason
// for this failure.
failure error
// Recorded logs for this job.
logged concurrentbuffer
}
// Return the unique identifier of this job.
func (bj *Job) Id() JobId {
bj.mu.Lock()
defer bj.mu.Unlock()
return bj.id
}
// Return the current state of this job.
func (bj *Job) State() JobState {
bj.mu.Lock()
defer bj.mu.Unlock()
return bj.state
}
// When State is Failed, return the error that caused
// the failure. When State is not failed, this method
// always returns nil.
func (bj *Job) Error() error {
bj.mu.Lock()
defer bj.mu.Unlock()
return bj.failure
}
// This is a synchronous operation that might take a long
// time. Instead of invoking Collect directly, you should
// create a Collector and Submit this job to it.
func (bj *Job) Collect(c *Collector) error {
if bj.collectfunc == nil {
return errors.New("collectfunc not set on Job")
}
bj.collectfunc(bj, c)
return nil
}
// Write to the log associated with this Job. The log message
// will be output with the standard logger as well as recorded
// to the buffer accessible with Job.Logs().
func (bj *Job) Log(v ...interface{}) {
log.Output(2, fmt.Sprint(v...))
}
// Write a formatted string to the log associated with this
// Job. The log message will be output with the standard
// logger as well as recorded to the buffer accessible with
// Job.Logs().
func (bj *Job) Logf(format string, v ...interface{}) {
log.Output(2, fmt.Sprintf(format, v...))
}
// Return the log stream of this job.
func (bj *Job) Logs() io.Reader {
panic("not yet implemented")
// Wait for this job to complete. If this job was never submitted
// before a call to Wait, this call to Wait will never return.
func (bj *Job) Wait() {
bj.wr.Wait()
}