Commit 936e366a authored by Leo Iannacone's avatar Leo Iannacone

refactory on broadcast, now called Debomatic

parent fadf0818
......@@ -12,7 +12,7 @@ routes = require("./routes")
config = require("./lib/config")
utils = require("./lib/utils")
Client = require("./lib/client")
Broadcaster = require("./lib/broadcaster")
Debomatic = require("./lib/broadcaster")
app = module.exports = express()
server = http.createServer(app)
io = require("socket.io")(server)
......@@ -83,14 +83,15 @@ server.listen config.port, config.host, null, (err) ->
process.setgid uid
process.setuid uid
# statuses
status = []
broadcast = new Broadcaster(io.sockets, status)
debomatic = new Debomatic(io.sockets)
debomatic.start()
io.sockets.on "connection", (socket) ->
client = new Client(socket)
client.start()
client.send_status status if status.length > 0
client.send_status_debomatic()
client.send_status debomatic.status
client.send_status_debomatic(debomatic.running)
client.send_distributions(debomatic.distributions)
return
console.log "Debomatic-webui listening on %s:%d in %s mode",
......
config = require("./config")
fs = require("fs")
config = require("./config")
utils = require("./utils")
Tail = require("./tail")
e = config.events.broadcast
__watch_status_check_same_obj = (obj1, obj2) ->
if obj1.status is obj2.status
if obj1.distribution is obj2.distribution
if obj1.hasOwnProperty("package") and obj2.hasOwnProperty("package")
return true if obj1.package is obj2.package
return false
return true
false
_get_distributions = (callback) ->
utils.get_files_list config.debomatic.path, true, (directories) ->
distributions = []
for dir in directories
data = {}
data.distribution = {}
data.distribution.name = dir
pool_path = utils.get_distribution_pool_path(data)
distributions.push dir if fs.existsSync(pool_path)
callback(distributions)
# watcher on build_status
__watch_status = (socket, status) ->
watcher = new Tail(config.debomatic.jsonfile)
watcher.on "line", (new_content) ->
data = null
try
data = JSON.parse(new_content)
catch err
utils.errors_handler "Broadcaster:" +
"__watch_status:JSON.parse(new_content) - ",
err, socket
return
# looking for same status already in statuses lists
if data.hasOwnProperty("success")
i = 0
class Debomatic
while i < status.length
if __watch_status_check_same_obj(data, status[i])
status.splice i, 1
break
else
continue
i++
else
status.push data
socket.emit config.events.broadcast.status_update, data
return
constructor: (@sockets) ->
@status = {}
@distributions = []
@running = fs.existsSync (config.debomatic.pidfile)
_get_distributions (distributions) => @distributions = distributions
watcher.on "error", (msg) ->
socket.emit config.events.error, msg
return
# watcher on new distributions
watch_distributions: ->
fs.watch config.debomatic.path, (event, fileName) =>
check = =>
_get_distributions (new_distributions) =>
if not utils.arrayEqual(@distributions, new_distributions)
@distributions = new_distributions
@sockets.emit(e.distributions, @distributions)
# wait half a second to get pool subdir created
setTimeout(check, 500)
return
watch_pidfile: ->
fs.watchFile config.debomatic.pidfile, (curr, prev) =>
# if === 0 means pidfile does not exists
@running = curr.ino isnt 0
@sockets.emit e.status_debomatic, running: @running
# watcher on new distributions
__watch_distributions = (socket) ->
fs.watch config.debomatic.path,
persistent: true
, (event, fileName) ->
# watcher on build_status
watch_status: ->
watcher = new Tail(config.debomatic.jsonfile)
watcher.on "line", (new_content) =>
data = null
try
data = JSON.parse(new_content)
catch err
utils.errors_handler "Debomatoci:" +
"watch_status:JSON.parse(new_content) - ",
err, @sockets
return
# get a id rapresentation of status
get_key = (status) ->
key = status.distribution
key += "/#{status.package}" if status.package?
key += "/#{status.status}" if status.status?
return key
key = get_key(data)
if data.hasOwnProperty("success") and @status.key?
delete @status[key]
else
@status[key] = data
@sockets.emit e.status_update, data
# wait half a second to get pool subdir created
setTimeout (->
utils.send_distributions socket
return
), 500
return
watcher.on "error", (msg) ->
@sockets.emit config.events.error, msg
return
__watch_pidfile = (socket) ->
fs.watchFile config.debomatic.pidfile, {
persistent: false
interval: 1007
}
, (curr, prev) ->
# if === 0 means pidfile does not exists
status_debomatic = running: curr.ino isnt 0
try
socket.emit socket.emit(
config.events.broadcast.status_debomatic,
status_debomatic)
return
return
start: ->
# if json file does not still exist wait for its creation
if not fs.existsSync(config.debomatic.jsonfile)
fs.watchFile config.debomatic.jsonfile, (curr, prev) ->
if curr.ino isnt 0
fs.unwatchFile(config.debomatic.jsonfile)
@watch_status()
else
@watch_status()
Broadcaster = (sockets, status) ->
if not fs.existsSync(config.debomatic.jsonfile)
# watch until json log file is created
fs.watchFile config.debomatic.jsonfile, (curr, prev) ->
if curr.ino isnt 0
fs.unwatchFile(config.debomatic.jsonfile)
__watch_status(sockets, status)
else
__watch_status(sockets, status)
__watch_distributions(sockets)
__watch_pidfile(sockets)
@watch_pidfile()
@watch_distributions()
module.exports = Broadcaster
module.exports = Debomatic
......@@ -91,16 +91,19 @@ class Client
@socket.emit e.file, data
send_status: (status) ->
@socket.emit e.status, status
data = status
if status instanceof Array == false
data = []
data.push v for k, v of status
@socket.emit e.status, data
send_status_debomatic: ->
fs.exists config.debomatic.pidfile, (exists) =>
@socket.emit config.events.broadcast.status_debomatic,
running: exists
send_status_debomatic: (running) ->
@socket.emit config.events.broadcast.status_debomatic, running: running
send_distributions: (distributions) ->
@socket.emit config.events.broadcast.distributions, distributions
start: ->
# init send distributions
utils.send_distributions @socket
# init events
@socket.on e.distribution_packages, (data) =>
......
......@@ -59,6 +59,9 @@ get_files_list = (dir, onlyDirectories, callback) ->
callback(result)
arrayEqual = (a, b) ->
a.length is b.length and a.every (elem, i) -> elem is b[i]
watch_path_onsocket = (event_name, socket, data, watch_path, callback) ->
socket_watchers = socket.watchers or {}
try
......@@ -95,18 +98,6 @@ watch_path_onsocket = (event_name, socket, data, watch_path, callback) ->
err, socket)
return
send_distributions = (socket) ->
get_files_list config.debomatic.path, true, (directories) ->
distributions = []
for dir in directories
data = {}
data.distribution = {}
data.distribution.name = dir
pool_path = get_distribution_pool_path(data)
distributions.push dir if fs.existsSync(pool_path)
socket.emit config.events.broadcast.distributions, distributions
errors_handler = (from, err, socket) ->
from = "NO SOCKET: " + from unless socket
console.error from, err.message
......@@ -122,5 +113,5 @@ module.exports.get_package_path = get_package_path
module.exports.get_file_path = get_file_path
module.exports.get_files_list = get_files_list
module.exports.watch_path_onsocket = watch_path_onsocket
module.exports.send_distributions = send_distributions
module.exports.errors_handler = errors_handler
module.exports.arrayEqual = arrayEqual
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment