gearman-go/example/worker.go

49 lines
1.2 KiB
Go
Raw Normal View History

2011-05-18 20:24:59 +08:00
package main
import (
"gearman"
"fmt"
"log"
"os"
"strings"
)
func ToUpper(job *gearman.WorkerJob) ([]byte, os.Error) {
2011-05-18 20:24:59 +08:00
data := []byte(strings.ToUpper(string(job.Data)))
return data, nil
}
func main() {
worker := gearman.NewWorker()
worker.AddServer("127.0.0.1:4730")
worker.AddFunction("ToUpper", ToUpper, 0)
worker.AddFunction("ToUpperTimeOut5", ToUpper, 5)
go func() {
log.Println("start worker")
for {
print("cmd: ")
var str string
fmt.Scan(&str)
switch str {
2011-05-24 13:20:22 +08:00
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")
2011-05-18 20:24:59 +08:00
}
}
}()
worker.Work()
}