forked from gocelery/gocelery
/
main.go
65 lines (54 loc) · 1.51 KB
/
main.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
// Copyright (c) 2019 Sick Yoon
// This file is part of gocelery which is released under MIT license.
// See file LICENSE for full license details.
package main
import (
"log"
"math/rand"
"reflect"
"time"
"github.com/gocelery/gocelery"
"github.com/gomodule/redigo/redis"
)
// Run Celery Worker First!
// celery -A worker worker --loglevel=debug --without-heartbeat --without-mingle
func main() {
// create redis connection pool
redisPool := &redis.Pool{
MaxIdle: 3, // maximum number of idle connections in the pool
MaxActive: 0, // maximum number of connections allocated by the pool at a given time
IdleTimeout: 240 * time.Second, // close connections after remaining idle for this duration
Dial: func() (redis.Conn, error) {
c, err := redis.DialURL("redis://")
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
// initialize celery client
cli, _ := gocelery.NewCeleryClient(
gocelery.NewRedisBroker(redisPool),
&gocelery.RedisCeleryBackend{Pool: redisPool},
1,
)
// prepare arguments
taskName := "worker.add"
argA := rand.Intn(10)
argB := rand.Intn(10)
// run task
asyncResult, err := cli.Delay(taskName, argA, argB)
if err != nil {
panic(err)
}
// get results from backend with timeout
res, err := asyncResult.Get(10 * time.Second)
if err != nil {
panic(err)
}
log.Printf("result: %+v of type %+v", res, reflect.TypeOf(res))
}