-
Notifications
You must be signed in to change notification settings - Fork 3
/
job.go
59 lines (49 loc) · 1.12 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
55
56
57
58
59
package main
import (
faktory "github.com/contribsys/faktory/client"
"github.com/robfig/cron"
)
type Job struct {
Schedule string `yaml:"schedule"`
Name string `yaml:"job"`
Args []interface{} `yaml:"args"`
Queue string `yaml:"queue"`
Retries int `yaml:"retries"`
Priority uint8 `yaml:"priority"`
}
func (j *Job) Start() {
// send the task to faktory
if len(j.Queue) == 0 {
j.Queue = "default"
}
log.Debugf("Running %v (queue: %v, args: %v)", j.Name, j.Queue, j.Args)
client, err := faktory.Open()
if err != nil {
log.Warnf("Failed to send %v to faktory: %v", j.Name, err)
return
}
defer client.Close()
job := faktory.NewJob(j.Name, j.Args...)
if j.Retries > 0 {
job.Retry = j.Retries
}
if j.Priority > 0 {
job.Priority = j.Priority
}
if len(job.Queue) > 0 {
job.Queue = j.Queue
}
err = client.Push(job)
if err != nil {
log.Warnf("Failed to push %v to faktory: %v", j.Name, err)
return
}
}
func (j *Job) GetFunc() cron.FuncJob {
return func() {
go j.Start()
}
}
func (j *Job) AddToScheduler() {
scheduler.AddFunc(j.Schedule, j.GetFunc())
}