gearman-go/example/worker.go

48 lines
1.3 KiB
Go
Raw Normal View History

2011-05-18 20:24:59 +08:00
package main
import (
"os"
"log"
"strings"
"bitbucket.org/mikespook/golib/signal"
"bitbucket.org/mikespook/gearman-go/worker"
2011-05-18 20:24:59 +08:00
)
func ToUpper(job *worker.Job) ([]byte, error) {
log.Printf("ToUpper -- Handle=[%s]; UID=[%s], Data=[%s]\n",
job.Handle, job.UniqueId, job.Data)
data := []byte(strings.ToUpper(string(job.Data)))
return data, nil
2011-05-18 20:24:59 +08:00
}
func main() {
log.Println("Starting ...")
defer log.Println("Shutdown complete!")
w := worker.New(worker.Unlimited)
2012-05-28 10:34:16 +08:00
defer w.Close()
w.ErrHandler = func(e error) {
log.Println(e)
2012-06-01 14:28:10 +08:00
if e == worker.ErrConnection {
proc, err := os.FindProcess(os.Getpid())
if err != nil {
log.Println(err)
}
if err := proc.Signal(os.Interrupt); err != nil {
log.Println(err)
}
}
}
w.JobHandler = func(job *worker.Job) error {
log.Printf("H=%s, UID=%s, Data=%s, DataType=%d\n", job.Handle,
job.UniqueId, job.Data, job.DataType)
return nil
}
w.AddServer("127.0.0.1:4730")
w.AddFunc("ToUpper", ToUpper, 0)
//w.AddFunc("ToUpperTimeOut5", ToUpper, 5)
go w.Work()
sh := signal.NewHandler()
sh.Bind(os.Interrupt, func() bool {return true})
sh.Loop()
2011-05-18 20:24:59 +08:00
}