2011-05-20 17:38:10 +08:00
|
|
|
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"
|
2011-05-20 17:38:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2012-05-22 20:05:39 +08:00
|
|
|
|
2013-12-26 15:28:42 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
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))
|
2013-12-26 15:28:42 +08:00
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
log.Printf("RESULT: %s\n", err)
|
|
|
|
}
|
|
|
|
case client.WorkWarning:
|
|
|
|
fallthrough
|
|
|
|
case client.WorkDate:
|
|
|
|
if data, err := resp.Update(); err == nil {
|
|
|
|
log.Printf("UPDATE: %V\n", data)
|
|
|
|
} else {
|
|
|
|
log.Printf("UPDATE: %V, %s\n", data, err)
|
|
|
|
}
|
|
|
|
case client.WorkStatus:
|
|
|
|
if data, err := resp.Status(); err == nil {
|
|
|
|
log.Printf("STATUS: %V\n", data)
|
|
|
|
} else {
|
|
|
|
log.Printf("STATUS: %s\n", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Printf("UNKNOWN: %V", resp.Data)
|
|
|
|
}
|
2013-12-25 17:01:42 +08:00
|
|
|
}
|
2013-12-26 15:28: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("%t", status)
|
2012-05-23 15:22:29 +08:00
|
|
|
|
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()
|
2011-05-20 17:38:10 +08:00
|
|
|
}
|