Skip to content
Advertisement

Unable to get Beanstalkd Queue to work for PHP

I have Ubuntu running XAMPP (the lamp stack: Linux, Apache, MySQL, PHP, Pear). I would like to use PHP and Beanstalkd together to make a simple queue that when a user goes on page1.php, a JOB is sent to the QUEUE for a WORKER to capture. The JOB would be an SQL statement that the WORKER would then execute:

What I have done so far is:

  1. Installed Beanstalkd: sudo apt-get install beanstalkd

  2. Developed php code and the “job” that has to be done in page1.php. The job would be to send the sql statement $sql to the queue for the workers to execute (in future versions the job will be much more complex hence the queue system will be even more important).:

page1.php:

if (isset($_SESSION['authenticated']))
{
    //if the user is logged in, send an sql statement to the queue
    $user_id = $_SESSION['id'];
    $sql = "UPDATE user_table SET count = count + 1 WHERE id = {$user_id}";

    //... missing code that would send the statement
}

?>
  1. Developed the actions that have to be done by the WORKER.

WORKER:

<?php

    $stmt = $conn->query($sql);//simple update

?>

PROBLEM / QUESTION:

The problem is I don’t know what functions to call that create a worker, what function to call to send the queue. I have searched online various examples, but there are no complete ones and with very vague explanations. I have seen that something called pheanstalkd exists, which I read was a wrapper for beanstalkd and a lot of people are using it online, but I’m not sure if this is a requirement or not. Can anyone guide me into the right direction with what functions I need to call or what codes I need to execute in the linux terminal just to get this one example working? All feedback is very much appreciated and would help me not loose any more hair this week.

Advertisement

Answer

SOLUTION FOUND:

After some more research, I have managed to get it to work! A decent amount was missing to get to that point. The process was the following:

  1. Execute sudo apt-get install beanstalkd in the linux terminal to install beanstalkd.
  2. Execute sudo apt install composer to install composer, which is the program recommended to be used to install pheanstalk.
  3. Create a composer.json file that will let composer know what library to download and what version of said library. For instance:

    {
      "require": {
        "pdapheanstalk": "2.1.1"
      }
    }
    
  4. Execute composer install in the linux terminal. This has to be done in the same folder as the composer.json file.

  5. Include the necessary code that will initiate the Pheanstalk class, and use it as documented. And that is it! Sample code would be as follows:

    <?php
    
    require_once('vendor/autoload.php');//require the autoload file provided by
                                        //composer
    
    //Initiate an instance of the Pheanstalk class
    $pheanstalk = new Pheanstalk_Pheanstalk('127.0.0.1');
    
    //adding a job to queue/tube testtube:
    $pheanstalk->useTube('testtube')->put('message');
    
    //obtaining the job by a worker:
    $job = $pheanstalk->watch('testtube')->ignore('default')->reserve();
    
    echo $job->getData;//outputting the message
    
    $pheanstalk->delete($job);//deleting the job from the queue.
    
    ?>
    
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement