2011-05-18 20:24:59 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-05-24 16:49:35 +08:00
|
|
|
"os"
|
2012-03-26 13:32:59 +08:00
|
|
|
"log"
|
2012-08-30 17:56:10 +08:00
|
|
|
"time"
|
2012-03-26 13:32:59 +08:00
|
|
|
"strings"
|
2012-12-21 11:11:37 +08:00
|
|
|
"github.com/mikespook/golib/signal"
|
|
|
|
"github.com/mikespook/gearman-go/worker"
|
2011-05-18 20:24:59 +08:00
|
|
|
)
|
|
|
|
|
2012-05-23 17:45:52 +08:00
|
|
|
func ToUpper(job *worker.Job) ([]byte, error) {
|
2012-08-30 17:56:10 +08:00
|
|
|
log.Printf("ToUpper: Handle=[%s]; UID=[%s], Data=[%s]\n",
|
2012-05-24 16:49:35 +08:00
|
|
|
job.Handle, job.UniqueId, job.Data)
|
2012-03-26 13:32:59 +08:00
|
|
|
data := []byte(strings.ToUpper(string(job.Data)))
|
|
|
|
return data, nil
|
2011-05-18 20:24:59 +08:00
|
|
|
}
|
|
|
|
|
2012-08-30 17:56:10 +08:00
|
|
|
func ToUpperDelay10(job *worker.Job) ([]byte, error) {
|
|
|
|
log.Printf("ToUpperDelay10: Handle=[%s]; UID=[%s], Data=[%s]\n",
|
|
|
|
job.Handle, job.UniqueId, job.Data)
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
data := []byte(strings.ToUpper(string(job.Data)))
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-18 20:24:59 +08:00
|
|
|
func main() {
|
2012-05-24 16:49:35 +08:00
|
|
|
log.Println("Starting ...")
|
|
|
|
defer log.Println("Shutdown complete!")
|
2012-05-23 17:45:52 +08:00
|
|
|
w := worker.New(worker.Unlimited)
|
2012-05-28 10:34:16 +08:00
|
|
|
defer w.Close()
|
2012-05-23 17:45:52 +08:00
|
|
|
w.ErrHandler = func(e error) {
|
2012-05-10 21:25:33 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2012-05-10 21:25:33 +08:00
|
|
|
}
|
2012-05-24 16:49:35 +08:00
|
|
|
w.JobHandler = func(job *worker.Job) error {
|
2012-08-30 16:12:15 +08:00
|
|
|
log.Printf("H=%s, UID=%s, Data=%s, DataType=%d\n", job.Handle,
|
|
|
|
job.UniqueId, job.Data, job.DataType)
|
2012-05-24 16:49:35 +08:00
|
|
|
return nil
|
|
|
|
}
|
2012-03-26 13:32:59 +08:00
|
|
|
w.AddServer("127.0.0.1:4730")
|
2012-09-02 22:42:54 +08:00
|
|
|
w.AddFunc("ToUpper", ToUpper, worker.Immediately)
|
2012-08-30 17:56:10 +08:00
|
|
|
w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5)
|
|
|
|
w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
|
2012-09-25 15:16:17 +08:00
|
|
|
w.AddFunc("SysInfo", worker.SysInfo, worker.Immediately)
|
|
|
|
w.AddFunc("MemInfo", worker.MemInfo, worker.Immediately)
|
2012-05-24 16:49:35 +08:00
|
|
|
go w.Work()
|
|
|
|
sh := signal.NewHandler()
|
|
|
|
sh.Bind(os.Interrupt, func() bool {return true})
|
|
|
|
sh.Loop()
|
2011-05-18 20:24:59 +08:00
|
|
|
}
|