gearman-go/example/client/client.go

81 lines
1.7 KiB
Go
Raw Normal View History

package main
import (
2013-12-24 14:39:04 +08:00
"github.com/mikespook/gearman-go/client"
2013-12-25 17:01:42 +08:00
"log"
"sync"
2014-03-03 11:40:42 +08:00
"os"
)
func main() {
2013-12-25 17:01:42 +08:00
// Set the autoinc id generator
2013-12-24 14:39:04 +08:00
// You can write your own id generator
2013-12-25 17:01:42 +08:00
// by implementing IdGenerator interface.
// client.IdGen = client.NewAutoIncId()
c, err := client.New(client.Network, "127.0.0.1:4730")
2013-12-25 17:01:42 +08:00
if err != nil {
log.Fatalln(err)
}
defer c.Close()
2013-12-24 14:39:04 +08:00
c.ErrorHandler = func(e error) {
2013-12-25 17:01:42 +08:00
log.Println(e)
2014-03-03 11:40:42 +08:00
os.Exit(1)
2013-12-25 17:01:42 +08:00
}
echo := []byte("Hello\x00 world")
2013-12-24 14:39:04 +08:00
echomsg, err := c.Echo(echo)
2013-09-22 22:58:22 +08:00
if err != nil {
log.Fatalln(err)
}
2013-12-25 17:01:42 +08:00
log.Println(string(echomsg))
jobHandler := func(resp *client.Response) {
2014-01-09 17:58:02 +08:00
switch resp.DataType {
case client.WorkException:
fallthrough
case client.WorkFail:
fallthrough
case client.WorkComplate:
if data, err := resp.Result(); err == nil {
log.Printf("RESULT: %v\n", data)
2014-01-09 17:58:02 +08:00
} else {
log.Printf("RESULT: %s\n", err)
}
case client.WorkWarning:
fallthrough
2014-01-15 10:09:10 +08:00
case client.WorkData:
2014-01-09 17:58:02 +08:00
if data, err := resp.Update(); err == nil {
log.Printf("UPDATE: %v\n", data)
2014-01-09 17:58:02 +08:00
} else {
log.Printf("UPDATE: %v, %s\n", data, err)
2014-01-09 17:58:02 +08:00
}
case client.WorkStatus:
if data, err := resp.Status(); err == nil {
log.Printf("STATUS: %v\n", data)
2014-01-09 17:58:02 +08:00
} else {
log.Printf("STATUS: %s\n", err)
}
default:
log.Printf("UNKNOWN: %v", resp.Data)
2014-01-09 17:58:02 +08:00
}
2013-12-25 17:01:42 +08:00
}
handle, err := c.Do("ToUpper", echo, client.JobNormal, jobHandler)
2013-12-25 17:01:42 +08:00
if err != nil {
log.Fatalln(err)
}
2013-12-24 14:39:04 +08:00
status, err := c.Status(handle)
2013-12-25 17:01:42 +08:00
if err != nil {
log.Fatalln(err)
}
log.Printf("%v", *status)
2014-01-09 17:58:02 +08:00
_, err = c.Do("Foobar", echo, client.JobNormal, jobHandler)
if err != nil {
log.Fatalln(err)
}
log.Println("Press Ctrl-C to exit ...")
var mutex sync.Mutex
mutex.Lock()
mutex.Lock()
}