forked from benmanns/goworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
118 lines (118 loc) · 2.78 KB
/
doc.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
// Package goworker is a Resque-compatible, Go-based
// background worker. It allows you to push jobs into a
// queue using an expressive language like Ruby while
// harnessing the efficiency and concurrency of Go to
// minimize job latency and cost.
//
// goworker workers can run alongside Ruby Resque clients
// so that you can keep all but your most
// resource-intensive jobs in Ruby.
//
// To create a worker, write a function matching the
// signature
//
// func(string, ...interface{}) error
//
// and register it using
//
// goworker.Register("MyClass", myFunc)
//
// Here is a simple worker that prints its arguments:
//
// package main
//
// import (
// "fmt"
// "github.com/benmanns/goworker"
// )
//
// func myFunc(queue string, args ...interface{}) error {
// fmt.Printf("From %s, %v\n", queue, args)
// return nil
// }
//
// func init() {
// goworker.Register("MyClass", myFunc)
// }
//
// func main() {
// if err := goworker.Work(); err != nil {
// fmt.Println("Error:", err)
// }
// }
//
// To create workers that share a database pool or other
// resources, use a closure to share variables.
//
// package main
//
// import (
// "fmt"
// "github.com/benmanns/goworker"
// )
//
// func newMyFunc(uri string) (func(queue string, args ...interface{}) error) {
// foo := NewFoo(uri)
// return func(queue string, args ...interface{}) error {
// foo.Bar(args)
// return nil
// }
// }
//
// func init() {
// goworker.Register("MyClass", newMyFunc("http://www.example.com/"))
// }
//
// func main() {
// if err := goworker.Work(); err != nil {
// fmt.Println("Error:", err)
// }
// }
//
// goworker worker functions receive the queue they are
// serving and a slice of interfaces. To use them as
// parameters to other functions, use Go type assertions
// to convert them into usable types.
//
// // Expecting (int, string, float64)
// func myFunc(queue, args ...interface{}) error {
// idNum, ok := args[0].(json.Number)
// if !ok {
// return errorInvalidParam
// }
// id, err := idNum.Int64()
// if err != nil {
// return errorInvalidParam
// }
// name, ok := args[1].(string)
// if !ok {
// return errorInvalidParam
// }
// weightNum, ok := args[2].(json.Number)
// if !ok {
// return errorInvalidParam
// }
// weight, err := weightNum.Float64()
// if err != nil {
// return errorInvalidParam
// }
// doSomething(id, name, weight)
// return nil
// }
//
// For testing, it is helpful to use the redis-cli program
// to insert jobs onto the Redis queue:
//
// redis-cli -r 100 RPUSH resque:queue:myqueue '{"class":"MyClass","args":["hi","there"]}'
//
// will insert 100 jobs for the MyClass worker onto the
// myqueue queue. It is equivalent to:
//
// class MyClass
// @queue = :myqueue
// end
//
// 100.times do
// Resque.enqueue MyClass, ['hi', 'there']
// end
package goworker