beanstalkd消息队列在生产环境的应用
参考:http://www.360doc.com/content/16/0421/20/16915_552671179.shtml
1
|
sudo apt-get install beanstalkd |
1
|
vim /etc/default/beanstalkd |
1
2
3
|
/etc/init .d /beanstalkd start lsof -i:11300 /etc/init .d /beanstalkd stop |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#!/usr/bin/php <?php require_once 'Configuration.php' ; require_once 'Record.class.php' ; require_once 'BeanStalk.class.php' ; $now =time(); $model = new RecordModel
(); $records = $model ->checkStartRecord( $now ); //print_r($records); //exit(); $beanstalk =
BeanStalk::open ( array ( 'servers' => array ( Configuration:: $record_config [ 'beanStak' ] ), 'select' => 'random
peek' )
); $beanstalk ->use_tube
( 'records' ); foreach ( $records as $record )
{ $beanstalk ->put
( 0, 0, 10, json_encode ( $record )
); } ?> |
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<?php require_once ( 'config.php' ); require_once ( 'func.php' ); require_once ( 'BeanStalk.class.php' ); $beanstalk =
BeanStalk::open( array ( 'servers' => array ( $config [ 'beanStak' ]
), 'select' => 'random
peek' )); $beanstalk ->watch( 'records' ); while (true){ //$beanstalk->watch('records'); $job = $beanstalk ->reserve_with_timeout(); if ( is_object ( $job )){ $data = $job ->get(); $json =json_decode( $data ,true); print_r( $json ); if (! empty ( $json [ "live_name" ])&&! empty ( $json [ "start_time" ])&&! empty ( $json [ "end_time" ])&&! empty ( $json [ "vod_id" ])){ //print_r($json); if (! empty ( $json [ "afterplay" ])&& $json [ "afterplay" ]==1) $cmd = "{$config['afterplaycmd']}
{$json[" live_name "]}
{$json[" vod_id "]}
{$json['start_time']} {$json['end_time']}" ; else $cmd = "{$config['recordcmd']}
{$json[" live_name "]}
{$json[" vod_id "]}
{$json['start_time']} {$json['end_time']}" ; echo $cmd ; $chkcmd = "ps
-ef |grep '" . $cmd . "'
|grep -v 'grep'|wc -l" ; //$chkcmd="ps
-ef |wc -l"; //echo
$chkcmd; $count =system( $chkcmd ); //echo
$count; if ( $count ==0) { //system($cmd); exec ( $cmd , $res , $rc ); //print_r($res); //print_r($rc); } Beanstalk:: delete ( $job ); //
Delete the job. $info = array (); $info [ "vod_id" ]= $json [ 'vod_id' ]; $info [ "record_msg" ]= "startjob" ; $data = array (); $data [ "type" ]= "reciveRecords" ; $data [ "message" ]= $info ; $url = $config [ 'recordStatus' ]; $httpcode =
200; $result =
test_api( $httpcode , $url , "post" ,json_encode( $data )); print_r( $data ); } //$beanstalk->watch('records'); } sleep(1); } ?> |