how to use callback in beanstalkd ?

468 views Asked by At

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

There are 1 answers

5
Sarath Sadasivan Pillai On BEST ANSWER

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