0

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)
}
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
haroldT
  • 153
  • 2
  • 11

1 Answers1

1

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

  • Thinkyou for your help.What your mean is set publisher and consumer on the same machine ? So that it can be “A publish B(consumer) and then B(publisher) publish A”? – haroldT Dec 28 '16 at 06:18
  • Need not be in the same machine. Let's call `master`(publisher) and `workers`(consumers). When master asks workers to do something any one of the workers may own the work and on completion it has to let master know it is done ,for that purpose master may attach a `callbaclurl` with the job request. – Sarath Sadasivan Pillai Dec 28 '16 at 07:08
  • Thank you your explain, so I can not find any function to do that in beanstalk(golang) or beanstalkc(python).I really confuse. – haroldT Dec 28 '16 at 08:19
  • 1
    It is not a queue(or libray) specific thing. Using beanstalk `master` will push the job where job body is a json say `{"id":"Sasa", "job":"actualjob","callback":"http://example.com/status"}` . One `worker` do a `reserve` gets the job decode json and now it knows the job body that is `json.job` and the `status callback` ie json.callback . Once job is finished the worker will do a `delete` on queue for the job and send status to the callback url – Sarath Sadasivan Pillai Dec 28 '16 at 08:26
  • Updated the answer please read . Tick as accepted if that helps you :D – Sarath Sadasivan Pillai Dec 28 '16 at 08:36