示例#1
0
func main() {
	var wg sync.WaitGroup
	// Set the autoinc id generator
	// You can write your own id generator
	// by implementing IdGenerator interface.
	client.IdGen = client.NewAutoIncId()

	c, err := client.New("127.0.0.1:4730")
	if err != nil {
		log.Fatalln(err)
	}
	defer c.Close()
	c.ErrHandler = func(e error) {
		log.Println(e)
		panic(e)
	}
	echo := []byte("Hello\x00 world")
	wg.Add(1)
	c.Echo(echo)
	wg.Add(1)
	jobHandler := func(job *client.Job) {
		log.Printf("%s", job.Data)
		wg.Done()
	}
	handle := c.Do("ToUpper", echo, client.JOB_NORMAL, jobHandler)

	wg.Add(1)
	log.Printf("%t", c.Status(handle))

	wg.Wait()
}
示例#2
0
func submit(funcName string, workload []byte) ([]byte, error) {
	var mutex sync.Mutex
	var result []byte
	var errResult error
	c, err := client.New("tcp4", *GEARMAND)
	if err != nil {
		return nil, err
	}
	defer c.Close()
	c.ErrorHandler = func(e error) {
		log.Println(e)
	}
	jobHandler := func(resp *client.Response) {
		result, errResult = resp.Result()
		mutex.Unlock()
	}
	_, err = c.Do(funcName, workload, client.JobNormal, jobHandler)
	if err != nil {
		log.Printf("Gearman Doing %s Error: %s\n", funcName, err)
		return nil, err
	}
	mutex.Lock()
	mutex.Lock()
	if bytes.Equal(result, []byte("error")) {
		errResult = errors.New("Gearman doing " + funcName + " Error")
	}
	return result, errResult
}
// test the goRoutine runner. This should be automatically started when we call the function etc
func (s *TestSuite) TestWorkerGoRoutine(c *C) {

	// create a channel for triggering our workers on / off
	push := make(chan int)

	// create a wait group to manage go routines running else where
	var waitGroup sync.WaitGroup

	// add to our wait group because we want to wait for the various tasks to complete etc
	waitGroup.Add(1)

	// initialize our worker and create a worker goroutine for handling tasks
	Worker(Tasks[0:1], push)

	// now we actually want to handle clients etc and ensure that we are getting a basic response back ...
	gearmanClient, _ := client.New("127.0.0.1:4730")

	// close the connection once we are finished
	defer gearmanClient.Close()

	// now create sample data to send to the element
	data := []byte("sampledata")

	// call back function for job
	jobHandler := func(job *client.Job) {

		c.Assert(string(job.Data), Equals, "test::sample::return")

		// we can kill our server now that we know its working
		push <- 0

		// decrement the wait group now
		waitGroup.Done()
	}

	// Initialize task etc
	gearmanClient.Do(Tasks[0].TaskName, data, client.JOB_HIGH, jobHandler)

	// now wait for until we have a response from the server before doing anything else
	waitGroup.Wait()
}
示例#4
0
// can create multiple clients for this particular application
// for now we need to let this be ... come back later on https://github.com/mikespook/gearman-go/blob/master/example/client/client.go
func CreateClient() *client.Client {

	// ensure that application is booted up and ready to go
	Bootstrap()

	// initialize first host
	hosts, _ := Config.Get("gearmanHosts").Array()

	// lets make sure we can safely convert the host to a string
	if str, ok := hosts[0].(string); ok {

		// now initialize a client
		c, _ := client.New(str)

		// return our client
		return c
	}

	// error handler etc for this functionality
	return nil
}
示例#5
0
func main() {
	var wg sync.WaitGroup
	// Set the autoinc id generator
	// You can write your own id generator
	// by implementing IdGenerator interface.
	// client.IdGen = client.NewAutoIncId()

	c, err := client.New(client.Network, "127.0.0.1:4730")
	if err != nil {
		log.Fatalln(err)
	}
	defer c.Close()
	c.ErrorHandler = func(e error) {
		log.Println(e)
	}
	echo := []byte("Hello\x00 world")
	wg.Add(1)
	echomsg, err := c.Echo(echo)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(string(echomsg))
	wg.Done()
	jobHandler := func(resp *client.Response) {
		log.Printf("%s", resp.Data)
		wg.Done()
	}
	handle, err := c.Do("ToUpper", echo, client.JobNormal, jobHandler)
	if err != nil {
		log.Fatalln(err)
	}
	wg.Add(1)
	status, err := c.Status(handle)
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("%t", status)

	wg.Wait()
}
示例#6
0
func main() {
	var wg sync.WaitGroup

	c, err := client.New("127.0.0.1:4730")
	if err != nil {
		log.Fatalln(err)
	}
	defer c.Close()
	echo := []byte("Hello\x00 world")
	c.JobHandler = func(job *client.Job) error {
		log.Printf("%s", job.Data)
		wg.Done()
		return nil
	}

	c.ErrHandler = func(e error) {
		log.Println(e)
		panic(e)
	}
	wg.Add(1)
	c.Echo(echo)
	wg.Add(1)
	handle, err := c.Do("ToUpper", echo, client.JOB_NORMAL)
	if err != nil {
		log.Fatalln(err)
	} else {
		log.Println(handle)
	}

	c.StatusHandler = func(handle string, known, running bool, numerator, denominator uint64) {
		log.Printf("%s: %b, %b, %d, %d", handle, known, running, numerator, denominator)
		wg.Done()
	}
	wg.Add(1)
	c.Status(handle)

	wg.Wait()
}
示例#7
0
func addBackupTasks(w http.ResponseWriter, r *http.Request) {
	var jcount = 0 //counter holding job count
	//redis := new(redis_back.RedisBack)
	//redis.Init()
	var wg sync.WaitGroup //create sync.WaitGroup for
	//let put redis aside for now! just stick with gearman

	body, _ := ioutil.ReadAll(r.Body)

	var tasks []definitions.Task
	//fmt.Println("received tasks in raw format => " ,string(body))

	json.Unmarshal(body, &tasks)

	jobHandler := func(job *client.Job) {
		wg.Done() //release the lock on
		log.Println("JOB COMPLETED")
	}

	//define function that handle sending of tasks
	sendTasks := func() {
		//connect gearman
		c, _ := client.New("127.0.0.1:4730")
		// ...
		defer c.Close()
		c.ErrHandler = func(e error) {
			log.Println(e)
			panic(e)
		}
		//lets create tasks
		for _, task := range tasks {
			//add backupForm Task  //example api key, TODO: receive jotform api key via cookie :)
			jcount = jcount + 1
			gtask := definitions.GearTask{
				"form",
				task.Id,
				"05f5108864eb5ee828ef9b7f8218b448",
				"",
				jcount}
			gtask_arr, _ := json.Marshal(gtask)
			wg.Add(1) //lock wg
			c.Do("backupForm", gtask_arr, client.JOB_BG, jobHandler)

			fmt.Println("backupForm task added for form id "+task.Id+" TaskID =>  ", jcount)
			//add backupSubmissons Task
			if len(task.SubmissionTasks) != 0 {
				for _, sub_range := range task.SubmissionTasks {
					gtask := definitions.GearTask{
						"submissions",
						task.Id,
						"05f5108864eb5ee828ef9b7f8218b448",
						string(sub_range[0]) + "-" + string(sub_range[1]),
						jcount}
					_, _ = json.Marshal(gtask)
					//_ = c.Do("backupSubmissions", gtask_arr, client.JOB_NORMAL, jobHandler)
					//fmt.Println("backupSubmissions task added for form id "+task.Id+" and for range "+string(sub_range[0])+"-"+string(sub_range[1]))
				}
			}

		}

	}

	go sendTasks()
	wg.Wait()
	fmt.Println("Tasks added")

}
示例#8
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)
		}
	}
}
示例#9
0
func main() {
	// Set the autoinc id generator
	// You can write your own id generator
	// by implementing IdGenerator interface.
	// client.IdGen = client.NewAutoIncId()

	c, err := client.New(client.Network, "127.0.0.1:4730")
	if err != nil {
		log.Fatalln(err)
	}
	defer c.Close()
	c.ErrorHandler = func(e error) {
		log.Println(e)
		os.Exit(1)
	}
	echo := []byte("Hello\x00 world")
	echomsg, err := c.Echo(echo)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(string(echomsg))
	jobHandler := func(resp *client.Response) {
		switch resp.DataType {
		case client.WorkException:
			fallthrough
		case client.WorkFail:
			fallthrough
		case client.WorkComplate:
			if data, err := resp.Result(); err == nil {
				log.Printf("RESULT: %v\n", data)
			} else {
				log.Printf("RESULT: %s\n", err)
			}
		case client.WorkWarning:
			fallthrough
		case client.WorkData:
			if data, err := resp.Update(); err == nil {
				log.Printf("UPDATE: %v\n", data)
			} else {
				log.Printf("UPDATE: %v, %s\n", data, err)
			}
		case client.WorkStatus:
			if data, err := resp.Status(); err == nil {
				log.Printf("STATUS: %v\n", data)
			} else {
				log.Printf("STATUS: %s\n", err)
			}
		default:
			log.Printf("UNKNOWN: %v", resp.Data)
		}
	}
	handle, err := c.Do("ToUpper", echo, client.JobNormal, jobHandler)
	if err != nil {
		log.Fatalln(err)
	}
	status, err := c.Status(handle)
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("%v", *status)

	_, 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()
}