John John - 20 days ago 9
PHP Question

PHP / Python Beanstalk sockets event Job

I'm using Phalcon php server side and I send job to beanstalk as the doc said.

To execute all jobs inside beanstalk I have a python script. This script search if there is existing jobs and execute them. But for the moment I execute this script with linux command line.

Then, I want to execute my python script automatically when a job was created and execute the task. I thought about sockets.

It's possible to catch the beanstalk socket event when it receives a job and execute my python script ?

Answer

Finally I wrote a python script with Tornado Beanstalkt.

Here is my code.

import tornado
import beanstalkt

def show(msg, value, cb):
  print(msg % value)
  cb()

def stop():
  client.close(ioloop.stop)

def connect(s):
   print('Connection established')
   reserve()

def reserve():
  client.reserve(callback=lambda s: show(
      "Reserved job %s", s, lambda: delete(s["id"])))

def delete(job_id):
  client.delete(job_id, callback=lambda s: show(
      "Deleted job with id %d", job_id, reserve))

ioloop = tornado.ioloop.IOLoop.instance()
client = beanstalkt.Client(host='my-server', port=11300)
client.connect(callback=connect)
client.watch("my-tube")
ioloop.start()

Theses line will establish the connection to my beanstalk server.

client.connect(callback=connect)
client.watch("my-tube")
ioloop.start()

I'll start the worker once the connect is successful that is why I'm using a callback inside the connect method. The watch method focus on the tube that I want and start method will start the async loop non blocking I/O.

In my connect function I'll call reserve function. If no job is available to be reserved and no timeout is given then reserve will wait for a futur job and will be triggered with a beanstalk socket.

After that I can give what I want inside the reserve function. Here I send the show function for test my process. After my print I'll call the delete callback to delete the current job.

Hope it helps.

Comments