-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoclean.php
executable file
·485 lines (484 loc) · 18.8 KB
/
autoclean.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
if (PHP_SAPI !== 'cli') { die(); }
ini_set('memory_limit', '1024M');
require_once('config.php');
#stream_context_set_default(array('http' => array('proxy' => 'tcp://192.168.233.253:7890', 'request_fulluri' => true)));
function LogStr(string $message, int $status = 0, bool $logToFile = true): bool {
$logType = ($status === -1 ? '错误' : '信息');
$date = date('Y-m-d');
$time = date('H:i:s');
$logStr = "[{$date} {$time}][{$logType}] {$message}.\n";
echo $logStr;
if ($logToFile) {
if (!file_put_contents(LogDir . "/{$date}.log", $logStr, FILE_APPEND)) {
LogStr('日志写入失败', -1, false);
return false;
}
}
return true;
}
function SendMessage(string $message) {
if (empty($message)) {
LogStr('发送消息失败, 因为消息为空', -1);
return false;
}
return @file_get_contents(TG_API_URL . 'sendMessage?chat_id=' . TG_GROUP . '&text=' . rawurlencode($message));
}
function RestrictChatMember(int $userID, array $permissions = array('can_send_messages' => false, 'can_send_media_messages' => false, 'can_send_other_messages' => false, 'can_add_web_page_previews' => false), int $untilDate = -1) {
if (empty($permissions)) {
return false;
}
$permissionsJSON = json_encode($permissions);
if ($untilDate === -1) {
$untilDate = time() + 43200;
}
return @file_get_contents(TG_API_URL . "restrictChatMember?chat_id=" . TG_GROUP . "&user_id={$userID}&permissions={$permissionsJSON}&until_date={$untilDate}");
}
/*
function ipv6Filter(int $var) {
return ($var === 1);
}
*/
function ConnectDB(): int {
global $db;
if (DBPort === null) {
$db = null;
return 0;
}
if ($db === null) {
$db = @new MySQLi(DBAddress, DBUser, DBPass, DBName, DBPort, DBSocket);
}
if (!isset($db->connect_errno) || !is_int($db->connect_errno)) {
LogStr('连接数据库时发生错误, 错误代码: 未知', -1);
return -1;
}
if ($db->connect_errno > 0) {
LogStr('连接数据库时发生错误, 错误代码: ' . $db->connect_errno, -1);
$db = null;
return -1;
}
return 1;
}
function CloseDB(): bool {
global $db;
if ($db !== null) {
$db->close();
$db = null;
}
return true;
}
function ConnectCache(): bool {
global $cache;
try {
if ($cache === null) {
$cache = new Redis();
$cache->connect(CacheAddress, CachePort, CacheTimeout_Autoclean);
if (CacheAuth !== null) {
$cache->auth(CacheAuth);
}
}
if ($cache->ping() !== true) {
CloseCache();
LogStr('连接缓存时发生错误', -1);
return false;
}
} catch (Exception $e) {
CloseCache();
LogStr('连接缓存时发生错误', -1);
return false;
}
return true;
}
function CloseCache(): bool {
global $cache;
if ($cache !== null) {
$cache->close();
$cache = null;
}
return true;
}
function GetNginxPID(bool $getFromFile = true): int {
$getMethod = ($getFromFile ? 'PID 文件' : '进程匹配');
if ($getFromFile) {
$nginxPID = (is_file(NginxPIDFile) ? file_get_contents(NginxPIDFile) : false);
} else {
$nginxPIDExec = exec('ps aux | grep nginx | grep master');
if (stripos($nginxPIDExec, 'ps aux') !== false || ($nginxPIDExecExplode = array_values(array_filter(explode(' ', $nginxPIDExec), function ($v) { if (empty($v)) { return false; } return true; }))) === false || count($nginxPIDExecExplode) < 2) {
$nginxPID = false;
} else {
$nginxPID = $nginxPIDExecExplode[1];
}
}
if ($nginxPID === false || !is_numeric($nginxPID)) {
LogStr("获取 Nginx PID 失败 ({$getMethod})", -1);
return -1;
}
$nginxPID = (int)$nginxPID;
LogStr("获取 Nginx PID 成功 ({$getMethod}, PID: {$nginxPID})");
return $nginxPID;
}
function CleanNginx(bool $manual = false) : bool {
global $lastCleanTime;
if (!$manual && ((time() - $lastCleanTime < 24) || is_file(NginxAccessLogFile))) {
return false;
}
$lastCleanTime = time();
$nginxPID = GetNginxPID();
if ($nginxPID === -1) {
$nginxPID = GetNginxPID(false);
if ($nginxPID === -1) {
return false;
}
}
if (posix_kill($nginxPID, SIGUSR1)) {
LogStr('重读 Nginx 日志成功');
return true;
}
return false;
}
if (!is_dir(LogDir) && !mkdir(LogDir)) {
LogStr('日志目录创建失败', -1, false);
die();
}
LogStr('脚本开始运行..');
mysqli_report(MYSQLI_REPORT_OFF);
$db = null;
$cache = null;
$lastHour21 = 0;
$lastHour22 = 0;
$lastHour3 = 0;
$lastDate1 = null;
#$lastDate2 = null;
$lastCleanTime = 0;
#$curNginxTimestampDone = 0;
while (true) {
$curMonth = intval(date('n'));
$curDay = intval(date('j'));
$curHour = intval(date('h'));
#$curHour2 = intval(date('H'));
$curMinute = intval(date('i'));
/*
if ($lastDate2 !== "{$curDay}-{$curHour2}" && $curHour2 === 8) {
$restrictResult = RestrictChatMember(1473575912);
if ($restrictResult !== false) {
$lastDate2 = "{$curDay}-{$curHour2}";
}
LogStr("已尝试自动限制 Bot, 返回信息: {$restrictResult}", (($restrictResult !== false) ? 0 : -1));
}
*/
$cleanRule1 = ($lastDate1 !== "{$curMonth}-{$curDay}" && $curDay === 1 && $curHour < 2); // 完成数统计. (每月 1 号执行)
#$cleanRule21 = ($lastHour21 !== $curHour && $curMinute >= 15 && $curMinute <= 30); // 每小时执行 1 次.
$cleanRule21 = false;
$cleanRule22 = ($lastHour22 !== $curHour && $curMinute >= 45 && $curMinute <= 60); // 每小时执行 1 次.
$cleanRule3 = ($lastHour3 !== $curHour && $curMinute >= 40 && $curMinute <= 45); // 每小时执行 1 次.
if ($cleanRule1 || $cleanRule21 || $cleanRule22 || $cleanRule3) {
$curTime = time();
$curYear = intval(date('Y'));
if ($cleanRule1) {
$connectDBStatus = ConnectDB();
if ($connectDBStatus < 0) {
LogStr('cleanRule-1: 数据库连接失败', -1);
sleep(DBRetryWaitTime);
continue;
} else if ($connectDBStatus === 0) {
LogStr('cleanRule-1: 忽略');
$lastDate1 = "{$curMonth}-{$curDay}"; // 跳过连接数据库后允许计时.
} else {
LogStr('cleanRule-1: 开始');
$lastDate1 = "{$curMonth}-{$curDay}"; // 成功连接数据库后允许计时.
$queryTimeStart1 = microtime(true);
$totalCompletedQuery = $db->query("SELECT SUM(total_completed) FROM Torrents LIMIT 1");
$totalCompleted = ($totalCompletedQuery !== false && ($totalCompletedResult = $totalCompletedQuery->fetch_row()) !== false && $totalCompletedResult !== null) ? intval($totalCompletedResult[0]) : 0;
$popularTorrentsQuery = $db->query('SELECT * FROM Torrents ORDER BY total_completed DESC LIMIT 50');
if ($popularTorrentsQuery !== false) {
$popularTorrentMessage = '';
while ($popularTorrentResult = $popularTorrentsQuery->fetch_assoc()) {
$popularTorrentMessage .= "种子 Hash: {$popularTorrentResult['info_hash']} (完成数: {$popularTorrentResult['total_completed']}).\n";
}
if ($curMonth === 1) {
$curYear--;
$curMonth = 12;
} else {
$curMonth = str_pad($curMonth - 1, 2, 0 ,STR_PAD_LEFT);
}
$startDate = "{$curYear}-{$curMonth}-01";
$endDate = date('Y-m-d', strtotime('-1 day'));
$popularTorrentMessage = "服务器 Tracker 已统计完成数记录 ({$startDate} 至 {$endDate}), 本次花费时间: " . round(microtime(true) - $queryTimeStart1, 3) . " 秒, 并将进行自动清理.\n本月共计完成数: {$totalCompleted}.\n\n{$popularTorrentMessage}";
if (($sendMessageRetCode = SendMessage($popularTorrentMessage)) === false) {
$lastDate1 = null;
LogStr('cleanRule-1: 发送消息失败, 将不清理 MySQL Torrents 表 (返回值: ' . var_export($sendMessageRetCode, true) . ')', -1);
} else {
$cleanTimeStart1 = microtime(true);
if ($db->query('TRUNCATE TABLE Torrents') !== false) {
LogStr('cleanRule-1: 清理 MySQL Torrents 表成功, 本次花费时间: ' . round(microtime(true) - $cleanTimeStart1, 3) . ' 秒');
}
}
}
}
}
if ($cleanRule21 || $cleanRule22) {
# 生成首页统计缓存.
if (!ConnectCache()) {
LogStr('cleanRule-2: 缓存连接失败', -1);
sleep(CacheRetryWaitTime);
continue;
}
LogStr('cleanRule-2: 开始');
// 成功连接缓存后允许计时.
if ($cleanRule21) {
$lastHour21 = $curHour;
}
if ($cleanRule22) {
$lastHour22 = $curHour;
}
$queryTimeStart2_0 = microtime(true);
$indexOutput = '';
$totalSeeder = 0;
$totalLeecher = 0;
#$totalUnknown = 0;
$totalIPv6 = 0;
$userAgentUsageList = array();
$torrentList = array(0 => array(), 1 => array(), 2 => array());
$peerIDList = array(0 => array(), 1 => array(), 2 => array()); // 完整 Peer ID 去重, 用于统计实际用户数.
$cache->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$peerInfoHashKeyIterator = null;
$queryTimeStart2_0_p = 0;
do {
$peerInfoHashKeyList = $cache->scan($peerInfoHashKeyIterator, "IH:*", IndexCount);
if ($peerInfoHashKeyList !== false) {
#$peerMulti = $cache->multi();
foreach ($peerInfoHashKeyList as $peerInfoHashKey) {
if ($peerInfoHashKey === false || empty($peerInfoHashKey)) {
continue;
}
$peerInfoHashPart = explode(':', $peerInfoHashKey, 2);
$peerInfoHash = $peerInfoHashPart[1];
if (isset($torrentList[0][$peerInfoHash]) || isset($torrentList[1][$peerInfoHash]) || isset($torrentList[2][$peerInfoHash])) {
continue;
}
$cache->zRemRangeByScore($peerInfoHashKey, 0, $curTime - AnnounceMaxInterval);
$peerInfoHashIDList = $cache->zRange($peerInfoHashKey, 0, -1);
foreach ($peerInfoHashIDList as $peerInfoHashID) {
$peerTypeAndEvent = $cache->get("IP:{$peerInfoHash}+{$peerInfoHashID}:TE");
if ($peerTypeAndEvent === false || empty($peerTypeAndEvent)) {
continue;
}
list($peerType, $peerEvent) = explode(':', $peerTypeAndEvent, 2);
$peerType = intval($peerType);
if ($peerType === 2) {
$totalSeeder++;
} else if ($peerType === 1) {
$totalLeecher++;
} else {
continue;
}
$peerUA = $cache->get("PI:UA:{$peerInfoHashID}");
$isIPv6 = ($cache->zCard("PI:A6:{$peerInfoHashID}") > 0 ? true : false);
if ($peerUA === false || strlen($peerUA) < 4 || stripos($peerUA, 'curl/') === 0 || stripos($peerUA, 'Mozilla/') === 0 || strlen($peerUA) > 50) {
$peerUA = substr($peerInfoHashID, 0, 8);
} else {
$uaExplode = explode(' ', $peerUA);
foreach ($uaExplode as $ua) {
if (preg_match('/((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}/', $ua) === 1) {
$peerUA = substr($peerInfoHashID, 0, 8);
break;
}
}
}
if (!isset($userAgentUsageList[$peerUA])) {
$userAgentUsageList[$peerUA] = array(0 => 0, 1 => 0, 2 => 0, 'ipv6' => 0);
}
if (!isset($peerIDList[$peerType][$peerInfoHashID])) {
$peerIDList[$peerType][$peerInfoHashID] = 0;
}
if (!isset($torrentList[$peerType][$peerInfoHash]) || ($isIPv6 && $torrentList[$peerType][$peerInfoHash] === 0)) {
$torrentList[$peerType][$peerInfoHash] = ($isIPv6 ? 1 : 0);
}
if ($isIPv6) {
$totalIPv6++;
$userAgentUsageList[$peerUA]['ipv6'] += 1;
if ($peerIDList[$peerType][$peerInfoHashID] === 0) {
$peerIDList[$peerType][$peerInfoHashID] = 1;
}
}
$userAgentUsageList[$peerUA][$peerType] += 1;
}
}
}
$queryTimeStart2_0_p++;
usleep(IndexSleepTime_Scan);
} while ($peerInfoHashKeyIterator > 0);
CloseCache();
$queryTimeStart2_1 = microtime(true);
$realUser = 0;
$realSeeder = 0;
$realLeecher = 0;
#$realUnknown = 0;
$realIPv6 = 0;
$realUserDupeArr = array();
$realTorrent = 0;
$realTorrentIPv6 = 0;
$realTorrentSeeder = 0;
$realTorrentLeecher = 0;
#$realTorrentUnknown = 0;
$realTorrentDupeArr = array();
/*
foreach ($peerIDList[0] as $unknownPeerID => $unknownIPv6) {
if (!isset($peerIDList[1][$unknownPeerID]) && !isset($peerIDList[2][$unknownPeerID])) {
$realUser++;
if ($unknownIPv6 === 1) {
$realIPv6++;
}
} else if (!in_array($unknownPeerID, $realUserDupeArr)) {
$realUserDupeArr[$unknownPeerID] = $unknownIPv6;
}
$realUnknown++;
}
*/
$peerCount2_1 = 0;
$queryTimeStart2_10_p = 0;
foreach ($peerIDList[1] as $leecherPeerID => $leecherIPv6) {
$peerCount2_1++;
if ($peerCount2_1 >= IndexCount) {
$peerCount2_1 = 0;
$queryTimeStart2_10_p++;
usleep(IndexSleepTime);
}
if (!isset($peerIDList[0][$leecherPeerID]) && !isset($peerIDList[2][$leecherPeerID])) {
$realUser++;
if ($leecherIPv6 === 1) {
$realIPv6++;
}
} else if (!in_array($leecherPeerID, $realUserDupeArr)) {
$realUserDupeArr[$leecherPeerID] = $leecherIPv6;
}
$realLeecher++;
}
$peerCount2_1 = 0;
$queryTimeStart2_11_p = 0;
foreach ($peerIDList[2] as $seederPeerID => $seederIPv6) {
$peerCount2_1++;
if ($peerCount2_1 >= IndexCount) {
$peerCount2_1 = 0;
$queryTimeStart2_11_p++;
usleep(IndexSleepTime);
}
if (!isset($peerIDList[1][$seederPeerID]) && !isset($peerIDList[0][$seederPeerID])) {
$realUser++;
if ($seederIPv6 === 1) {
$realIPv6++;
}
} else if (!in_array($seederPeerID, $realUserDupeArr)) {
$realUserDupeArr[$seederPeerID] = $seederIPv6;
}
$realSeeder++;
}
unset($peerIDList);
$realUser += count($realUserDupeArr);
$realIPv6 += count(array_filter($realUserDupeArr, function ($v) { return ($v === 1); }));
unset($realUserDupeArr);
/*
foreach ($torrentList[0] as $unknownTorrent => $unknownTorrentIPv6) {
if (!isset($torrentList[1][$unknownTorrent]) && !isset($torrentList[2][$unknownTorrent])) {
$realTorrent++;
if ($unknownTorrentIPv6 === 1) {
$realTorrentIPv6++;
}
} else if (!in_array($unknownTorrent, $realTorrentDupeArr)) {
$realTorrentDupeArr[$unknownTorrent] = $unknownTorrentIPv6;
}
$realTorrentUnknown++;
}
*/
$peerCount2_1 = 0;
$queryTimeStart2_12_p = 0;
foreach ($torrentList[1] as $leecherTorrent => $leecherTorrentIPv6) {
$peerCount2_1++;
if ($peerCount2_1 >= IndexCount) {
$peerCount2_1 = 0;
$queryTimeStart2_12_p++;
usleep(IndexSleepTime);
}
if (!isset($torrentList[0][$leecherTorrent]) && !isset($torrentList[2][$leecherTorrent])) {
$realTorrent++;
if ($leecherTorrentIPv6 === 1) {
$realTorrentIPv6++;
}
} else if (!in_array($leecherTorrent, $realTorrentDupeArr)) {
$realTorrentDupeArr[$leecherTorrent] = $leecherTorrentIPv6;
}
$realTorrentLeecher++;
}
$peerCount2_1 = 0;
$queryTimeStart2_13_p = 0;
foreach ($torrentList[2] as $seederTorrent => $seederTorrentIPv6) {
$peerCount2_1++;
if ($peerCount2_1 >= IndexCount) {
$peerCount2_1 = 0;
$queryTimeStart2_13_p++;
usleep(IndexSleepTime);
}
if (!isset($torrentList[1][$seederTorrent]) && !isset($torrentList[0][$seederTorrent])) {
$realTorrent++;
if ($seederTorrentIPv6 === 1) {
$realTorrentIPv6++;
}
} else if (!in_array($seederTorrent, $realTorrentDupeArr)) {
$realTorrentDupeArr[$seederTorrent] = $seederTorrentIPv6;
}
$realTorrentSeeder++;
}
unset($torrentList);
$realTorrent += count($realTorrentDupeArr);
$realTorrentIPv6 += count(array_filter($realTorrentDupeArr, function ($v) { return ($v === 1); }));
unset($realTorrentDupeArr);
$queryTimeStart2_2 = microtime(true);
$indexOutput .= "Tracker 状态信息 (近 1 小时) [缓存生成于 " . date('Y-m-d H:i:s', $curTime) . "]\n";
$indexOutput .= "种子数: {$realTorrent}. (IPv6 种子数: {$realTorrentIPv6}, 做种数: {$realTorrentSeeder}, 下载数: {$realTorrentLeecher})\n";
$indexOutput .= "用户数: " . ($totalSeeder + $totalLeecher) . ". (IPv6 用户数: {$totalIPv6}, 做种数: {$totalSeeder}, 下载数: {$totalLeecher})\n";
$indexOutput .= "实际用户人数: {$realUser}. (IPv6 实际用户人数: {$realIPv6}, 实际做种人数: {$realSeeder}, 实际下载人数: {$realLeecher})\n";
array_multisort(array_map(function ($v) { unset($v['ipv6']); return array_sum($v); }, $userAgentUsageList), SORT_DESC, $userAgentUsageList);
foreach ($userAgentUsageList as $userAgent => $numOfUser) {
$totalUserCount = $numOfUser[2] + $numOfUser[1] + $numOfUser[0];
if ($totalUserCount < 5) {
// 降序排序结果可使用 break 直接跳出整个循环以提升性能.
break;
}
$userAgent = htmlspecialchars($userAgent, ENT_NOQUOTES, 'UTF-8');
$indexOutput .= "客户端 {$userAgent} 的用户数: {$totalUserCount}. (IPv6 用户数: {$numOfUser['ipv6']}, 做种数: {$numOfUser[2]}, 下载数: {$numOfUser[1]})\n";
}
unset($userAgentUsageList);
$queryTimeStart2_3 = microtime(true);
file_put_contents('indexCache-Redis.txt', $indexOutput, LOCK_EX);
@chmod('indexCache-Redis.txt', 0777);
$queryTimeEnd2 = microtime(true);
$queryTimeStart2_0_ps = $queryTimeStart2_0_p * (IndexSleepTime_Scan / 1000000);
$queryTimeStart2_10_ps = $queryTimeStart2_10_p * (IndexSleepTime / 1000000);
$queryTimeStart2_11_ps = $queryTimeStart2_11_p * (IndexSleepTime / 1000000);
$queryTimeStart2_12_ps = $queryTimeStart2_12_p * (IndexSleepTime / 1000000);
$queryTimeStart2_13_ps = $queryTimeStart2_13_p * (IndexSleepTime / 1000000);
$queryTimeStart2_1x_pt = $queryTimeStart2_10_p + $queryTimeStart2_11_p + $queryTimeStart2_12_p + $queryTimeStart2_13_p;
$queryTimeStart2_1x_pst = $queryTimeStart2_10_ps + $queryTimeStart2_11_ps + $queryTimeStart2_12_ps + $queryTimeStart2_13_ps;
LogStr("cleanRule-2: 生成首页统计缓存成功, 本次花费时间: 总共/" . round($queryTimeEnd2 - $queryTimeStart2_0, 3) . " 秒, 实际/" . round($queryTimeEnd2 - $queryTimeStart2_0 - $queryTimeStart2_0_ps - $queryTimeStart2_1x_pst, 3) . " 秒, 一阶段 (数据库获取/数组分配)/" . round($queryTimeStart2_1 - $queryTimeStart2_0, 3) . "秒 (暂停/{$queryTimeStart2_0_p} 次, {$queryTimeStart2_0_ps} 秒), 二阶段 (实际用户分析)/" . round($queryTimeStart2_2 - $queryTimeStart2_1, 3) . " 秒 (暂停/{$queryTimeStart2_10_p} + {$queryTimeStart2_11_p} + {$queryTimeStart2_12_p} + {$queryTimeStart2_13_p} = {$queryTimeStart2_1x_pt} 次, {$queryTimeStart2_10_ps} + {$queryTimeStart2_11_ps} + {$queryTimeStart2_12_ps} + {$queryTimeStart2_13_ps} = {$queryTimeStart2_1x_pst} 秒), 三阶段 (User-Agent 用户数求和)/" . round($queryTimeEnd2 - $queryTimeStart2_2, 3) . " 秒");
}
if ($cleanRule3) {
LogStr('cleanRule-3: 开始');
$lastHour3 = $curHour; // 成功连接数据库后允许计时.
# 清理 Nginx 日志和数据库.
//if (($curNginxTimestampDone + 3600) < microtime(true)) {
$curNginxTimestampDone = microtime(true);
if (is_file(NginxAccessLogFile . '.tmp')) {
system('rm -f ' . NginxAccessLogFile . '.tmp ' . NginxAccessLogFile . '-*');
}
if (is_file(NginxAccessLogFile) && system('mv ' . NginxAccessLogFile . ' '. NginxAccessLogFile . '.tmp') !== false) {
LogStr('cleanRule-3: 成功移动 Nginx 日志至临时');
}
LogStr('cleanRule-3: 清理 Nginx 日志成功, 本次花费时间: ' . round(microtime(true) - $curNginxTimestampDone, 3) . ' 秒');
CleanNginx(true);
}
CloseDB();
}
CleanNginx();
sleep(CheckInterval);
}
?>