-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy_worker.go
53 lines (45 loc) · 971 Bytes
/
dummy_worker.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
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/voidshard/igor/pkg/api"
"github.com/voidshard/igor/pkg/database"
"github.com/voidshard/igor/pkg/queue"
)
const (
taskSleep = "sleep"
taskError = "die"
)
func main() {
pgURL := os.Getenv("DATABASE_URL")
rdURL := os.Getenv("QUEUE_URL")
svc, err := api.New(
&database.Options{URL: pgURL},
&queue.Options{URL: rdURL},
api.OptionsClientDefault(),
)
if err != nil {
panic(err)
}
myErr := fmt.Errorf("boom")
svc.Register(taskSleep, func(work []*queue.Meta) error {
for _, w := range work {
w.SetRunning("hi")
d := time.Duration(rand.Intn(5)+1) * time.Second
fmt.Printf("Sleeping for %s (%d 'tasks')\n", d, len(work))
time.Sleep(d)
fmt.Printf("Done sleeping\n")
}
return nil
})
svc.Register(taskError, func(work []*queue.Meta) error {
for _, w := range work {
fmt.Printf("Erroring %s\n", w.Task.ID)
w.SetError(myErr)
}
return nil
})
svc.Run()
}