@@ -39,4 +39,42 @@ rpc.call<any>('withoutCB', {}, function (msg) {
39
39
console . log ( 'withoutCB results:' , msg ) ; //output: please run function without cb parameter
40
40
} ) ;
41
41
42
- rpc . call < any > ( 'withoutCB' , { } ) ; //output message on server side console
42
+ rpc . call < any > ( 'withoutCB' , { } ) ; //output message on server side console
43
+
44
+ import os = require( 'os' ) ;
45
+ interface State {
46
+ type : string ;
47
+ }
48
+
49
+ var counter = 0 ;
50
+ rpc . onBroadcast < State > ( 'getWorkerStat' , function ( params , cb ) {
51
+ if ( params && params . type == 'fullStat' ) {
52
+ cb ( null , {
53
+ pid : process . pid ,
54
+ hostname : os . hostname ( ) ,
55
+ uptime : process . uptime ( ) ,
56
+ counter : counter ++
57
+ } ) ;
58
+ }
59
+ else {
60
+ cb ( null , { counter : counter ++ } )
61
+ }
62
+ } ) ;
63
+
64
+ var all_stats : any = { } ;
65
+ rpc . callBroadcast < State > (
66
+ 'getWorkerStat' ,
67
+ { type : 'fullStat' } , //request parameters
68
+ { //call options
69
+ ttl : 1000 , //wait response time (1 seconds), after run onComplete
70
+ onResponse : function ( err : any , stat : any ) { //callback on each worker response
71
+ all_stats [ stat . hostname + ':' + stat . pid ] = stat ;
72
+ } ,
73
+ onComplete : function ( ) { //callback on ttl expired
74
+ console . log ( '----------------------- WORKER STATISTICS ----------------------------------------' ) ;
75
+ for ( var worker in all_stats ) {
76
+ var s : any = all_stats [ worker ] ;
77
+ console . log ( worker , '\tuptime=' , s . uptime . toFixed ( 2 ) + ' seconds' , '\tcounter=' , s . counter ) ;
78
+ }
79
+ }
80
+ } ) ;
0 commit comments