本文整理匯總了Golang中github.com/kr/beanstalk.Conn.Reserve方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.Reserve方法的具體用法?Golang Conn.Reserve怎麽用?Golang Conn.Reserve使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/kr/beanstalk.Conn
的用法示例。
在下文中一共展示了Conn.Reserve方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: withRandomReservedJob
func withRandomReservedJob(t *testing.T, conn *beanstalk.Conn) *Job {
withRandomJob(t, conn)
id, body, err := conn.Reserve(0)
if err != nil {
t.Fail()
}
return NewJob(conn, "default", id, body)
}
示例2: consumer
func consumer() {
var c *beanstalk.Conn = nil
for {
// connect to beanstalkd, infinite loop,
if c == nil {
var err error = nil
c, err = beanstalk.Dial("tcp", CONN_BS_HOST+":"+CONN_BS_PORT)
if err != nil {
fmt.Println("Error connect to beanstalkd:", err.Error())
} else {
fmt.Println("Consumer connect to beanstalkd successfully.")
}
}
/*
keys := make([]string, 0, len(mSNConn))
for k := range mSNConn {
keys = append(keys, k)
}
tubeSet := beanstalk.NewTubeSet(c, keys...)
id, body, err := tubeSet.Reserve(5*time.Second)
*/
// blocking api
id, body, err := c.Reserve(120 * time.Second)
if err != nil {
// this err indicate the job queue is empty
//fmt.Println("Error comsume beanstalk:", err.Error())
} else {
//
val, ok := mSNConn[string(body[:3])]
if ok {
val.Write(body)
}
// delete job
c.Delete(id)
fmt.Printf("task id is: 【%d】; task content is 【%s】\n", id, string(body))
}
}
}
示例3: main
func main() {
flag.Parse()
if len(ignoreChannels) > 0 {
common.SetIgnores(ignoreChannels)
}
var (
bs *beanstalk.Conn
err error
)
if len(flag.Args()) >= 1 {
bs, err = beanstalk.Dial("tcp", flag.Args()[0])
if err != nil {
log.Fatal(err)
}
if !quiet {
log.Printf("Connected to [%s]", flag.Args()[0])
}
if len(flag.Args()) >= 2 {
bs.Tube.Name = flag.Args()[1]
}
} else {
log.Fatalf("provide the beanstalk publisher! like example.com:11300")
}
//Clear out the old messages before we start back up
for {
id, msg, err := bs.Reserve(5 * time.Second)
if !quiet {
noti_msg := common.IrcNotify{}
json.Unmarshal(msg, ¬i_msg)
log.Printf("removing old message [%s]", noti_msg.Message)
}
if err != nil {
break
}
err = bs.Delete(id)
if err != nil {
log.Fatal(err)
}
}
for {
id, msg, err := bs.Reserve(5 * time.Second)
if err == nil {
noti_msg := common.IrcNotify{}
json.Unmarshal(msg, ¬i_msg)
go common.Display(noti_msg, linger, quiet)
err = bs.Delete(id)
if err != nil {
log.Fatal(err)
}
}
time.Sleep(500 * time.Millisecond)
}
}