Welcome Guest, Not a member yet? Register   Sign In
Integrating pheanstalk with CI
#1

[eluser]msgmash[/eluser]
Hi,

I want to use Pheanstalk with CI but, being a CI newcomer, I'm a little unsure about how to do this. What I have so far is pheanstalk in a directory under /var and a library class coded as follows:

Code:
<?php

require_once('pheanstalk_init.php');

// we're only going to write jobs to the beanstalk server
class MessageQueue {
    private $beanstalk;
    function __construct() {
        $this->beanstalk = new Pheanstalk(); // use default address and port
    }

    function put($body)
    {
        $this->beanstalk->put($body);
    }
}
?>

This then gets called every time a particular controller is requested.

I'm sure there must be a better way to do this, so am seeking advice!

Thanks

Simon
#2

[eluser]vbsaltydog[/eluser]
According to this documentation:

https://github.com/pda/pheanstalk

Your code seems fine for queuing the controller's output to the pheanstalk service.
You may need to exit CI after sending the payload to the queue depending on your situation.

You can also choose to do the pheanstalk->put() in the post system hook.

I don't see the reason for it but thats your business.
#3

[eluser]manasrawat[/eluser]
Hi,
Did you guys figure out how to use pheanstalk with CI?
It will be awesome if you can post an example here. I am a total newbie and am having a hard time figuring this out.
Thanks.
#4

[eluser]tapan.thapa[/eluser]
Hi,

I got success in integrating Pheanstalk with Codeigniter 2.1.4.

Here are the steps.
1. Download Pheastalk from github (https://github.com/pda/pheanstalk) and add pheanstalk folder inside application/libraries.
2. Create pheanstalk.php under application/config and put below content on it.
Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

$config['ip'] = '192.168.0.2'; // Example IP
$config['port'] = 11300;

/* End of file pheanstalk.php */
/* Location: application/config/pheanstalk.php */
3. Create Pheanstalk.php (Please note P is in caps) under application/libraries and paste below content in it.
Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/** Pheanstalk root directory */
if (!defined('PHEANSTALK_ROOT')) {
    define('PHEANSTALK_ROOT', dirname(__FILE__) . '/');
    require(PHEANSTALK_ROOT . 'pheanstalk/pheanstalk_init.php');
}

class Pheanstalk extends Pheanstalk_Pheanstalk {

    public function __construct($params) {
        parent::__construct($params['ip'], $params['port']);
    }

}

/* End of file pheanstalk.php */
4. Example Controller Code
Code:
<?php

/**
* Description of test
*
* @author h0002
*/
class Test extends CI_Controller {

    public function index() {
        $this->load->library('pheanstalk');
        var_dump($this->pheanstalk->listTubes());
        $this->pheanstalk->useTube('testtube')->put("job payload goes here\n");

        $job = $this->pheanstalk
                ->watch('testtube')
                ->ignore('default')
                ->reserve();

        echo $job->getData();

        $this->pheanstalk->delete($job);
    }

}

/* End of file test.php */
/* Location: ./application/controllers/test.php */

Note: All method of pheanstalk should work ideally however i have not tried all methods.

Let me know if it works for you or ping me for any issue.

Thanks & Regards
Tapan Thapa




Theme © iAndrew 2016 - Forum software by © MyBB