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") ...@@ -12,7 +12,7 @@ routes = require("./routes")
config = require("./lib/config") config = require("./lib/config")
utils = require("./lib/utils") utils = require("./lib/utils")
Client = require("./lib/client") Client = require("./lib/client")
Broadcaster = require("./lib/broadcaster") Debomatic = require("./lib/broadcaster")
app = module.exports = express() app = module.exports = express()
server = http.createServer(app) server = http.createServer(app)
io = require("socket.io")(server) io = require("socket.io")(server)
...@@ -83,14 +83,15 @@ server.listen config.port, config.host, null, (err) -> ...@@ -83,14 +83,15 @@ server.listen config.port, config.host, null, (err) ->
process.setgid uid process.setgid uid
process.setuid uid process.setuid uid
# statuses debomatic = new Debomatic(io.sockets)
status = [] debomatic.start()
broadcast = new Broadcaster(io.sockets, status)
io.sockets.on "connection", (socket) -> io.sockets.on "connection", (socket) ->
client = new Client(socket) client = new Client(socket)
client.start() client.start()
client.send_status status if status.length > 0 client.send_status debomatic.status
client.send_status_debomatic() client.send_status_debomatic(debomatic.running)
client.send_distributions(debomatic.distributions)
return return
console.log "Debomatic-webui listening on %s:%d in %s mode", console.log "Debomatic-webui listening on %s:%d in %s mode",
......
config = require("./config")
fs = require("fs") fs = require("fs")
config = require("./config")
utils = require("./utils") utils = require("./utils")
Tail = require("./tail") Tail = require("./tail")
e = config.events.broadcast
__watch_status_check_same_obj = (obj1, obj2) -> _get_distributions = (callback) ->
if obj1.status is obj2.status utils.get_files_list config.debomatic.path, true, (directories) ->
if obj1.distribution is obj2.distribution distributions = []
if obj1.hasOwnProperty("package") and obj2.hasOwnProperty("package") for dir in directories
return true if obj1.package is obj2.package data = {}
return false data.distribution = {}
return true data.distribution.name = dir
false 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 class Debomatic
if data.hasOwnProperty("success")
i = 0
while i < status.length constructor: (@sockets) ->
if __watch_status_check_same_obj(data, status[i]) @status = {}
status.splice i, 1 @distributions = []
break @running = fs.existsSync (config.debomatic.pidfile)
else _get_distributions (distributions) => @distributions = distributions
continue
i++
else
status.push data
socket.emit config.events.broadcast.status_update, data
return
watcher.on "error", (msg) -> # watcher on new distributions
socket.emit config.events.error, msg watch_distributions: ->
return 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 # watcher on build_status
__watch_distributions = (socket) -> watch_status: ->
fs.watch config.debomatic.path, watcher = new Tail(config.debomatic.jsonfile)
persistent: true watcher.on "line", (new_content) =>
, (event, fileName) -> 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 watcher.on "error", (msg) ->
setTimeout (-> @sockets.emit config.events.error, msg
utils.send_distributions socket
return
), 500
return
return start: ->
__watch_pidfile = (socket) -> # if json file does not still exist wait for its creation
fs.watchFile config.debomatic.pidfile, { if not fs.existsSync(config.debomatic.jsonfile)
persistent: false fs.watchFile config.debomatic.jsonfile, (curr, prev) ->
interval: 1007 if curr.ino isnt 0
} fs.unwatchFile(config.debomatic.jsonfile)
, (curr, prev) -> @watch_status()
# if === 0 means pidfile does not exists else
status_debomatic = running: curr.ino isnt 0 @watch_status()
try
socket.emit socket.emit(
config.events.broadcast.status_debomatic,
status_debomatic)
return
return
Broadcaster = (sockets, status) -> @watch_pidfile()
if not fs.existsSync(config.debomatic.jsonfile) @watch_distributions()
# 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)
module.exports = Broadcaster module.exports = Debomatic
...@@ -91,16 +91,19 @@ class Client ...@@ -91,16 +91,19 @@ class Client
@socket.emit e.file, data @socket.emit e.file, data
send_status: (status) -> 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: -> send_status_debomatic: (running) ->
fs.exists config.debomatic.pidfile, (exists) => @socket.emit config.events.broadcast.status_debomatic, running: running
@socket.emit config.events.broadcast.status_debomatic,
running: exists send_distributions: (distributions) ->
@socket.emit config.events.broadcast.distributions, distributions
start: -> start: ->
# init send distributions
utils.send_distributions @socket
# init events # init events
@socket.on e.distribution_packages, (data) => @socket.on e.distribution_packages, (data) =>
......
...@@ -59,6 +59,9 @@ get_files_list = (dir, onlyDirectories, callback) -> ...@@ -59,6 +59,9 @@ get_files_list = (dir, onlyDirectories, callback) ->
callback(result) 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) -> watch_path_onsocket = (event_name, socket, data, watch_path, callback) ->
socket_watchers = socket.watchers or {} socket_watchers = socket.watchers or {}
try try
...@@ -95,18 +98,6 @@ watch_path_onsocket = (event_name, socket, data, watch_path, callback) -> ...@@ -95,18 +98,6 @@ watch_path_onsocket = (event_name, socket, data, watch_path, callback) ->
err, socket) err, socket)
return 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) -> errors_handler = (from, err, socket) ->
from = "NO SOCKET: " + from unless socket from = "NO SOCKET: " + from unless socket
console.error from, err.message console.error from, err.message
...@@ -122,5 +113,5 @@ module.exports.get_package_path = get_package_path ...@@ -122,5 +113,5 @@ module.exports.get_package_path = get_package_path
module.exports.get_file_path = get_file_path module.exports.get_file_path = get_file_path
module.exports.get_files_list = get_files_list module.exports.get_files_list = get_files_list
module.exports.watch_path_onsocket = watch_path_onsocket module.exports.watch_path_onsocket = watch_path_onsocket
module.exports.send_distributions = send_distributions
module.exports.errors_handler = errors_handler 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