15 type JobRequest struct {
16 Score string `json:"score"`
17 Scope JobScope `json:"scope"`
18 Players []string `json:"players"`
20 State JobState `json:"state"`
21 Params map[string]string `json:"params"`
22 Tasks []*TaskRequest `json:"tasks"`
23 // you need to use the registry to access these - only public for
25 Results map[string]*TaskResponse `json:"results"`
28 // Timeout for autoexpiry. Only valid if State if
29 // job.State.Finished() is true.
33 func NewJobRequest() (req *JobRequest) {
35 req.Results = make(map[string]*TaskResponse)
39 func JobRequestFromReader(src io.Reader) (req *JobRequest, err os.Error) {
41 jdec := json.NewDecoder(src)
43 err = jdec.Decode(req)
45 if req.Results == nil {
46 req.Results = make(map[string]*TaskResponse)
53 func (req *JobRequest) normalise() {
54 if (len(req.Players) > 1) {
55 /* sort targets so search works */
56 sort.Strings(req.Players)
58 if (req.Scope == SCOPE_ONEOF) {
59 req.Scope = SCOPE_ALLOF
64 func (req *JobRequest) MakeTasks() (tasks []*TaskRequest) {
73 numtasks = len(req.Players)
75 tasks = make([]*TaskRequest, numtasks)
77 for c := 0; c < numtasks; c++ {
80 if (req.Scope == SCOPE_ALLOF) {
81 t.Player = req.Players[c]
88 func (req *JobRequest) Valid() bool {
89 if (len(req.Players) <= 0) {
95 func (req *JobRequest) FilenameForSpool() string {
96 if (req.State == JOB_PENDING) {
97 return path.Join(GetSpoolDirectory(), "active", FilenameForJobId(req.Id))
99 return path.Join(GetSpoolDirectory(), "finished", FilenameForJobId(req.Id))
102 // dump the bytestream in buf into the serialisation file for req.
103 func (req *JobRequest) doSerialisation(buf []byte) {
104 // first up, clean up old state.
105 UnlinkNodesForJobId(req.Id)
106 outpath := req.FilenameForSpool()
107 fh, err := os.OpenFile(outpath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
109 o.Warn("Could not create persistence file %s: %s", outpath, err)
116 func (req *JobRequest) UpdateInSpool() {
117 buf, err := json.MarshalIndent(req, "", " ")
118 o.MightFail(err, "Failed to marshal job %d", req.Id)
119 //FIXME: should try to do this out of the registry's thread.
120 req.doSerialisation(buf)
123 // deserialise the job record from the finished spool
124 func LoadFromFinished(jobid uint64) (req *JobRequest, err os.Error) {
125 fpath := path.Join(GetSpoolDirectory(), "finished", FilenameForJobId(jobid))
126 fh, err := os.Open(fpath)
132 req, err = JobRequestFromReader(fh)