github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/task/gearman/producer.go (about) 1 package gearman 2 3 import ( 4 "fmt" 5 "github.com/johnnyeven/libtools/task/constants" 6 "github.com/mikespook/gearman-go/client" 7 "github.com/sirupsen/logrus" 8 ) 9 10 type GearmanProducer struct { 11 client *client.Client 12 } 13 14 func NewGearmanProducer(info constants.ConnectionInfo) *GearmanProducer { 15 c, err := client.New(info.Protocol, fmt.Sprintf("%s:%d", info.Host, info.Port)) 16 if err != nil { 17 logrus.Panicf("NewGearmanProducer err: %v", err) 18 } 19 20 return &GearmanProducer{ 21 client: c, 22 } 23 } 24 25 func (c *GearmanProducer) SendTask(task *constants.Task) error { 26 data, err := constants.MarshalData(task) 27 if err != nil { 28 logrus.Errorf("GearmanProducer.SendTask err: %v", err) 29 return err 30 } 31 _, err = c.client.DoBg(task.Channel, data, client.JobNormal) 32 if err != nil { 33 return err 34 } 35 36 return nil 37 } 38 39 func (c *GearmanProducer) Stop() { 40 c.client.Close() 41 }