-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLog2Ck.php
186 lines (166 loc) · 4.49 KB
/
Log2Ck.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Class Log2Ck
* @author tanszhe [email protected]
*/
class Log2Ck
{
public $max_flow_during_time = 3;
public $min_block_len = 100;
public $max_cache_data_len = 100000;
protected $last_time = 0;
protected $data = [];
protected $last_fail_time = 0;
/**
* @var null|OneCk\Client
*/
protected $ck = null;
/**
* @var null|\Closure
*/
protected $log_fn = null;
protected $table_name = '';
protected $table_fields = [];
protected $db_conf = [];
public function __construct($db_conf, $table, array $fields)
{
$this->table_name = $table;
$this->table_fields = $fields;
$this->db_conf = $db_conf;
$this->getCk(1);
}
public function regLogFn($call)
{
$this->log_fn = $call;
return $this;
}
protected function listenBlock($fd)
{
while (true) {
yield fgets($fd);
}
}
protected function listen($fd)
{
do {
$read = [$fd];
$write = [];
$except = [];
$result = stream_select($read, $write, $except, 0);
echo time() . PHP_EOL;
if ($result === 0) {
sleep(1);
$result = 2;
yield null;
} else {
yield fgets($fd);
}
} while ($result);
}
public function run()
{
foreach ($this->listenBlock(STDIN) as $info) {
if ($info) {
$fn = $this->log_fn;
$row = $fn($info);
if ($row !== false) {
$this->data[] = $row;
}
}
if ($this->last_fail_time < time() - 3) {
$this->save();
}
}
}
protected function getCk($i = 0)
{
try {
$this->ck = new \OneCk\Client(
$this->db_conf['host'],
$this->db_conf['username'],
$this->db_conf['password'],
$this->db_conf['database']
);
} catch (\Exception $e) {
echo 'error: ' . $e->getMessage() . PHP_EOL;
return false;
}
if ($i) {
return $this->start();
}
return true;
}
protected function end()
{
try {
$this->ck->writeEnd();
echo 'write end' . PHP_EOL;
} catch (\Exception $e) {
if ($this->getCk() === false) {
echo "write end fail \n";
return false;
}
}
return $this->start(0);
}
protected function start($i = 0)
{
try {
$this->ck->writeStart($this->table_name, $this->table_fields);
echo 'write start' . PHP_EOL;
return true;
} catch (\Exception $e) {
echo 'start Exception:' . $e->getMessage() . PHP_EOL;
if ($i === 0) {
usleep(10000);
if ($this->getCk()) {
return $this->start(++$i);
}
}
echo "write start fail \n";
return false;
}
}
protected function write($i = 0)
{
try {
$this->ck->writeBlock($this->data);
echo 'write count:' . count($this->data) . PHP_EOL;
return true;
} catch (\Exception $e) {
$this->last_fail_time = time();
echo 'write Exception:' . $e->getMessage() . PHP_EOL;
if ($i === 0) {
usleep(10000);
if ($this->getCk(1)) {
return $this->write(++$i);
}
}
$this->data = array_slice($this->data, -$this->max_cache_data_len);
echo 'date len:' . count($this->data) . PHP_EOL;
return false;
}
}
protected function save()
{
if (count($this->data) >= $this->min_block_len) {
if ($this->write()) {
$this->data = [];
} else {
return false;
}
}
if (($this->last_time + $this->max_flow_during_time) < time()) {
$this->last_time = time();
if ($this->data) {
if ($this->write()) {
$this->data = [];
} else {
return false;
}
}
return $this->end();
}
return true;
}
}