61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/mikespook/gearman-go/worker"
|
|
"github.com/mikespook/golib/signal"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func ToUpper(job worker.Job) ([]byte, error) {
|
|
log.Printf("ToUpper: Data=[%s]\n", job.Data())
|
|
data := []byte(strings.ToUpper(string(job.Data())))
|
|
return data, nil
|
|
}
|
|
|
|
func ToUpperDelay10(job worker.Job) ([]byte, error) {
|
|
log.Printf("ToUpper: Data=[%s]\n", job.Data())
|
|
time.Sleep(10 * time.Second)
|
|
data := []byte(strings.ToUpper(string(job.Data())))
|
|
return data, nil
|
|
}
|
|
|
|
func main() {
|
|
log.Println("Starting ...")
|
|
defer log.Println("Shutdown complete!")
|
|
w := worker.New(worker.Unlimited)
|
|
defer w.Close()
|
|
w.ErrorHandler = func(e error) {
|
|
log.Println(e)
|
|
if e == worker.ErrLostConn {
|
|
proc, err := os.FindProcess(os.Getpid())
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
if err := proc.Signal(os.Interrupt); err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
}
|
|
w.JobHandler = func(job worker.Job) error {
|
|
log.Printf("Data=%s\n", job.Data())
|
|
return nil
|
|
}
|
|
w.AddServer("tcp4", "127.0.0.1:4730")
|
|
w.AddFunc("ToUpper", ToUpper, worker.Immediately)
|
|
w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5)
|
|
w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
|
|
w.AddFunc("SysInfo", worker.SysInfo, worker.Immediately)
|
|
w.AddFunc("MemInfo", worker.MemInfo, worker.Immediately)
|
|
if err := w.Ready(); err != nil {
|
|
log.Fatal(err)
|
|
return
|
|
}
|
|
go w.Work()
|
|
sh := signal.NewHandler()
|
|
sh.Bind(os.Interrupt, func() bool { return true })
|
|
sh.Loop()
|
|
}
|