gearman-go/example/worker.go

48 lines
933 B
Go
Raw Normal View History

2011-05-18 20:24:59 +08:00
package main
import (
2011-12-28 17:59:44 +08:00
"fmt"
"gearman"
"log"
"strings"
2011-05-18 20:24:59 +08:00
)
2011-12-28 17:59:44 +08:00
func ToUpper(job *gearman.WorkerJob) ([]byte, error) {
data := []byte(strings.ToUpper(string(job.Data)))
return data, nil
2011-05-18 20:24:59 +08:00
}
func main() {
2011-12-28 17:59:44 +08:00
worker := gearman.NewWorker()
worker.AddServer("127.0.0.1:4730")
worker.AddFunction("ToUpper", ToUpper, 0)
worker.AddFunction("ToUpperTimeOut5", ToUpper, 5)
2011-05-18 20:24:59 +08:00
2011-12-28 17:59:44 +08:00
go func() {
log.Println("start worker")
for {
print("cmd: ")
var str string
fmt.Scan(&str)
switch str {
case "echo":
worker.Echo([]byte("Hello world!"))
var job *gearman.WorkerJob
for job = <-worker.JobQueue; job.DataType != gearman.ECHO_RES; job = <-worker.JobQueue {
log.Println(job)
}
log.Println(string(job.Data))
case "quit":
worker.Close()
return
case "result":
job := <-worker.JobQueue
log.Println(string(job.Data))
default:
log.Println("Unknown command")
}
}
}()
worker.Work()
2011-05-18 20:24:59 +08:00
}