@@ -114,6 +114,39 @@ module.exports = function (RED) {
114
114
let cursor ;
115
115
let getNextRows ;
116
116
117
+ // Do not update status faster than x ms
118
+ const updateStatusPeriodMs = 1000 ;
119
+
120
+ let nbQueue = 0 ;
121
+ let hasError = false ;
122
+ let statusTimer = null ;
123
+ const updateStatus = ( incQueue = 0 , isError = false ) => {
124
+ nbQueue += incQueue ;
125
+ hasError |= isError ;
126
+ if ( ! statusTimer ) {
127
+ statusTimer = setTimeout ( ( ) => {
128
+ let fill = 'grey' ;
129
+ if ( hasError ) {
130
+ fill = 'red' ;
131
+ } else if ( nbQueue <= 0 ) {
132
+ fill = 'blue' ;
133
+ } else if ( nbQueue <= node . config . pgPool . totalCount ) {
134
+ fill = 'green' ;
135
+ } else {
136
+ fill = 'yellow' ;
137
+ }
138
+ node . status ( {
139
+ fill,
140
+ shape : hasError || nbQueue > node . config . pgPool . totalCount ? 'ring' : 'dot' ,
141
+ text : 'Queue: ' + nbQueue + ( hasError ? ' Error!' : '' ) ,
142
+ } ) ;
143
+ hasError = false ;
144
+ statusTimer = null ;
145
+ } , updateStatusPeriodMs ) ;
146
+ }
147
+ } ;
148
+ updateStatus ( 0 , false ) ;
149
+
117
150
node . on ( 'input' , async ( msg , send , done ) => {
118
151
// 'send' and 'done' require Node-RED 1.0+
119
152
send = send || function ( ) { node . send . apply ( node , arguments ) ; } ;
@@ -134,7 +167,7 @@ module.exports = function (RED) {
134
167
135
168
let client = null ;
136
169
137
- const handleDone = async ( ) => {
170
+ const handleDone = async ( isError = false ) => {
138
171
if ( cursor ) {
139
172
cursor . close ( ) ;
140
173
cursor = null ;
@@ -146,13 +179,16 @@ module.exports = function (RED) {
146
179
await client . end ( ) ;
147
180
}
148
181
client = null ;
182
+ updateStatus ( - 1 , isError ) ;
183
+ } else if ( isError ) {
184
+ updateStatus ( - 1 , isError ) ;
149
185
}
150
186
getNextRows = null ;
151
187
} ;
152
188
153
189
const handleError = ( err ) => {
154
190
const error = ( err ? err . toString ( ) : 'Unknown error!' ) + ' ' + query ;
155
- handleDone ( ) ;
191
+ handleDone ( true ) ;
156
192
msg . payload = error ;
157
193
msg . parts = {
158
194
id : partsId ,
@@ -171,6 +207,7 @@ module.exports = function (RED) {
171
207
} ;
172
208
173
209
handleDone ( ) ;
210
+ updateStatus ( + 1 ) ;
174
211
downstreamReady = true ;
175
212
176
213
try {
@@ -200,7 +237,7 @@ module.exports = function (RED) {
200
237
} else {
201
238
const complete = rows . length < node . rowsPerMsg ;
202
239
if ( complete ) {
203
- handleDone ( ) ;
240
+ handleDone ( false ) ;
204
241
}
205
242
const msg2 = Object . assign ( { } , msg , {
206
243
payload : ( node . rowsPerMsg || 1 ) > 1 ? rows : rows [ 0 ] ,
0 commit comments