func NewRunner(verbose bool, logger *log.Logger) (*Runner, error) { this := new(Runner) this.logger = logger q, err := lentil.Dial(Config.QueueAddr) if err != nil { return nil, err } this.q = q this.verbose = verbose return this, nil }
func SendQueue(queue string, content string) { beanstalkd, err := lentil.Dial(Config.Genral.BeanstalkServer) if err != nil { log.Error(err) } else { err = beanstalkd.Use(queue) } if err != nil { log.Error(err) } else { beanstalkd.Put(0, 0, 30, []byte(content)) } }
func NewListener(verbose, inclusive bool, filter []string, logpath string) (*Listener, error) { this := new(Listener) this.logpath = logpath err := this.resetLog() if err != nil { return nil, err } q, err := lentil.Dial(Config.QueueAddr) if err != nil { return nil, err } this.q = q this.verbose = verbose this.jobqueue = NewJobQueue(this.q, inclusive, filter) return this, nil }
//NewQueueService constructs a QueueService func NewQueueService(tube string) *QueueService { conn, e := lentil.Dial(os.Getenv("QUEUE_BEANSTALK_HOST") + ":11300") if e != nil { log.Fatal(e) } tubeName := "" switch tube { case "luminous": tubeName = os.Getenv("QUEUE_PREDICT") case "go": tubeName = os.Getenv("QUEUE_GOPREDICT") } conn.Use(tubeName) return &QueueService{ conn: conn, } }
func (j *Jobsms) Run() { for { Utils.LogInfo("Jobsms delay %d Second", Config.GetLoopTime().Sms) time.Sleep(time.Second * Config.GetLoopTime().Sms) beanstalkd, err := lentil.Dial(Config.GetBeanstalk().Server) if err != nil { Utils.LogPanicErr(err) } else { err = beanstalkd.Use(Config.GetBeanstalk().MobileQueue) } if err != nil { Utils.LogPanicErr(err) } else { for i := 0; i < 10; i++ { job, err := beanstalkd.PeekReady() if err != nil { //Utils.LogPanicErr(err) break } else { body := strings.SplitN(string(job.Body), "\t", 2) if len(body) == 2 { r, err := base64.StdEncoding.DecodeString(body[1]) if err == nil { fmt.Printf("Job id: %d \nmobile: %s \nbody: %s", job.Id, body[0], string(r)) e := SendSms(body[0], string(r)) if e != nil { Utils.LogPanicErr(e) } } } beanstalkd.Delete(job.Id) } } } } }
func BenchmarkPuttingJobsOld(b *testing.B) { b.StopTimer() client, err := lentil.Dial("127.0.0.1:11300") if err != nil { b.Logf("Beanstalkd connection error: ") b.FailNow() } defer client.Quit() var testString string = "{14113 http://www.musicload.de 10 200 OK ms:=http://www.musicload.de#options 10 3 98018 [14 12] 1341217746}" var testTube string = "test_tube" err = client.Use(testTube) if err != nil { b.Logf("Trouble with selecting correct tube in beanstalk %s\n", err) } b.StartTimer() for i := 0; i < b.N; i++ { _, _ = client.Put(10, 0, 120, []byte(testString)) } }
func BenchmarkReservingJobsOld(b *testing.B) { b.StopTimer() client, err := lentil.Dial("127.0.0.1:11300") if err != nil { b.Logf("Beanstalkd connection error: ") b.FailNow() } defer client.Quit() var testTube string = "test_tube" _, err = client.Watch(testTube) if err != nil { b.Logf("Trouble with selecting correct tube in beanstalk %s\n", err) } b.StartTimer() for i := 0; i < b.N; i++ { _, _ = client.Reserve() } }
func (j *Jobmail) Run() { for { Utils.LogInfo("Jobmail delay %d Second", Config.GetLoopTime().Mail) time.Sleep(time.Second * Config.GetLoopTime().Mail) beanstalkd, err := lentil.Dial(Config.GetBeanstalk().Server) if err != nil { Utils.LogPanicErr(err) } else { err = beanstalkd.Use(Config.GetBeanstalk().MailQueue) } if err != nil { Utils.LogPanicErr(err) } else { for i := 0; i < 10; i++ { job, err := beanstalkd.PeekReady() if err != nil { //Utils.LogPanicErr(err) break } else { body := strings.SplitN(string(job.Body), "\t", 4) if len(body) == 4 { r, err := base64.StdEncoding.DecodeString(body[3]) if err == nil { fmt.Printf("Job id: %d \nmail to:%s \nsubject:%s\nbody: , %s", job.Id, body[0], body[1], string(r)) //SendMail( body[0], body[1], string(r), body[2]) SendMailBySohu(body[0], body[1], string(r)) } } beanstalkd.Delete(job.Id) } } } } }
func connect(t *testing.T) *lentil.Beanstalkd { gq := os.Getenv("GLOW_QUEUE") if gq == "" { gq = "localhost:11300" } q, e := lentil.Dial(gq) if e != nil { t.Fatal(e) } // Clear beanstalkd tubes, e := q.ListTubes() if e != nil { t.Fatal(e) } for _, tube := range tubes { if tube == "default" { continue } _, e = q.Watch(tube) if e != nil { t.Fatal(e) } for { job, e := q.ReserveWithTimeout(0) if e != nil { break } q.Delete(job.Id) } _, e := q.Ignore(tube) if e != nil { t.Fatal(e) } } return q }
// 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, } } } }
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 } } }
func main() { lentil.ReaderSize = 65536 flag.Parse() log.SetFlags(0) q, e := lentil.Dial(*addr) err(e) if *listTubes { tubes, e := q.ListTubes() err(e) for _, tube := range tubes { fmt.Println(tube) } os.Exit(0) } if *pauseTube != "" { if *delay == 0 { log.Fatal("Usage: lentil pause-tube=<tube> delay=<seconds>") } e := q.PauseTube(*pauseTube, *delay) err(e) os.Exit(0) } if *statsTube != "" { stats, e := q.StatsTube(*statsTube) err(e) for k, v := range stats { fmt.Printf("%s:%s\n", k, v) } os.Exit(0) } if *stats { stats, e := q.Stats() err(e) for k, v := range stats { fmt.Printf("%s:%s\n", k, v) } os.Exit(0) } if *drain != "" { _, e := q.Watch(*drain) err(e) if *drain != "default" { _, e = q.Ignore("default") err(e) } for { job, e := q.ReserveWithTimeout(0) if e != nil { if e.Error() == "TIMED_OUT\r\n" { break } log.Fatal(e) } fmt.Printf("%d:%s\n", job.Id, job.Body) e = q.Delete(job.Id) err(e) } os.Exit(0) } if *put != "" { e = q.Use(*tube) err(e) id, e := q.Put(*pri, *delay, *ttr, []byte(*put)) err(e) fmt.Printf("id:%d\n", id) os.Exit(0) } if *peek != 0 { job, e := q.Peek(uint64(*peek)) err(e) fmt.Printf("%d:%s\n", job.Id, job.Body) os.Exit(0) } if *peekBuried { e := q.Use(*tube) err(e) job, e := q.PeekBuried() err(e) fmt.Printf("%d:%s\n", job.Id, job.Body) os.Exit(0) } if *peekDelayed { e := q.Use(*tube) err(e) job, e := q.PeekDelayed() err(e) fmt.Printf("%d:%s\n", job.Id, job.Body) os.Exit(0) } if *kick != 0 { e := q.Use(*tube) err(e) kicked, e := q.Kick(*kick) err(e) fmt.Printf("%d\n", kicked) os.Exit(0) } if *del != 0 { e := q.Delete(uint64(*del)) err(e) os.Exit(0) } flag.Usage() os.Exit(1) }