func main() { log.Println("Starting ...") defer log.Println("Shutdown complete!") w := worker.New(worker.Unlimited) defer w.Close() w.ErrHandler = func(e error) { log.Println(e) 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) } } } w.JobHandler = func(job *worker.Job) error { log.Printf("H=%s, UID=%s, Data=%s, DataType=%d\n", job.Handle, job.UniqueId, job.Data, job.DataType) return nil } w.AddServer("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) go w.Work() sh := signal.NewHandler() sh.Bind(os.Interrupt, func() bool { return true }) sh.Loop() }
func TestJobs(t *testing.T) { w := worker.New(worker.Unlimited) if err := w.AddServer(GEARMAND); err != nil { t.Error(err) return } defer w.Close() if err := w.AddFunc("ToUpper", ToUpper, 0); err != nil { t.Error(err) return } if err := w.AddFunc("Sleep", Sleep, 0); err != nil { t.Error(err) return } w.ErrHandler = func(e error) { t.Error(e) } go w.Work() c, err := client.New(GEARMAND) if err != nil { t.Error(err) return } defer c.Close() c.ErrHandler = func(e error) { // t.Error(e) t.Log(e) } { var w sync.WaitGroup jobHandler := func(job *client.Job) { upper := strings.ToUpper(STR) if string(job.Data) != upper { t.Error("%s expected, got %s", []byte(upper), job.Data) } w.Done() } w.Add(1) handle := c.Do("ToUpper", []byte(STR), client.JOB_NORMAL, jobHandler) w.Wait() status, err := c.Status(handle, time.Second) if err != nil { t.Error(err) return } if status.Known { t.Errorf("%s shouldn't be known", status.Handle) return } if status.Running { t.Errorf("%s shouldn't be running", status.Handle) } } { handle := c.DoBg("Sleep", nil, client.JOB_NORMAL) time.Sleep(time.Second) status, err := c.Status(handle, time.Second) if err != nil { t.Error(err) return } if !status.Known { t.Errorf("%s should be known", status.Handle) return } if !status.Running { t.Errorf("%s should be running", status.Handle) } } { status, err := c.Status("not exists handle", time.Second) if err != nil { t.Error(err) return } if status.Known { t.Errorf("%s shouldn't be known", status.Handle) return } if status.Running { t.Errorf("%s shouldn't be running", status.Handle) } } }
func main() { log.Message("Starting ... ") defer func() { time.Sleep(time.Second) log.Message("Shutdown complate!") }() // init profiling file if *proffile != "" { log.Debugf("Open a profiling file: %s", *proffile) if err := prof.Start(*proffile); err != nil { log.Error(err) } else { defer prof.Stop() } } // init heap dumping file if *dumpfile != "" { log.Debugf("Open a heap dumping file: %s", *dumpfile) if err := prof.NewDump(*dumpfile); err != nil { log.Error(err) } else { defer prof.CloseDump() go func() { for prof.Dumping { time.Sleep(time.Duration(*dumptime) * time.Second) prof.Dump() } }() } } // init pid file log.Debugf("Open a pid file: %s", *pidfile) if pidFile, err := pid.New(*pidfile); err != nil { log.Error(err) } else { defer pidFile.Close() } w := worker.New(*joblimit) if err := w.AddServer(*gearmand); err != nil { log.Error(err) return } if err := w.AddFunc("exec", execShell, uint32(*timeout)); err != nil { log.Error(err) return } if err := w.AddFunc("execphp", execPHP, uint32(*timeout)); err != nil { log.Error(err) return } defer w.Close() go w.Work() // signal handler sh := signal.NewHandler() sh.Bind(os.Interrupt, func() bool { return true }) sh.Loop() }