-
Notifications
You must be signed in to change notification settings - Fork 21
/
job.go
54 lines (47 loc) · 1.35 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
package dalga
import (
"encoding/json"
"fmt"
"time"
)
// Job is the record stored in jobs table.
// Primary key for the table is JobKey.
type Job struct {
JobKey
// Interval is the duration between each POST to the endpoint.
// Interval is 0 for one-off jobs.
Interval time.Duration
// NextRun is the next run time of the job, stored in UTC.
NextRun time.Time
}
type JobKey struct {
// Path is where the job is going to be POSTed when it's time came.
Path string
// Body of POST request.
Body string
}
// String returns the job in human-readable form.
func (j *Job) String() string {
return fmt.Sprintf("Job{%q, %q, %s, %s}", j.Body, j.Path, j.Interval, j.NextRun.String()[:23])
}
// OneOff returns true for one-off jobs. One-off jobs are stored with 0 interval on jobs table.
func (j *Job) OneOff() bool {
return j.Interval == 0
}
// Remaining returns the remaining time to the job's next run time.
func (j *Job) Remaining() time.Duration {
return j.NextRun.Sub(time.Now().UTC())
}
func (j *Job) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Path string `json:"path"`
Body string `json:"body"`
Interval time.Duration `json:"interval"`
NextRun string `json:"next_run"`
}{
Path: j.Path,
Body: j.Body,
Interval: j.Interval / time.Second,
NextRun: j.NextRun.Format(time.RFC3339),
})
}