func connect() (*beanstalkd.BeanstalkdClient, error) { queue, e := beanstalkd.Dial(config.C.Beanstalk) if e != nil { return nil, e } // Only listen to email queue. queue.Use("email") if _, e := queue.Watch("email"); e != nil { return nil, e } queue.Ignore("default") return queue, nil }
func Mail(path string, key string, diff map[string]diff.Res) error { q, ok := config.C.Queues.Mail[key] if !ok { return ErrNotFound } txt := "" counter := 0 for file, meta := range diff { if len(meta.Diff) == 0 { continue } counter += len(meta.Diff) txt += file + "\n===============================\n\n" + meta.Diff + "\n\n" } if counter == 0 { // no diff return nil } m := Email{ From: q.From, To: q.To, Subject: q.Subject + config.Hostname + " " + path, Text: txt, } w := new(bytes.Buffer) enc := json.NewEncoder(w) if e := enc.Encode(m); e != nil { return e } queue, err := beanstalkd.Dial(q.Beanstalkd) if err != nil { return err } defer queue.Quit() queue.Use("email") _, e := queue.Put( 1, 0*time.Second, 5*time.Second, w.Bytes(), ) return e }
func Newline(path string, diff map[string]diff.Res, conf config.File) error { q, ok := config.C.Queues.Newline[conf.To] if !ok { return ErrNotFound } var lines []LineDiff for file, meta := range diff { if len(meta.Diff) == 0 { continue } if conf.Linediff { for _, line := range strings.Split(meta.Diff, "\n") { lines = append(lines, LineDiff{ Hostname: config.Hostname, Path: file, Line: line, Tags: conf.Tags, }) } } else { txt := meta.Diff lines = append(lines, LineDiff{ Hostname: config.Hostname, Path: file, Line: txt, Tags: conf.Tags, }) } } if len(lines) == 0 { // no diff return nil } // queue lines queue, err := beanstalkd.Dial(q.Beanstalkd) if err != nil { return err } defer queue.Quit() queue.Use(q.Queue) w := new(bytes.Buffer) for _, line := range lines { w.Reset() enc := json.NewEncoder(w) if e := enc.Encode(line); e != nil { return e } _, e := queue.Put( 1, 0*time.Second, 5*time.Second, w.Bytes(), ) if e != nil { return e } } return nil }