在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:benmanns/goworker开源软件地址:https://github.com/benmanns/goworker开源编程语言:Go 100.0%开源软件介绍:goworkergoworker 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. InstallationTo install goworker, use go get github.com/benmanns/goworker to install the package, and then from your worker import "github.com/benmanns/goworker" Getting StartedTo 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)
}
} Here is a simple worker with settings: 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() {
settings := goworker.WorkerSettings{
URI: "redis://localhost:6379/",
Connections: 100,
Queues: []string{"myqueue", "delimited", "queues"},
UseNumber: true,
ExitOnComplete: false,
Concurrency: 2,
Namespace: "resque:",
Interval: 5.0,
}
goworker.SetSettings(settings)
goworker.Register("MyClass", myFunc)
}
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 -r 100 RPUSH resque:queue:myqueue '{"class":"MyClass","args":["hi","there"]}' will insert 100 jobs for the class MyClass
@queue = :myqueue
end
100.times do
Resque.enqueue MyClass, ['hi', 'there']
end or goworker.Enqueue(&goworker.Job{
Queue: "myqueue",
Payload: goworker.Payload{
Class: "MyClass",
Args: []interface{}{"hi", "there"},
},
}) FlagsThere are several flags which control the operation of the goworker client.
You can also configure your own flags for use within your workers. Be sure to set them before calling Signal Handling in goworkerTo stop goworker, send a Failure ModesLike Resque, goworker makes no guarantees about the safety of jobs in the event of process shutdown. Workers must be both idempotent and tolerant to loss of the job in the event of failure. If the process is killed with a If you are running goworker on a system like Heroku, which sends a
as a JSON object with keys Contributing
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论