示例#1
0
// this can be created multiple times
func CreateWorker() *worker.Worker {

	// make sure application is bootstrapped and config is ready
	Bootstrap()

	// now lets grab all of the hosts that we need for this application
	hosts, err := Config.Get("gearmanHosts").Array()

	// handler gearman error as it comes about
	if err != nil {

		log.Println("Invalid configuration for gearmanHosts")
	}

	// register a new gearman worker
	w := worker.New(worker.Unlimited)

	// now loop through and add each host
	for _, host := range hosts {

		// assert that the host is a string and add it to the gearmanlist
		if str, ok := host.(string); ok {

			// assert that we pass in a string from the element
			w.AddServer(str)
		}
	}

	return w
}
示例#2
0
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("an error occured ErrHandler")
		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.AddServer("127.0.0.1:4730")
	w.AddFunc("backupForm", backupForm, worker.OneByOne)
	w.AddFunc("backupSubmissions", backupSubmissions, worker.OneByOne)
	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()
}
示例#3
0
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("reverse", Reverse, worker.Immediately)
	go w.Work()
	sh := signal.NewHandler()
	sh.Bind(os.Interrupt, func() bool { return true })
	sh.Loop()
}
示例#4
0
func ExampleWorker() {
	// An example of worker
	w := worker.New(worker.Unlimited)
	defer w.Close()
	// Add a gearman job server
	if err := w.AddServer(worker.Network, "127.0.0.1:4730"); err != nil {
		fmt.Println(err)
		return
	}
	// A function for handling jobs
	foobar := func(job worker.Job) ([]byte, error) {
		// Do nothing here
		return nil, nil
	}
	// Add the function to worker
	if err := w.AddFunc("foobar", foobar, 0); err != nil {
		fmt.Println(err)
		return
	}
	var wg sync.WaitGroup
	// A custome handler, for handling other results, eg. ECHO, dtError.
	w.JobHandler = func(job worker.Job) error {
		if job.Err() == nil {
			fmt.Println(string(job.Data()))
		} else {
			fmt.Println(job.Err())
		}
		wg.Done()
		return nil
	}
	// An error handler for handling worker's internal errors.
	w.ErrorHandler = func(e error) {
		fmt.Println(e)
		// Ignore the error or shutdown the worker
	}
	// Tell Gearman job server: I'm ready!
	if err := w.Ready(); err != nil {
		fmt.Println(err)
		return
	}
	// Running main loop
	go w.Work()
	wg.Add(1)
	// calling Echo
	w.Echo([]byte("Hello"))
	// Waiting results
	wg.Wait()
	// Output: Hello
}
示例#5
0
func main() {
	w := worker.New(worker.OneByOne)
	w.ErrorHandler = func(e error) {
		log.Println(e)
	}
	w.AddServer("tcp4", *GEARMAND)
	w.AddFunc("CAFFE:TRAIN", caffeTrainTask, worker.Unlimited)
	w.AddFunc("CAFFE:TRAIN:STATUS", caffeStatusTask, worker.Unlimited)
	w.AddFunc("CAFFE:TRAIN:PLOT", caffePlotTask, worker.Unlimited)
	w.AddFunc("CAFFE:TRAIN:STOP", caffeTrainStopTask, worker.Unlimited)

	if err := w.Ready(); err != nil {
		log.Fatal(err)
		return
	}
	w.Work()
}
示例#6
0
文件: getter.go 项目: CPSSD/feedlark
func Create() error {
	// Create the worker and connect it to Gearman

	dbhelp.Log(0, "Creating Gearman worker 'db-get'")
	w := worker.New(worker.OneByOne)
	w.ErrorHandler = func(e error) {
		fmt.Println(e)
	}
	w.AddServer("tcp4", "127.0.0.1:4730")
	w.AddFunc("db-get", DBGet, worker.Unlimited)
	if err := w.Ready(); err != nil {
		dbhelp.Log(2, "Fatal error: "+err.Error())
		return err
	}
	w.Work()
	return nil
}
示例#7
0
文件: adder.go 项目: CPSSD/feedlark
func Create() error {
	// Create the worker and connect it to Gearman

	dbhelp.Log(0, "Creating Gearman workers 'db-add' and 'db-update'")
	w := worker.New(worker.OneByOne)
	w.ErrorHandler = func(e error) {
		fmt.Println(e)
	}
	w.AddServer("tcp4", "127.0.0.1:4730")
	w.AddFunc("db-add", DbAdd, worker.Unlimited)
	w.AddFunc("db-update", DbUpdate, worker.Unlimited)
	w.AddFunc("db-upsert", DbUpsert, worker.Unlimited)
	if err := w.Ready(); err != nil {
		fmt.Println("Fatal error")
		fmt.Println(err)
		return err
	}
	w.Work()
	return nil
}
示例#8
0
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 opErr, ok := e.(*net.OpError); ok {
			if !opErr.Temporary() {
				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("Foobar", Foobar, worker.Unlimited)
	w.AddFunc("ToUpper", ToUpper, worker.Unlimited)
	w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5)
	w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20)
	w.AddFunc("SysInfo", worker.SysInfo, worker.Unlimited)
	w.AddFunc("MemInfo", worker.MemInfo, worker.Unlimited)
	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()
}
示例#9
0
func createGearmanWorker(address string) *worker.Worker {
	w := worker.New(worker.Unlimited)
	w.AddServer("tcp4", address)
	return w
}
示例#10
0
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)
		}
	}
}
示例#11
0
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()
}