-
Notifications
You must be signed in to change notification settings - Fork 230
/
tracking.go
74 lines (61 loc) · 1.46 KB
/
tracking.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
package client
import (
"encoding/json"
"fmt"
"time"
"github.com/contribsys/faktory/util"
)
type JobTrack struct {
Jid string `json:"jid"`
Percent int `json:"percent,omitempty"`
Description string `json:"desc,omitempty"`
State string `json:"state"`
UpdatedAt string `json:"updated_at"`
}
func (c *Client) TrackGet(jid string) (*JobTrack, error) {
err := c.writeLine(c.wtr, "TRACK GET", []byte(jid))
if err != nil {
return nil, err
}
data, err := c.readResponse(c.rdr)
if err != nil {
return nil, err
}
var trck JobTrack
err = util.JsonUnmarshal(data, &trck)
if err != nil {
return nil, err
}
return &trck, nil
}
type setJobTrack struct {
Jid string `json:"jid"`
Percent int `json:"percent,omitempty"`
Description string `json:"desc,omitempty"`
ReserveUntil string `json:"reserve_until,omitempty"`
}
func (c *Client) TrackSet(jid string, percent int, desc string, reserveUntil *time.Time) error {
if jid == "" {
return fmt.Errorf("Job Track missing JID")
}
tset := setJobTrack{
Jid: jid,
Description: desc,
Percent: percent,
}
if reserveUntil != nil && time.Now().Before(*reserveUntil) {
tset.ReserveUntil = util.Thens(*reserveUntil)
}
return c.trackSet(&tset)
}
func (c *Client) trackSet(tset *setJobTrack) error {
data, err := json.Marshal(tset)
if err != nil {
return err
}
err = c.writeLine(c.wtr, "TRACK SET", data)
if err != nil {
return err
}
return c.ok(c.rdr)
}