-
Notifications
You must be signed in to change notification settings - Fork 4
/
process.go
137 lines (111 loc) · 2.91 KB
/
process.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package goworker
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/redis/go-redis/v9"
)
type process struct {
Hostname string
Pid int
ID string
Queues []string
}
func newProcess(id string, queues []string) (*process, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
return &process{
Hostname: hostname,
Pid: os.Getpid(),
ID: id,
Queues: queues,
}, nil
}
func (p *process) String() string {
return fmt.Sprintf("%s:%d-%s:%s", p.Hostname, p.Pid, p.ID, strings.Join(p.Queues, ","))
}
func (p *process) open(c *redis.Client) error {
err := c.SAdd(ctx, fmt.Sprintf("%sworkers", workerSettings.Namespace), p.String()).Err()
if err != nil {
return err
}
err = c.Set(ctx, fmt.Sprintf("%sstat:processed:%v", workerSettings.Namespace, p), "0", 0).Err()
if err != nil {
return err
}
err = c.Set(ctx, fmt.Sprintf("%sstat:failed:%v", workerSettings.Namespace, p), "0", 0).Err()
if err != nil {
return err
}
// We set the heartbeat as the first thing
err = c.HSet(ctx, fmt.Sprintf("%s%s", workerSettings.Namespace, heartbeatKey), p.String(), time.Now().Format(time.RFC3339)).Err()
if err != nil {
return err
}
return nil
}
func (p *process) close(c *redis.Client) error {
logger.Infof("%v shutdown", p)
err := c.SRem(ctx, fmt.Sprintf("%sworkers", workerSettings.Namespace), p.String()).Err()
if err != nil {
return err
}
err = c.Del(ctx, fmt.Sprintf("%sstat:processed:%s", workerSettings.Namespace, p)).Err()
if err != nil {
return err
}
err = c.Del(ctx, fmt.Sprintf("%sstat:failed:%s", workerSettings.Namespace, p)).Err()
if err != nil {
return err
}
err = c.HDel(ctx, fmt.Sprintf("%s%s", workerSettings.Namespace, heartbeatKey), p.String()).Err()
if err != nil {
return err
}
return nil
}
func (p *process) start(c *redis.Client) error {
err := c.Set(ctx, fmt.Sprintf("%sworker:%s:started", workerSettings.Namespace, p), time.Now().String(), 0).Err()
if err != nil {
return err
}
return nil
}
func (p *process) finish(c *redis.Client) error {
err := c.Del(ctx, fmt.Sprintf("%sworker:%s", workerSettings.Namespace, p)).Err()
if err != nil {
return err
}
err = c.Del(ctx, fmt.Sprintf("%sworker:%s:started", workerSettings.Namespace, p)).Err()
if err != nil {
return err
}
return nil
}
func (p *process) fail(c *redis.Client) error {
err := c.Incr(ctx, fmt.Sprintf("%sstat:failed", workerSettings.Namespace)).Err()
if err != nil {
return err
}
err = c.Incr(ctx, fmt.Sprintf("%sstat:failed:%s", workerSettings.Namespace, p)).Err()
if err != nil {
return err
}
return nil
}
func (p *process) queues(strict bool) []string {
// If the queues order is strict then just return them.
if strict {
return p.Queues
}
// If not then we want to shuffle the queues before returning them.
queues := make([]string, len(p.Queues))
for i, v := range rand.Perm(len(p.Queues)) {
queues[i] = p.Queues[v]
}
return queues
}