-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.coffee
More file actions
74 lines (56 loc) · 2.47 KB
/
app.coffee
File metadata and controls
74 lines (56 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
############################### Settings ##############################
queueNameFilter = /nigiri.server.message/i
apiPath = 'http://illum-qa-india:15672/api/'
intervalSeconds = 5
startAtCountLessThan = 100
stopAtCountGreaterThan = 116
#######################################################################
rest = require 'rest/'
pathPrefix = require 'rest/interceptor/pathPrefix'
errorCode = require 'rest/interceptor/errorCode'
mime = require 'rest/interceptor/mime'
basicAuth = require 'rest/interceptor/basicAuth'
service = "wuauserv"
serviceAlreadyRunning = 1056
serviceNotBeenStarted = 1062
client = rest.chain mime
.chain errorCode, { code: 500 }
.chain pathPrefix, { prefix: apiPath }
.chain basicAuth, { username: 'guest', password: 'guest' };
handler = (idx, callback) ->
(res) ->
ct = 0
ct += q.backing_queue_status.persistent_count for q in res.entity when queueNameFilter.test q.name
console.log "At #{new Date()} #{q.vhost}/#{q.name}: #{q.backing_queue_status.persistent_count} " for q in res.entity when queueNameFilter.test(q.name) and q.backing_queue_status.persistent_count
callback null, ct
setCount = (ct) ->
if ct < startAtCountLessThan
ensureRunning ct
if ct > stopAtCountGreaterThan
ensureStopped ct
ensureRunning = (ct) ->
console.log " Start #{service} if it's not already running. ct=" + ct
runProcess "sc start #{service}", [serviceAlreadyRunning]
ensureStopped = (ct) ->
console.log " Stop #{service} if it's running. ct=" + ct
runProcess "sc stop #{service}", [serviceNotBeenStarted]
checkCount = () ->
client(path:'queues').then handler("first", (err, ct) -> setCount ct)
runProcess = (command, ignoredStatuses) ->
terminal = require('child_process').exec command
exited = false
output = ""
terminal.stdout.on 'data', (data) ->
output += data
terminal.on 'exit', (code) ->
if -1 == ignoredStatuses.indexOf code
console.log ' ' + output.replace /\r\n/g, ' '
console.log ' child process exited with code ' + code
exited = false
console.log "======================================================="
console.log "Monitoring #{queueNameFilter} on #{apiPath}"
console.log " every #{intervalSeconds} seconds"
console.log "======================================================="
console.log " Note: This must be run with elevated priviliges"
console.log "======================================================="
setInterval checkCount, intervalSeconds * 1000