-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer2.php
72 lines (63 loc) · 1.83 KB
/
consumer2.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
$lockfile = '/tmp/mytest2.lock';
if(file_exists($lockfile)){
exit();
}else{
file_put_contents($lockfile, 1, true);
}
require_once('./kafka.php');
require_once('./comm_consume.php');
$topic = $argv[1];
try {
runConsumer($topic);
//lock($topic)
} catch (Exception $e) {
logs('consume run error!',1,'consumer',$topic);
unlock(2);
}
function runConsumer($topic){
$lockfile = '/tmp/mytest2.lock';
$startTime = explode(' ',microtime());
$kmlCachePath = getconfig('kmlCachePath');
//本地缓存里存在数据则优先执行
$cacheFiles = getFileList('./cache/'.$topic);
if(!empty($cacheFiles)){
sort($cacheFiles);
foreach($cacheFiles as $f){
$kmls = json_decode(file_get_contents($f));
$items = array_chunk($kmls , 25);
foreach($items as $item){
updataKml($item,$startTime,$f,2,$topic);
}
}
}
// $i = 1;
$f = '';
logs(date('h:i:s',time()).$topic.' start ...',1,'consumer',$topic);
while($da = kafka::getInstance()->get($topic)){
$starttime = explode(' ',microtime());
if(!empty($da->messageList)){
foreach($da->messageList as $d){
$kmls[] = json_decode($d->message);
}
//$i++;
//if($i > 10){
updataKml($kmls,$starttime,$f,1,$topic);
usleep(10);
logs(date('H:i:s').'sleep 10', 1, 'consumer',$topic);
$kmls = [];
/* $i = 1;
}
}else{
if(!empty($kmls)){
updataKml($kmls,$starttime,$f,1, $topic);
}
break;*/
}else{
unlink($lockfile);
exit;
}
}
logs('success total time:'.getTime($startTime), 1, 'consumer',$topic);
unlink($lockfile);
}