2011-05-18 20:24:59 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-03-26 13:32:59 +08:00
|
|
|
"bitbucket.org/mikespook/gearman-go/gearman"
|
|
|
|
"bitbucket.org/mikespook/gearman-go/gearman/worker"
|
2012-04-09 09:26:06 +08:00
|
|
|
"bitbucket.org/mikespook/golib/signal"
|
|
|
|
"os"
|
2012-03-26 13:32:59 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2011-05-18 20:24:59 +08:00
|
|
|
)
|
|
|
|
|
2012-03-26 13:32:59 +08:00
|
|
|
func ToUpper(job *worker.WorkerJob) ([]byte, error) {
|
|
|
|
data := []byte(strings.ToUpper(string(job.Data)))
|
|
|
|
return data, nil
|
2011-05-18 20:24:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2012-05-10 21:25:33 +08:00
|
|
|
w := worker.New(worker.Unlimit)
|
|
|
|
w.ErrFunc = func(e error) {
|
|
|
|
log.Println(e)
|
|
|
|
}
|
2012-03-26 13:32:59 +08:00
|
|
|
w.AddServer("127.0.0.1:4730")
|
|
|
|
w.AddFunction("ToUpper", ToUpper, 0)
|
|
|
|
w.AddFunction("ToUpperTimeOut5", ToUpper, 5)
|
2011-05-18 20:24:59 +08:00
|
|
|
|
2012-03-27 22:48:03 +08:00
|
|
|
// Catch the interrupt to exit the working loop.
|
2012-04-09 09:26:06 +08:00
|
|
|
sh := signal.NewHandler()
|
|
|
|
sh.Bind(os.Interrupt, func() bool {
|
2012-03-27 22:48:03 +08:00
|
|
|
w.Close()
|
|
|
|
return true
|
2012-04-09 09:26:06 +08:00
|
|
|
})
|
2012-03-27 22:48:03 +08:00
|
|
|
go sh.Loop()
|
|
|
|
|
2012-03-26 13:32:59 +08:00
|
|
|
go func() {
|
|
|
|
log.Println("start worker")
|
|
|
|
for {
|
|
|
|
print("cmd: ")
|
|
|
|
var str string
|
|
|
|
fmt.Scan(&str)
|
|
|
|
switch str {
|
|
|
|
case "echo":
|
|
|
|
w.Echo([]byte("Hello world!"))
|
|
|
|
var job *worker.WorkerJob
|
|
|
|
for job = <-w.JobQueue; job.DataType != gearman.ECHO_RES; job = <-w.JobQueue {
|
|
|
|
log.Println(job)
|
|
|
|
}
|
|
|
|
log.Println(string(job.Data))
|
|
|
|
case "quit":
|
2012-05-10 21:25:33 +08:00
|
|
|
os.Exit(0)
|
2012-03-26 13:32:59 +08:00
|
|
|
return
|
|
|
|
case "result":
|
|
|
|
job := <-w.JobQueue
|
|
|
|
log.Println(string(job.Data))
|
|
|
|
default:
|
|
|
|
log.Println("Unknown command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
w.Work()
|
2011-05-18 20:24:59 +08:00
|
|
|
}
|