Authored by 陈峰

cache清理开发完成

@@ -20,24 +20,27 @@ class PageCahe extends Model { @@ -20,24 +20,27 @@ class PageCahe extends Model {
20 async removeCache(keys, storeTableName) { 20 async removeCache(keys, storeTableName) {
21 let self = this; 21 let self = this;
22 let servers = await self.findAll(); 22 let servers = await self.findAll();
23 - for (var i = 0; i < servers.length; i++) { 23 + for (let i = 0; i < servers.length; i++) {
24 try { 24 try {
25 - await self._removeAllNginxCache(servers[i], keys, storeTableName); 25 + await self._removeCache(servers[i], keys, storeTableName);
26 } catch (err) { 26 } catch (err) {
27 console.log(err) 27 console.log(err)
28 } 28 }
29 } 29 }
  30 + console.log('over')
30 } 31 }
31 async removeAllCache(storeTableName) { 32 async removeAllCache(storeTableName) {
32 let self = this; 33 let self = this;
33 let servers = await self.findAll(); 34 let servers = await self.findAll();
34 - for (var i = 0; i < servers.length; i++) { 35 + for (let i = 0; i < servers.length; i++) {
  36 + let server = servers[i];
35 try { 37 try {
36 - let server = servers[i]; 38 + //验证两个变量是否为空避免特殊bug造成为空后拼接的shell执行危险操作
37 if (server.cachepath && storeTableName) { 39 if (server.cachepath && storeTableName) {
38 - await self._connStart(async (conn) => {  
39 - let scirpt = `rm -rf ${server.cachepath}/${storeTableName}`;  
40 - await self._evalScript(conn, scirpt); 40 + //连接ssh后执行操作
  41 + return await self._connStart(async (conn) => {
  42 + let script = `rm -rf ${server.cachepath}/${storeTableName}`;
  43 + return await self._evalScript(conn, script);
41 }, server); 44 }, server);
42 } 45 }
43 } catch (err) { 46 } catch (err) {
@@ -45,24 +48,26 @@ class PageCahe extends Model { @@ -45,24 +48,26 @@ class PageCahe extends Model {
45 } 48 }
46 } 49 }
47 } 50 }
48 - async _removeAllNginxCache(server, keys, storeTableName) { 51 + async _removeCache(server, keys, storeTableName) {
49 let self = this; 52 let self = this;
50 let limit = 2; 53 let limit = 2;
51 let block = parseInt(keys.length / limit, 10) + (keys.length % limit ? 1 : 0); 54 let block = parseInt(keys.length / limit, 10) + (keys.length % limit ? 1 : 0);
52 let blockIndex = 0; 55 let blockIndex = 0;
  56 + //连接ssh后执行操作
53 await self._connStart(async (conn) => { 57 await self._connStart(async (conn) => {
54 for (var i = blockIndex; i < block; i++) { 58 for (var i = blockIndex; i < block; i++) {
55 blockIndex = i; 59 blockIndex = i;
56 let items = keys.slice(i * limit, i * limit + 2); 60 let items = keys.slice(i * limit, i * limit + 2);
57 - let scirpt = self._joinRemoveScript(server, items, storeTableName);  
58 - self._evalScript(conn, scirpt); 61 + let script = self._joinRemoveScript(server, items, storeTableName);
  62 + await self._evalScript(conn, script);
59 } 63 }
60 }, server) 64 }, server)
61 } 65 }
62 - _checkDangerScirpt(scirpt) {  
63 - if (scirpt.test(/rm -rf \/($| )/)) { 66 + _checkDangerScript(script) {
  67 + if (/rm -rf \/($| )/.test(script)) {
64 return false; 68 return false;
65 } 69 }
  70 + return true;
66 } 71 }
67 _connStart(callback, server) { 72 _connStart(callback, server) {
68 //ssh掉线重连 73 //ssh掉线重连
@@ -70,6 +75,7 @@ class PageCahe extends Model { @@ -70,6 +75,7 @@ class PageCahe extends Model {
70 let connStart = (err) => { 75 let connStart = (err) => {
71 let conn = new ssh.Client(); 76 let conn = new ssh.Client();
72 conn.on('ready', async() => { 77 conn.on('ready', async() => {
  78 + console.log(`connect ${server.host}`);
73 await callback(conn) 79 await callback(conn)
74 resolve(); 80 resolve();
75 }).on('error', () => { 81 }).on('error', () => {
@@ -90,19 +96,23 @@ class PageCahe extends Model { @@ -90,19 +96,23 @@ class PageCahe extends Model {
90 connStart(0, 0); 96 connStart(0, 0);
91 }); 97 });
92 } 98 }
93 - _evalScript(conn, scirpt) { 99 + _evalScript(conn, script) {
94 let self = this; 100 let self = this;
95 - return new Promise((resolve) => {  
96 - console.log(scirpt)  
97 - resolve();  
98 - // let result = conn.exec(scirpt, (err, stream) => {  
99 - // if (!err) {  
100 - // resolve();  
101 - // }  
102 - // });  
103 - }) 101 + return new Promise((resolve, reject) => {
  102 + //检查shell脚本的危险操作
  103 + if (self._checkDangerScript(script)) {
  104 + console.log('_evalScript start');
  105 + resolve();
  106 + // let result = conn.exec(script, (err, stream) => {
  107 + // if (!err) {
  108 + // resolve();
  109 + // }
  110 + // });
  111 + } else {
  112 + reject('检测到危险操作');
  113 + }
104 114
105 - 115 + })
106 } 116 }
107 _joinRemoveScript(server, keys, storeTableName) { 117 _joinRemoveScript(server, keys, storeTableName) {
108 let script = 'rm'; 118 let script = 'rm';
@@ -30,19 +30,22 @@ const pageCahe = { @@ -30,19 +30,22 @@ const pageCahe = {
30 await ctx.render('action/page_cache', data); 30 await ctx.render('action/page_cache', data);
31 }, 31 },
32 async clear(ctx) { 32 async clear(ctx) {
33 - let queryUri = ctx.request.body.query_uri; 33 + let queryUris = ctx.request.body.query_uri;
34 let storeTableName = ctx.request.body.table_name; 34 let storeTableName = ctx.request.body.table_name;
35 - if (queryUri && storeTableName) {  
36 - queryUri = PageCache.replaceUrl(queryUri);  
37 - let sql = `select FIRST(cache_status) from ${storeTableName} where full_path =~ /${queryUri}.*/ group by "key"`;  
38 - let result = await InfluxDB.query(sql);  
39 - PageCache.removeCache(result[0], storeTableName); 35 + if (queryUris && storeTableName) {
  36 + let queryUriList = queryUris.split('\n');
  37 + queryUriList.forEach(async queryUri => {
  38 + queryUri = PageCache.replaceUrl(queryUri);
  39 + let sql = `select FIRST(cache_status) from ${storeTableName} where full_path =~ /${queryUri}.*/ group by "key"`;
  40 + let result = await InfluxDB.query(sql);
  41 + PageCache.removeCache(result[0], storeTableName);
  42 + })
40 } 43 }
41 }, 44 },
42 async clearAll(ctx) { 45 async clearAll(ctx) {
43 let storeTableName = ctx.request.body.table_name; 46 let storeTableName = ctx.request.body.table_name;
44 if (storeTableName) { 47 if (storeTableName) {
45 - PageCache.removeAllCache(storeTableName); 48 + await PageCache.removeAllCache(storeTableName);
46 } 49 }
47 } 50 }
48 } 51 }
@@ -23,7 +23,7 @@ @@ -23,7 +23,7 @@
23 23
24 </div> 24 </div>
25 <div class="query"> 25 <div class="query">
26 - <textarea name="" id="uri" cols="30" rows="20" class="form-control" placeholder="支持多条换行输入">http://m.yohobuy.com</textarea> 26 + <textarea name="" id="uri" cols="30" rows="20" class="form-control" placeholder="支持多条换行输入"></textarea>
27 </div> 27 </div>
28 </div> 28 </div>
29 </div> 29 </div>
@@ -33,7 +33,8 @@ @@ -33,7 +33,8 @@
33 <option value="{{tableName}}">{{name}}</option> 33 <option value="{{tableName}}">{{name}}</option>
34 {{/each}} 34 {{/each}}
35 </select> 35 </select>
36 - <button class="btn btn-warning btn-clear">清除</button> 36 + <button class="btn btn-warning btn-clear pull-left">清除</button>
  37 + <div class="result pull-left ml20"></div>
37 <button class="btn btn-danger btn-all-clear pull-right">全网刷新</button> 38 <button class="btn btn-danger btn-all-clear pull-right">全网刷新</button>
38 </div> 39 </div>
39 </div> 40 </div>
@@ -76,7 +77,7 @@ @@ -76,7 +77,7 @@
76 query_uri: uri, 77 query_uri: uri,
77 table_name: tableName 78 table_name: tableName
78 }, function(res) { 79 }, function(res) {
79 - 80 + $('.result').text('执行成功');
80 }); 81 });
81 } 82 }
82 }) 83 })
@@ -86,7 +87,7 @@ @@ -86,7 +87,7 @@
86 $.post('/page_cache/clear/all', { 87 $.post('/page_cache/clear/all', {
87 table_name: tableName 88 table_name: tableName
88 }, function(res) { 89 }, function(res) {
89 - 90 + $('.result').text('执行成功');
90 }); 91 });
91 } 92 }
92 }) 93 })