Authored by 陈峰

cache清理开发完成

... ... @@ -20,24 +20,27 @@ class PageCahe extends Model {
async removeCache(keys, storeTableName) {
let self = this;
let servers = await self.findAll();
for (var i = 0; i < servers.length; i++) {
for (let i = 0; i < servers.length; i++) {
try {
await self._removeAllNginxCache(servers[i], keys, storeTableName);
await self._removeCache(servers[i], keys, storeTableName);
} catch (err) {
console.log(err)
}
}
console.log('over')
}
async removeAllCache(storeTableName) {
let self = this;
let servers = await self.findAll();
for (var i = 0; i < servers.length; i++) {
try {
for (let i = 0; i < servers.length; i++) {
let server = servers[i];
try {
//验证两个变量是否为空避免特殊bug造成为空后拼接的shell执行危险操作
if (server.cachepath && storeTableName) {
await self._connStart(async (conn) => {
let scirpt = `rm -rf ${server.cachepath}/${storeTableName}`;
await self._evalScript(conn, scirpt);
//连接ssh后执行操作
return await self._connStart(async (conn) => {
let script = `rm -rf ${server.cachepath}/${storeTableName}`;
return await self._evalScript(conn, script);
}, server);
}
} catch (err) {
... ... @@ -45,24 +48,26 @@ class PageCahe extends Model {
}
}
}
async _removeAllNginxCache(server, keys, storeTableName) {
async _removeCache(server, keys, storeTableName) {
let self = this;
let limit = 2;
let block = parseInt(keys.length / limit, 10) + (keys.length % limit ? 1 : 0);
let blockIndex = 0;
//连接ssh后执行操作
await self._connStart(async (conn) => {
for (var i = blockIndex; i < block; i++) {
blockIndex = i;
let items = keys.slice(i * limit, i * limit + 2);
let scirpt = self._joinRemoveScript(server, items, storeTableName);
self._evalScript(conn, scirpt);
let script = self._joinRemoveScript(server, items, storeTableName);
await self._evalScript(conn, script);
}
}, server)
}
_checkDangerScirpt(scirpt) {
if (scirpt.test(/rm -rf \/($| )/)) {
_checkDangerScript(script) {
if (/rm -rf \/($| )/.test(script)) {
return false;
}
return true;
}
_connStart(callback, server) {
//ssh掉线重连
... ... @@ -70,6 +75,7 @@ class PageCahe extends Model {
let connStart = (err) => {
let conn = new ssh.Client();
conn.on('ready', async() => {
console.log(`connect ${server.host}`);
await callback(conn)
resolve();
}).on('error', () => {
... ... @@ -90,19 +96,23 @@ class PageCahe extends Model {
connStart(0, 0);
});
}
_evalScript(conn, scirpt) {
_evalScript(conn, script) {
let self = this;
return new Promise((resolve) => {
console.log(scirpt)
return new Promise((resolve, reject) => {
//检查shell脚本的危险操作
if (self._checkDangerScript(script)) {
console.log('_evalScript start');
resolve();
// let result = conn.exec(scirpt, (err, stream) => {
// let result = conn.exec(script, (err, stream) => {
// if (!err) {
// resolve();
// }
// });
})
} else {
reject('检测到危险操作');
}
})
}
_joinRemoveScript(server, keys, storeTableName) {
let script = 'rm';
... ...
... ... @@ -30,19 +30,22 @@ const pageCahe = {
await ctx.render('action/page_cache', data);
},
async clear(ctx) {
let queryUri = ctx.request.body.query_uri;
let queryUris = ctx.request.body.query_uri;
let storeTableName = ctx.request.body.table_name;
if (queryUri && storeTableName) {
if (queryUris && storeTableName) {
let queryUriList = queryUris.split('\n');
queryUriList.forEach(async queryUri => {
queryUri = PageCache.replaceUrl(queryUri);
let sql = `select FIRST(cache_status) from ${storeTableName} where full_path =~ /${queryUri}.*/ group by "key"`;
let result = await InfluxDB.query(sql);
PageCache.removeCache(result[0], storeTableName);
})
}
},
async clearAll(ctx) {
let storeTableName = ctx.request.body.table_name;
if (storeTableName) {
PageCache.removeAllCache(storeTableName);
await PageCache.removeAllCache(storeTableName);
}
}
}
... ...
... ... @@ -23,7 +23,7 @@
</div>
<div class="query">
<textarea name="" id="uri" cols="30" rows="20" class="form-control" placeholder="支持多条换行输入">http://m.yohobuy.com</textarea>
<textarea name="" id="uri" cols="30" rows="20" class="form-control" placeholder="支持多条换行输入"></textarea>
</div>
</div>
</div>
... ... @@ -33,7 +33,8 @@
<option value="{{tableName}}">{{name}}</option>
{{/each}}
</select>
<button class="btn btn-warning btn-clear">清除</button>
<button class="btn btn-warning btn-clear pull-left">清除</button>
<div class="result pull-left ml20"></div>
<button class="btn btn-danger btn-all-clear pull-right">全网刷新</button>
</div>
</div>
... ... @@ -76,7 +77,7 @@
query_uri: uri,
table_name: tableName
}, function(res) {
$('.result').text('执行成功');
});
}
})
... ... @@ -86,7 +87,7 @@
$.post('/page_cache/clear/all', {
table_name: tableName
}, function(res) {
$('.result').text('执行成功');
});
}
})
... ...