dongweihuan8610 2016-12-26 03:18
浏览 24
已采纳

在beantalkd中如何使用回调?

There is 3 machines and one publisher, two consumers. I am using golang to publish order to some machine. And the machine is used python to be consumer. I want to know how can I get the result that the order is finished or failed in publisher.

And if the order is not belong to machine one, what should i do? release or bury it?

python: consumer:

import beanstalkc

def get_beanstalk_data(conf):
    beanstalk = beanstalkc.Connection(host='127.0.0.1',port=11300)
    beanstalk.use('cloud')
    beanstalk.watch('cloud')
    beanstalk.ignore('default')
    job = beanstalk.reserve()

    if job.body == "one":  #job.body == "two"
        #TODO
        job.delete()
        return job.body
    else:
        #TODO    what should I do in here, because there is two consumer and get different orders

while True:
    data = get_beanstalk_data(conf)
    print data

golang: publish:

package main

import (
    "fmt"
    "github.com/kr/beanstalk"
    "time"
)

func main() {
    c, err := beanstalk.Dial("tcp", "127.0.0.1:11300")
    id, err := c.Put([]byte("hello"), 1, 0, 120*time.Second)

    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(id)
}
  • 写回答

1条回答 默认 最新

  • dongna1593 2016-12-26 06:03
    关注

    The right way to let publisher know the status of a job is using callbacks

    With job let the publisher put a callback url (a queue or http) and on success or failure of the job th consumer may send a status message to the status call back

    so the job structure may loom like

    //JobRequest has the struct storing request name and body
    type JobRequest struct {
            ID              string
            RequestBody     []byte
            CallbackURL  *string
    }
    

    The json strng of the above struct will be the job body.The consumer would get the CallbackURL and will senbd status to that url

    Trying to explain as reqiuested details

    Let's call producer and consumer(s) master and workers(s) .

    1. When ever a job is available master would create a job object which has

      • job id (A unique value to identify teh job)
      • RequestBody (The details of the job)
      • StatusCallbackURL (URL the worker would hit with the job status)
    2. One of the workers listening to the queue reserve the job,there buy telling i will try to do this job

    3. Decode the json and get the job details .Now does the job
    4. On success delete the job from queue and send status to the CallbackURL
    5. On failure if non temporary failure send status as fail to the CallbackURL and delete the job
    6. If temporary failure do nothing as after reserve timeout over it wil be re-enqueued

    Now this object is converted to json and put in queue

    PS: Do not delete the job before succesfully completing .On Completion or Permanant Failure only delte the job

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效