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 {
|
2014-03-07 17:21:45 +08:00
|
|
|
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 {
|
2014-03-07 17:21:45 +08:00
|
|
|
log.Printf("UPDATE: %v\n", data)
|
2014-01-09 17:58:02 +08:00
|
|
|
} else {
|
2014-03-07 17:21:45 +08:00
|
|
|
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 {
|
2014-03-07 17:21:45 +08:00
|
|
|
log.Printf("STATUS: %v\n", data)
|
2014-01-09 17:58:02 +08:00
|
|
|
} else {
|
|
|
|
log.Printf("STATUS: %s\n", err)
|
|
|
|
}
|
|
|
|
default:
|
2014-03-07 17:21:45 +08:00
|
|
|
log.Printf("UNKNOWN: %v", resp.Data)
|
2014-01-09 17:58:02 +08:00
|
|
|
}
|
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)
|
|
|
|
}
|
2014-03-07 17:21:45 +08:00
|
|
|
log.Printf("%v", *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
|
|
|
}
|