33const BATCH_SIZE = 100 ;
44
55const Promise = require ( 'bluebird' ) ;
6- const request = Promise . promisify ( require ( 'request' ) ) ;
76const fs = Promise . promisifyAll ( require ( 'fs' ) ) ;
87const zlib = Promise . promisifyAll ( require ( 'zlib' ) ) ;
98const mkdirp = Promise . promisify ( require ( 'mkdirp' ) ) ;
10- const querystring = require ( 'querystring' ) ;
119const _ = require ( 'lodash' ) ;
1210const path = require ( 'path' ) ;
1311const co = require ( 'co' ) ;
1412const util = require ( 'util' ) ;
1513
1614const api = require ( '../lib/api' ) ;
1715const { loadClientData } = require ( '../lib/clientData' ) ;
16+ const { requestAndRetry } = require ( '../lib/retrier' ) ;
1817
1918const command = 'analyze [json..]' ;
2019const desc = 'Send analysis or job in each file to CEE, then optionally poll CEE until analysis is complete and retrieve results. If polling is enabled analysis results will be written to a directory specified by the output parameter.' ;
@@ -59,7 +58,7 @@ module.exports = {
5958 batchJobs ( argv ) . mapSeries ( batch =>
6059 zlib . gzipAsync ( JSON . stringify ( batch ) )
6160 . then ( jobData =>
62- request ( {
61+ requestAndRetry ( {
6362 url : config . server + '/job?apiToken=' + config . apiToken ,
6463 proxy : config . proxy ,
6564 gzip : true ,
@@ -71,15 +70,15 @@ module.exports = {
7170 'Content-Encoding' : 'gzip'
7271 } ,
7372 body : jobData
73+ } , ( response ) => {
74+ try {
75+ return JSON . parse ( response . body ) ;
76+ } catch ( e ) {
77+ console . log ( `Unable to parse response JSON: ${ response . body } ` ) ;
78+ throw e ;
79+ }
7480 } )
75- ) . then ( response => {
76- try {
77- return JSON . parse ( response . body ) ;
78- } catch ( e ) {
79- console . log ( `Unable to parse response JSON: ${ response . body } ` ) ;
80- return [ ] ;
81- }
82- } )
81+ )
8382 ) . then ( responses => {
8483 responses = _ . flatten ( responses ) ;
8584
@@ -103,7 +102,6 @@ module.exports = {
103102 console . log ( 'The following errors occured:' ) ;
104103 console . log ( util . inspect ( errors , { depth : null } ) ) ;
105104 }
106-
107105 return showProgress ( jobIds , argv , config ) . then ( ( ) => console . log ( 'Done.' ) ) ;
108106 }
109107 } )
@@ -113,7 +111,7 @@ module.exports = {
113111function pollFor ( jobIds , status , config , onUpdate ) {
114112 return co ( function * ( ) {
115113 while ( jobIds . length > 0 ) {
116- const response = yield request ( {
114+ const updatedJobIds = yield requestAndRetry ( {
117115 url : config . server + '/job/poll' ,
118116 proxy : config . proxy ,
119117 qs : { apiToken : config . apiToken , status, ids : JSON . stringify ( jobIds . slice ( 0 , BATCH_SIZE ) ) } ,
@@ -122,10 +120,15 @@ function pollFor(jobIds, status, config, onUpdate) {
122120 'User-Agent' : 'cee-cli' ,
123121 'Accept' : 'application/json' ,
124122 }
123+ } , ( response ) => {
124+ try {
125+ return JSON . parse ( response . body ) . map ( j => j . id )
126+ } catch ( e ) {
127+ console . log ( `Failed to poll for jobs ${ response . body } ` )
128+ throw e
129+ }
125130 } ) ;
126131
127- const updatedJobIds = JSON . parse ( response . body ) . map ( j => j . id ) ;
128-
129132 _ . pullAll ( jobIds , updatedJobIds ) ;
130133 const updateP = onUpdate ( updatedJobIds ) ;
131134 if ( updateP ) {
@@ -158,7 +161,7 @@ function showProgress(jobIds,argv,config) {
158161 displayProgress ( validCount , startedCount , finishedCount ) ;
159162 } ) ,
160163 pollFor ( unfinishedJobs , 'FINISHED' , config , jobIds =>
161- request ( {
164+ requestAndRetry ( {
162165 url : config . server + '/job' ,
163166 qs : {
164167 apiToken : config . apiToken ,
@@ -170,7 +173,7 @@ function showProgress(jobIds,argv,config) {
170173 'User-Agent' : 'cee-cli' ,
171174 'Accept' : 'application/json' ,
172175 }
173- } ) . then ( response =>
176+ } , ( response ) =>
174177 Promise . all (
175178 JSON . parse ( response . body ) . map ( result =>
176179 fs . writeFileAsync ( path . join ( output , result . externalId ) , JSON . stringify ( result ) )
@@ -358,3 +361,4 @@ function batchJobs(argv) {
358361
359362
360363
364+
0 commit comments