@@ -8,8 +8,10 @@ import { ProcessOutput } from './process';
88import * as envVars from './env-variables' ;
99import chalk from 'chalk' ;
1010import * as style from 'ansi-styles' ;
11+ import { platform } from 'os' ;
1112
12- export let docker = new dockerode ( ) ;
13+ export const docker = new dockerode ( ) ;
14+ const binds = platform ( ) === 'darwin' ? [ ] : [ '/var/run/docker.sock:/var/run/docker.sock' ] ;
1315
1416export function createContainer (
1517 name : string ,
@@ -24,7 +26,7 @@ export function createContainer(
2426 OpenStdin : true ,
2527 StdinOnce : false ,
2628 Env : envVars . serialize ( envs ) || [ ] ,
27- Binds : [ '/var/run/docker.sock:/var/run/docker.sock' ] ,
29+ Binds : binds ,
2830 Privileged : true ,
2931 ExposedPorts : {
3032 '22/tcp' : { } ,
@@ -290,37 +292,23 @@ export function calculateContainerStats(
290292 container : dockerode . ContainerInfo ,
291293 processes : any
292294) : Promise < any > {
293- return docker . getContainer ( container . Id ) . stats ( )
294- . then ( stream => {
295- let json = '' ;
296- return new Promise ( ( resolve , reject ) => {
297- stream . on ( 'data' , buf => {
298- let rawJson = json + buf . toString ( ) ;
299- try {
300- let data = JSON . parse ( rawJson ) ;
301-
302- if ( data && data . precpu_stats . system_cpu_usage ) {
303- let jobId = container . Names [ 0 ] . split ( '_' ) [ 2 ] || - 1 ;
304- let job = processes . find ( p => p . job_id === Number ( jobId ) ) ;
305- let debug = false ;
306- if ( job ) {
307- debug = job . debug || false ;
308- }
309-
310- let stats = {
311- id : container . Id ,
312- name : container . Names [ 0 ] . substr ( 1 ) || '' ,
313- debug : debug ,
314- data : data
315- } ;
295+ return docker . getContainer ( container . Id ) . stats ( { stream : false } )
296+ . then ( stats => {
297+ const data = stats ;
298+ if ( data && data . precpu_stats . system_cpu_usage ) {
299+ const jobId = container . Names [ 0 ] . split ( '_' ) [ 2 ] || - 1 ;
300+ const job = processes . find ( p => p . job_id === Number ( jobId ) ) ;
301+ const debug = job && job . debug || false ;
302+ const stats = {
303+ id : container . Id ,
304+ name : container . Names [ 0 ] . substr ( 1 ) || '' ,
305+ debug : debug ,
306+ data : data
307+ } ;
316308
317- stream . destroy ( ) ;
318- resolve ( stats ) ;
319- }
320- } catch ( e ) {
321- json = rawJson ;
322- }
323- } ) ;
324- } ) ;
309+ return stats ;
310+ } else {
311+ return null ;
312+ }
325313 } ) ;
326314}
0 commit comments