...
|
...
|
@@ -10,6 +10,7 @@ import Model from './model'; |
|
|
import ssh from 'ssh2';
|
|
|
import InfluxDB from '../logger/influxdb';
|
|
|
import ws from '../../lib/ws';
|
|
|
import md5 from 'md5';
|
|
|
|
|
|
class PageCahe extends Model {
|
|
|
|
...
|
...
|
@@ -36,20 +37,21 @@ class PageCahe extends Model { |
|
|
let queryUri = queryUriList[i];
|
|
|
|
|
|
self._broadcast(`规则${i+1}:${queryUri}`);
|
|
|
queryUri = self.replaceUrl(queryUri);
|
|
|
let sql = `select FIRST(cache_status) from ${storeTableName} where full_path =~ /${queryUri}.*/ group by "key"`;
|
|
|
let result = await InfluxDB.query(sql);
|
|
|
if (!result.length) {
|
|
|
self._broadcast(`未查询到匹配的缓存信息`);
|
|
|
continue;
|
|
|
}
|
|
|
let keys = [md5(queryUri)];
|
|
|
// queryUri = self.replaceUrl(queryUri);
|
|
|
// let sql = `select FIRST(cache_status) from ${storeTableName} where full_path =~ /${queryUri}.*/ group by "key"`;
|
|
|
// let result = await InfluxDB.query(sql);
|
|
|
// if (!result.length) {
|
|
|
// self._broadcast(`未查询到匹配的缓存信息`);
|
|
|
// continue;
|
|
|
// }
|
|
|
let servers = await self.findAll();
|
|
|
|
|
|
self._broadcast(`共${servers.length}台nginx准备清理..`)
|
|
|
for (let i = 0; i < servers.length; i++) {
|
|
|
try {
|
|
|
self._broadcast(`第${i+1}台nginx准备清理:`)
|
|
|
await self._removeCache(servers[i], result[0], storeTableName);
|
|
|
await self._removeCache(servers[i], keys, storeTableName);
|
|
|
} catch (err) {
|
|
|
self._broadcast(`错误信息:${err}`)
|
|
|
}
|
...
|
...
|
@@ -103,7 +105,8 @@ class PageCahe extends Model { |
|
|
for (var i = blockIndex; i < block; i++) {
|
|
|
self._broadcast(`正在执行清理,进度:${i+1}/${block}`);
|
|
|
blockIndex = i;
|
|
|
let items = keys.slice(i * limit, i * limit + 2);
|
|
|
// let items = keys.slice(i * limit, i * limit + 2);
|
|
|
let items = keys;
|
|
|
let script = self._joinRemoveScript(server, items, storeTableName);
|
|
|
await self._evalScript(conn, script);
|
|
|
}
|
...
|
...
|
@@ -163,14 +166,14 @@ class PageCahe extends Model { |
|
|
return new Promise((resolve, reject) => {
|
|
|
//检查shell脚本的危险操作
|
|
|
if (self._checkDangerScript(script)) {
|
|
|
console.log(script)
|
|
|
let result = conn.exec(script, (err, stream) => {
|
|
|
if (!err) {
|
|
|
resolve();
|
|
|
} else {
|
|
|
self._broadcast('执行错误:${err}');
|
|
|
}
|
|
|
});
|
|
|
self._broadcast(script);
|
|
|
// let result = conn.exec(script, (err, stream) => {
|
|
|
// if (!err) {
|
|
|
// resolve();
|
|
|
// } else {
|
|
|
// self._broadcast('执行错误:${err}');
|
|
|
// }
|
|
|
// });
|
|
|
} else {
|
|
|
self._broadcast('检测到危险操作');
|
|
|
reject('检测到危险操作');
|
...
|
...
|
@@ -187,9 +190,11 @@ class PageCahe extends Model { |
|
|
_joinRemoveScript(server, keys, storeTableName) {
|
|
|
let script = 'rm';
|
|
|
keys.forEach((key) => {
|
|
|
let level1 = key.key.substr(key.key.length - 1, 1);
|
|
|
let level2 = key.key.substr(key.key.length - 3, 2);
|
|
|
script += ` ${server.cachepath}/${storeTableName}/${level1}/${level2}/${key.key}`;
|
|
|
// let level1 = key.key.substr(key.key.length - 1, 1);
|
|
|
// let level2 = key.key.substr(key.key.length - 3, 2);
|
|
|
let level1 = key.substr(key.length - 1, 1);
|
|
|
let level2 = key.substr(key.length - 3, 2);
|
|
|
script += ` ${server.cachepath}/${storeTableName}/${level1}/${level2}/${key}`;
|
|
|
});
|
|
|
return script;
|
|
|
}
|
...
|
...
|
@@ -202,13 +207,21 @@ class PageCahe extends Model { |
|
|
async init() {
|
|
|
let count = await this.count({});
|
|
|
if (count === 0) {
|
|
|
// await this.insert({
|
|
|
// host: '127.0.0.1',
|
|
|
// username: 'chenfeng',
|
|
|
// password: '1',
|
|
|
// port: 22,
|
|
|
// tag: 'nginx2',
|
|
|
// cachepath: '/usr/local/nginx'
|
|
|
// });
|
|
|
await this.insert({
|
|
|
host: '10.66.1.2',
|
|
|
username: 'node',
|
|
|
password: 'yoho9646',
|
|
|
port: 22,
|
|
|
tag: 'nginx2',
|
|
|
cachepath: '/usr/local/nginx'
|
|
|
cachepath: '/usr/local/openresty/nginx'
|
|
|
});
|
|
|
await this.insert({
|
|
|
host: '10.66.1.3',
|
...
|
...
|
@@ -216,7 +229,7 @@ class PageCahe extends Model { |
|
|
password: 'yoho9646',
|
|
|
port: 22,
|
|
|
tag: 'nginx3',
|
|
|
cachepath: '/usr/local/nginx'
|
|
|
cachepath: '/usr/local/openresty/nginx'
|
|
|
});
|
|
|
await this.insert({
|
|
|
host: '10.66.1.15',
|
...
|
...
|
@@ -224,7 +237,7 @@ class PageCahe extends Model { |
|
|
password: 'yoho9646',
|
|
|
port: 22,
|
|
|
tag: 'nginx15',
|
|
|
cachepath: '/usr/local/nginx'
|
|
|
cachepath: '/usr/local/openresty/nginx'
|
|
|
});
|
|
|
await this.insert({
|
|
|
host: '10.66.1.84',
|
...
|
...
|
@@ -232,7 +245,7 @@ class PageCahe extends Model { |
|
|
password: 'yoho9646',
|
|
|
port: 22,
|
|
|
tag: 'nginx84',
|
|
|
cachepath: '/usr/local/nginx'
|
|
|
cachepath: '/usr/local/openresty/nginx'
|
|
|
});
|
|
|
await this.insert({
|
|
|
host: '10.66.1.97',
|
...
|
...
|
@@ -240,7 +253,7 @@ class PageCahe extends Model { |
|
|
password: 'yoho9646',
|
|
|
port: 22,
|
|
|
tag: 'nginx97',
|
|
|
cachepath: '/usr/local/nginx'
|
|
|
cachepath: '/usr/local/openresty/nginx'
|
|
|
});
|
|
|
}
|
|
|
}
|
...
|
...
|
|