Ejemplo n.º 1
0
func put(t *testing.T, jobName, tube string, delay int, q *lentil.Beanstalkd) {
	job := make(map[string]string)
	job["tube"] = tube
	job["name"] = jobName
	jobjson, _ := json.Marshal(job)
	e := q.Use(tube)
	if e != nil {
		t.Fatal(e)
	}
	_, e = q.Put(0, delay, 60, jobjson)
	if e != nil {
		t.Error(e)
	}
}
Ejemplo n.º 2
0
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *ToBeanstalkd) Run() {
	var conn *lentil.Beanstalkd
	var tube = "default"
	var ttr = 0
	var host = ""
	var err error
	for {
		select {
		case msgI := <-b.inrule:
			// set hostname for beanstalkd server
			host, err = util.ParseString(msgI, "Host")
			if err != nil {
				b.Error(err.Error())
				continue
			}
			// set tube name
			tube, err = util.ParseString(msgI, "Tube")
			if err != nil {
				b.Error(errors.New("Could not parse tube name, setting to 'default'"))
				tube = "default"
			}
			// set time to reserve
			ttr, err = util.ParseInt(msgI, "TTR")
			if err != nil || ttr < 0 {
				b.Error(errors.New("Error parsing TTR. Setting TTR to 0"))
				ttr = 0
			}
			// create beanstalkd connection
			conn, err = lentil.Dial(host)
			if err != nil {
				// swallowing a panic from lentil here - streamtools must not die
				b.Error(errors.New("Could not initiate connection with beanstalkd server"))
				continue
			}
			// use the specified tube
			conn.Use(tube)
		case <-b.quit:
			// close connection to beanstalkd and quit
			if conn != nil {
				conn.Quit()
			}
			return
		case msg := <-b.in:
			// deal with inbound data
			msgStr, err := json.Marshal(msg)
			if err != nil {
				b.Error(err)
				continue
			}
			if conn != nil {
				_, err := conn.Put(0, 0, ttr, msgStr)
				if err != nil {
					b.Error(err.Error())
				}
			} else {
				b.Error(errors.New("Beanstalkd connection not initated or lost. Please check your beanstalkd server or block settings."))
			}
		case respChan := <-b.queryrule:
			// deal with a query request
			respChan <- map[string]interface{}{
				"Host": host,
				"Tube": tube,
				"TTR":  ttr,
			}
		}
	}
}
Ejemplo n.º 3
0
func ToBeanstalkd(b *Block) {

	type toBeanstalkdRule struct {
		Host string
		Tube string
		TTR  int
	}
	var conn *lentil.Beanstalkd
	var e error
	var tube = "default"
	var ttr = 0
	var rule *toBeanstalkdRule

	for {
		select {
		case m := <-b.Routes["set_rule"]:
			if rule == nil {
				rule = &toBeanstalkdRule{}
			}
			unmarshal(m, rule)

			conn, e = lentil.Dial(rule.Host)
			if e != nil {
				log.Println(e.Error())
			} else {
				if len(rule.Tube) > 0 {
					tube = rule.Tube
				}
				if rule.TTR > 0 {
					ttr = rule.TTR
				}
				conn.Use(tube)
				log.Println("initialized connection using tube:", tube)
			}

		case r := <-b.Routes["get_rule"]:
			if rule == nil {
				marshal(r, &toBeanstalkdRule{})
			} else {
				marshal(r, rule)
			}

		case msg := <-b.InChan:
			if rule == nil {
				break
			}
			msgStr, err := json.Marshal(msg.Msg)
			if err != nil {
				log.Println("wow bad json")
				break
			}
			if conn == nil {
				log.Panic("Connection to beanstalkd was dropped. Something is not right.")
				break
			}
			jobId, err := conn.Put(0, 0, ttr, msgStr)
			if err != nil {
				log.Println(err.Error())
			} else {
				log.Println("put job on queue: Job Id:", jobId)
			}
		case msg := <-b.AddChan:
			updateOutChans(msg, b)
		case <-b.QuitChan:
			quit(b)
			return
		}
	}
}