-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support windows report with proxy (#149)
- Loading branch information
1 parent
aed5b58
commit 3cd0463
Showing
2 changed files
with
24 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,40 @@ | ||
import rp from 'request-promise'; | ||
import URI from './config'; | ||
import shell from 'shelljs'; | ||
|
||
export default { | ||
retryCount: 0, | ||
log: {}, | ||
report(param, log) { | ||
report(param, log, needProxy = true) { | ||
this.log = log || {}; | ||
const proxyIsOn = shell | ||
.exec(`curl ${URI.REPORT_PROXY}`, { silent: true }) | ||
.stdout.trim(); | ||
|
||
if (proxyIsOn) { | ||
log.debug('Report, chose proxy curl'); | ||
this.reportWithProxy(param); | ||
} else { | ||
log.debug('Report, chose normal http'); | ||
this.reportWithoutProxy(param); | ||
} | ||
}, | ||
reportWithoutProxy(param) { | ||
rp({ | ||
const rpOption: any = { | ||
method: 'POST', | ||
uri: URI.REPORT_URL, | ||
body: param, | ||
json: true | ||
}) | ||
json: true, | ||
timeout: 600, | ||
}; | ||
|
||
if (needProxy) { | ||
this.log.debug('report with proxy'); | ||
rpOption.proxy = URI.REPORT_PROXY; | ||
} | ||
|
||
rp(rpOption) | ||
.then(res => { | ||
this.log.debug('got report response', res); | ||
}) | ||
.catch(e => { | ||
this.log.debug('feflow report fail', e); | ||
this.log.debug('feflow report fail', e.message); | ||
if (/ETIMEDOUT/.test(e.message || '')) { | ||
if (this.retryCount > 2) return; | ||
// timeout retry | ||
this.log.debug('feflow report 超时重试', this.retryCount); | ||
// 3nd rp request, remove rp proxy | ||
const needProxy = this.retryCount != 2; | ||
this.retryCount++; | ||
this.report(Object.assign({}, param), this.log, needProxy); | ||
} | ||
}); | ||
}, | ||
reportWithProxy(param) { | ||
let dataString = ''; | ||
// get formated report params | ||
Object.keys(param).forEach(key => { | ||
let value = param[key]; | ||
value = typeof value == 'object' ? JSON.stringify(value) : value; | ||
if (value != undefined) { | ||
dataString += `&${key}=${value}`; | ||
} | ||
}); | ||
|
||
const cmdWithProxy = []; | ||
|
||
// curl | ||
cmdWithProxy.push('curl'); | ||
|
||
// proxy | ||
cmdWithProxy.push('-x'); | ||
cmdWithProxy.push(URI.REPORT_PROXY); | ||
|
||
// url | ||
cmdWithProxy.push(URI.REPORT_URL); | ||
|
||
// params | ||
cmdWithProxy.push('-d'); | ||
cmdWithProxy.push("'" + dataString.slice(1) + "'"); | ||
|
||
this.log.debug("cmdWithProxy.join(' ')", cmdWithProxy.join(' ')); | ||
|
||
const res = shell | ||
.exec(cmdWithProxy.join(' '), { silent: true }) | ||
.stdout.trim(); | ||
|
||
this.log.debug('res', res); | ||
} | ||
}; |