Unable to get Beanstalkd Queue to work for PHP
Asked Answered
P

1

1

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.

Pout answered 20/5, 2016 at 21:55 Comment(11)
Yes, you need pheanstalk. It's a PHP library that provides functions to allow you to send and receive messages in a beanstalk queue. That's what we use in our applications, but I don't know the details of installing it, that was done by our system administrator.Suricate
@Suricate Oh ok, so pheanstalk uses beanstalk to make the queue work? I'll research it more online now. Once I get pheanstalk installed, are the functions pretty simple to use? Would you happen to know how it could be used to make something like the previous code work? I just have been looking everywhere online with little luck lately.Pout
Yes, pheanstalk is just an API to beanstalk. There's some example code on the github page: github.com/pda/pheanstalkSuricate
@Suricate I think I'm about to get it to work. Isn't there a security risk though? beanstalkd uses TCP with 127.0.0.1 as the IP address (which is the same computer) on port 11300. Doesn't this allow users from outside to send jobs to the queue if they send jobs to that computers IP on the same port?Pout
I don't know much about configuring beanstalkd, but you may be able to tell it to only listen on 127.0.0.1, not the public IP. Also, you can check for a password in the payload, and ignore messages without it, that's what we do.Suricate
@Suricate awesome. Yeah I just made it work haha! and I will probably take that same approach, send an array encoded with json_encode(), have the first value be a password, and always check if it matches, sounds like it should be safe.Pout
@Suricate Please don't hate me!: #37371188Pout
@Suricate I just noticed... If you place a password in the queue to be viewed by the worker, someone could just snoop the password from the queue by doing: telnet ipaddressofsite 11300, then list-tubes to find the ones being used, then use a_tube_found, then by using peek-ready, he would see everything inside the queued job, including the password. Doesn't this mean that there is in fact still a security issue?Pout
@Suricate and with that password, he could create his own job, with the password that would validate it, and hence hack into the system and execute any jobs he wants. At least that's how it seems after playing around a bit with telnet recently.Pout
@Suricate I asked the users at github page for beanstalkd this issue: (github.com/kr/beanstalkd/issues/320), they mentioned that beanstalkd was never meant to be for an open network. I mean I'm sure there are ways to protect the open network, but the fact it wasn't designed for being safe could mean there might be ways for a hacker to infiltrate? Though I suppose only allowing your IP to have access would be enough.. but I really don't have enough of a background with terminals, port security and such to know the tricks of the trade hahahPout
You could run a VPN or use an SSH proxy between the clients and beanstalk server.Suricate
P
1

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": {
        "pda\pheanstalk": "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.
    
    ?>
    
Pout answered 22/5, 2016 at 1:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.