gearman-go/example/client.go

46 lines
1010 B
Go
Raw Normal View History

package main
import (
"gearman"
"log"
)
func main() {
client := gearman.NewClient()
defer client.Close()
2011-10-09 17:53:55 +08:00
if err := client.AddServer("127.0.0.1:4730"); err != nil {
log.Fatalln(err)
}
echo := []byte("Hello\x00 world")
if data, err := client.Echo(echo); err != nil {
2011-10-09 17:53:55 +08:00
log.Fatalln(string(data))
}
handle, err := client.Do("ToUpper", echo, gearman.JOB_NORMAL)
if err != nil {
2011-10-09 17:53:55 +08:00
log.Fatalln(err)
} else {
log.Println(handle)
2011-12-28 17:59:44 +08:00
job := <-client.JobQueue
if data, err := job.Result(); err != nil {
2011-10-09 17:53:55 +08:00
log.Fatalln(err)
} else {
log.Println(string(data))
2011-12-28 17:59:44 +08:00
}
}
2011-05-24 13:20:22 +08:00
known, running, numerator, denominator, err := client.Status(handle)
if err != nil {
2011-10-09 17:53:55 +08:00
log.Fatalln(err)
}
if !known {
log.Println("Unknown")
}
if running {
2011-05-24 13:20:22 +08:00
log.Printf("%g%%\n", float32(numerator)*100/float32(denominator))
} else {
log.Println("Not running")
}
}