Install Beanstalkd in Ubuntu14

Server environment

1
2
3
4
5
6
7
8
9
10
11
$ cat /etc/issue
Ubuntu 14.04.5 LTS

$ beanstalkd -v
beanstalkd 1.9

$ php -v
PHP 5.5.9-1ubuntu4.22

$ composer -V
Composer version 1.5.2

Beanstalkd ‘s core concept of design

  • job:A task that needs to be handled asynchronously is the basic unit in Beanstalkd and needs to be placed in a tube。
  • tube:A task queue, used to store a uniform type of job, is an object of producer and consumer operations。
  • producer:Job producer, put a job by put command into a tube。
  • consumer:Job consumers, through the reserve/ release/ bury/ delete command to get the job or change the status of the job。

Job ‘s life

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
put with delay               release with delay
----------------> [DELAYED] <------------.
| |
kick | (time passes) |
| |
put v reserve | delete
-----------------> [READY] ---------> [RESERVED] --------> *poof*
^ ^ | |
| \ release | |
| `-------------' |
| |
| kick |
| |
| bury |
[BURIED] <---------------'
|
| delete
`--------> *poof*

Job ‘s status

  • READY : Tasks that need to be processed immediately, automatically become the current task when the DELAYED task expires;
  • DELAYED : delay the implementation of the task, when the consumer to deal with the task, you can use the message back to the DELAYED queue delayed execution;
  • RESERVED : Has been consumer acquisition, the ongoing task. Beanstalkd is responsible for checking whether the task is completed within the time-to-run (TTR)
  • BURIED : Reserved tasks: The task will not be executed, it will not disappear unless someone “kicked” it back in the queue;
  • DELETED : The message is completely deleted. Beanstalkd no longer maintain these messages.

Install Beanstalkd

1
$ sudo aptitude install -y beanstalkd

Auto start

1
$ vim /etc/default/beanstalkd

1
2
3
4
5
6
7
8
9
10
11
## Defaults for the beanstalkd init script, /etc/init.d/beanstalkd on
## Debian systems.

BEANSTALKD_LISTEN_ADDR=173.82.218.209
BEANSTALKD_LISTEN_PORT=11300

# You can use BEANSTALKD_EXTRA to pass additional options. See beanstalkd(1)
# for a list of the available options. Uncomment the following line for
# persistent job storage.
BEANSTALKD_EXTRA="-b /var/lib/beanstalkd"
START=yes

Pay attention to this place "-b /var/lib/beanstalkd" which can recovery your queue data after machine shutdown suddenly.
Beanstalkd will find its binlog files which is 10M max size default under this directory

Using Beanstalkd php client

I’m using the php client pda/pheanstalk
It needs php5.3+

Install PHP5.5

1
$ sudo apt-get install -y php5

Install PHP composer

1
2
3
4
5
6
$ sudo apt-get update
$ sudo apt-get install -y curl
$ sudo curl -s https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
$ composer -V
Composer version 1.5.2

Create a Beanstalked PHP demo

1
2
3
4
$ mkdir beanstalkedDemo
$ cd beanstalkedDemo/
$ composer init
$ composer require pda/pheanstalk
  • If dependence package cannot be download, please add Chinese resp in composer.json
    1
    2
    3
    4
    5
    6
    7
    ...
    "repositories": {
    "packagist": {
    "type": "composer",
    "url": "https://packagist.phpcomposer.com"
    }
    }

PHP demo

1
$ vim main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
require 'vendor/autoload.php';
use Pheanstalk\Pheanstalk;

$level = 1;// most urgent: 0, least urgent: 4294967295
$delay = 10; //10s
$ttl = 10; //10s


$pheanstalk = new Pheanstalk('127.0.0.1');

// Create a job then put into the tube named testtube
$jobId = $pheanstalk
->useTube('testtube')
->put(
"job payload goes here\n",
$level,
$delay,
$ttl
);

echo ($jobId) . "\n";

// The consumer watchs the tube, waits a ready job
$job = $pheanstalk
->watch('testtube')
->reserve();

// Deal with the data
echo $job->getData();
// Delete the job after logic success
$pheanstalk->delete($job);
1
2
$ php main.php 
job payload goes here
Share