// single process mode start({ baseDir: __dirname,ignoreWarning: true}).then(app=>{ const port= 1002; app.logger.info('123'); app.on('egg-ready', () => {}); app.emit('egg-ready'); app.getLogger('test').info('123'); app.inspect(); app.listen(port); app.logger.info(app.locals.test); const ctxHttpClient = new app.ContextHttpClient({} as Context); ctxHttpClient.request('http://127.0.0.1', { method: 'GET' }); const appHttpClient = new app.HttpClient(app); appHttpClient.request('http://127.0.0.1', { method: 'GET' }); app.httpclient.request('http://127.0.0.1', { method: 'GET' }).catch(() => {}); app.logger.info(app.Service); app.logger.info(app.Controller); app.controller.test().then(() => {}); });
// 获得本地文件缓存 async ipCityFileCache() { this.cacheIpJson = {}; if (!this.app.config.ip_city_cache_file.isuse) return {}; try { const beginTime = new Date().getTime(); const filepath = path.resolve(__dirname, `../../cache/${this.app.config.ip_city_cache_file.wx}`); const ipDatas = fs.readFileSync(filepath, { encoding: 'utf8' }); const result = JSON.parse(`{${ipDatas.slice(0, -1)}}`); this.cacheIpJson = result; this.app.logger.info(`--------读取文件城市Ip地址耗时为 ${new Date().getTime() - beginTime}ms-------`); } catch (err) { this.cacheIpJson = {}; } }
// 把db2的数据经过加工之后同步到db3中 的定时任务 async saveWxReportDatasForMongodb() { let beginTime = await this.app.redis.get('wx_task_begin_time'); const endTime = new Date(); const query = { create_time: { $lt: endTime } }; if (beginTime) { beginTime = new Date(new Date(beginTime).getTime() + 1000); query.create_time.$gt = beginTime; } /* * 请求db1数据库进行同步数据 * 查询db1是否正常,不正常则重启 */ try { const datas = await this.ctx.model.Wx.WxReport.find(query) .read('sp') .sort({ create_time: 1 }) .exec(); this.app.logger.info(`-----------db1--查询wx端db1数据库是否可用----${datas.length}------`); // 储存数据 this.commonSaveDatas(datas); } catch (err) { this.app.restartMongodbs('db1', this.ctx, err); } }
// 把db2的数据经过加工之后同步到db3中 的定时任务(从mongodb中拉取数据) async saveWebReportDatasForMongodb() { let beginTime = await this.app.redis.get('web_task_begin_time'); const endTime = new Date(); const query = { create_time: { $lt: endTime } }; if (beginTime) { beginTime = new Date(new Date(beginTime).getTime() + 1000); query.create_time.$gt = beginTime; } /* * 请求db1数据库进行同步数据 * 查询db1是否正常,不正常则重启 */ try { const datas = await this.ctx.model.Web.WebReport.find(query) .read('sp') .sort({ create_time: 1 }) .exec(); this.app.logger.info(`-----------db1--查询web端db1数据库是否可用---${datas.length}-------`); // 储存数据 this.commonSaveDatas(datas); } catch (err) { this.app.restartMongodbs('db1', this.ctx, err); } }
// 获得本地文件缓存 async ipCityFileCache() { this.cacheIpJson = {}; if (!this.app.config.ip_city_cache_file.isuse) return {}; try { const beginTime = new Date().getTime(); const filepath = path.resolve(__dirname, `../../cache/${this.app.config.ip_city_cache_file.web}`); const ipDatas = fs.readFileSync(filepath, { encoding: 'utf8' }); const result = JSON.parse(`{${ipDatas.slice(0, -1)}}`); this.cacheIpJson = result; this.app.logger.info(`--------读取文件城市Ip地址耗时为 ${new Date().getTime() - beginTime}ms-------`); } catch (err) { this.cacheIpJson = {}; } }
async connect() { const { ctx, app } = this; const nsp = app.io.of('/'); const message = ctx.args[0] || {}; const { socket } = ctx; const client = socket.id; try { const { target, payload } = message; if (!target) return; const msg = ctx.helper.parseMsg('connect', payload, { client, target }); nsp.emit(target, msg); } catch (error) { app.logger.error(error); } }
/* * kill应用 */ async killApp(appid) { const killRes = shell.exec('docker kill dapps-' + appid, { silent: true, }); this.app.logger.info( '[DockerService] [killApp] appid:, killRes:', appid, killRes ); if (killRes.code === 0) { return true; } return false; }
this.app.logger.info( '[DockerComposeService] [stop] stop stopRes:', stopRes
/* * network clean */ async networkClean() { try { const cleanRes = shell.exec('docker network prune -f'); this.app.logger.info( '[DockerService] [networkClean] cleanRes:', cleanRes ); if (cleanRes.code === 0) { return true; } return false; } catch (e) { this.app.logger.error('[DockerService] [networkClean]: error ', e); } return true; }
/* * 删除应用 */ async delApp(appid) { const delRes = shell.exec('docker rm dapps-' + appid, { silent: true, }); this.app.logger.info( '[DockerService] [delApp] appid:, delRes:', appid, delRes ); if (delRes.code === 0) { return true; } return false; }
/* * 删除网络 */ async delAppNetwork(appid) { const delRes = shell.exec('docker network rm ' + appid + '_default', { silent: true, }); this.app.logger.info( '[DockerService] [delAppNetwork] appid:, delRes:', appid, delRes ); if (delRes.code === 0) { return true; } return false; }
this.app.logger.info( '[DockerComposeService] [start] start startRes:', startRes