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

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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示