Commit 8a6307f0 authored by Leo Iannacone's avatar Leo Iannacone

moved watcher to send lib and renamed event package_file_list in package_files_list

parent ab0a52af
...@@ -7,7 +7,6 @@ var express = require('express') ...@@ -7,7 +7,6 @@ var express = require('express')
, routes = require('./routes') , routes = require('./routes')
, fs = require('fs') , fs = require('fs')
, path = require('path') , path = require('path')
, Tail = require('tail').Tail
, config = require('./lib/config.js') , config = require('./lib/config.js')
, send = require('./lib/send.js') , send = require('./lib/send.js')
, utils = require('./lib/utils.js') , utils = require('./lib/utils.js')
...@@ -40,39 +39,6 @@ var io = require('socket.io').listen(app); ...@@ -40,39 +39,6 @@ var io = require('socket.io').listen(app);
app.get('/', routes.index); app.get('/', routes.index);
app.get('/distribution', routes.distribution) app.get('/distribution', routes.distribution)
function watch_path_onsocket(event_name, socket, data, watch_path, updater) {
name = "watcher-" + event_name
socket.get(name, function (err, watcher) {
if (watcher) {
try {
watcher.unwatch()
} catch (errorWatchingDirectory) {
watcher.close()
}
}
try {
fs.stat(watch_path, function(err, stats) {
if (err)
return
if (stats.isDirectory()) {
watcher = fs.watch(watch_path, {persistent: true}, function (event, fileName) {
if(event == 'rename')
updater(socket, data)
})
}
else {
watcher = new Tail(watch_path)
watcher.on('line', function(new_content) {
data.file.new_content = new_content + '\n'
updater(socket, data)
})
}
socket.set(name, watcher)
})
} catch (err_watch) {}
})
}
io.sockets.on('connection', function(socket) { io.sockets.on('connection', function(socket) {
send.distributions(socket); send.distributions(socket);
...@@ -80,25 +46,19 @@ io.sockets.on('connection', function(socket) { ...@@ -80,25 +46,19 @@ io.sockets.on('connection', function(socket) {
socket.on('get_distribution_packages', function (data) { socket.on('get_distribution_packages', function (data) {
if (! utils.check_data_distribution(data)) if (! utils.check_data_distribution(data))
return return
distribution_path = path.join(config.debomatic.path, data.distribution.name, 'pool')
watch_path_onsocket('get_distribution_packages', socket, data, distribution_path, send.distribution_packages)
send.distribution_packages(socket, data); send.distribution_packages(socket, data);
}) })
socket.on('get_package_file_list', function(data) { socket.on('get_package_files_list', function(data) {
if (! utils.check_data_package(data)) if (! utils.check_data_package(data))
return return
package_path = utils.get_package_path(data) send.package_files_list(socket, data)
watch_path_onsocket('get_package_file_list', socket, data, package_path, send.package_file_list)
send.package_file_list(socket, data)
}) })
socket.on('get_file', function (data){ socket.on('get_file', function (data){
if (! utils.check_data_file(data)) if (! utils.check_data_file(data))
return return
file_path = utils.get_file_path(data)
watch_path_onsocket('get_file', socket, data, file_path, send.file_newcontent)
send.file(socket, data) send.file(socket, data)
}) })
}); });
......
var fs = require('fs') var fs = require('fs')
, path = require('path') , path = require('path')
, Tail = require('tail').Tail
, config = require('./config.js') , config = require('./config.js')
, utils = require('./utils.js') , utils = require('./utils.js')
...@@ -57,13 +58,13 @@ function __get_files_list_from_package(data, callback) { ...@@ -57,13 +58,13 @@ function __get_files_list_from_package(data, callback) {
}); });
} }
function __send_package_files_list (socket, data) { function __send_package_files_list (event_name, socket, data) {
__get_files_list_from_package(data, function(new_data){ __get_files_list_from_package(data, function(new_data){
socket.emit('package_file_list', new_data) socket.emit(event_name, new_data)
}); });
} }
function __send_distribution_packages (socket, data) { function __send_distribution_packages (event_name, socket, data) {
distro_path = utils.get_distribution_pool_path(data) distro_path = utils.get_distribution_pool_path(data)
__get_files_list(distro_path, true, function (packages) { __get_files_list(distro_path, true, function (packages) {
data.distribution.packages = [] data.distribution.packages = []
...@@ -80,20 +81,57 @@ function __send_distribution_packages (socket, data) { ...@@ -80,20 +81,57 @@ function __send_distribution_packages (socket, data) {
} }
data.distribution.packages.push(pack) data.distribution.packages.push(pack)
}); });
socket.emit("distribution_packages", data) socket.emit(event_name, data)
}); });
} }
function __send_file (socket, data) { function __send_file (event_name, socket, data) {
file_path = utils.get_file_path(data) file_path = utils.get_file_path(data)
fs.readFile(file_path, 'utf8', function (err, content) { fs.readFile(file_path, 'utf8', function (err, content) {
if (err) return; if (err) return;
data.file.orig_name = file_path.split('/').pop() data.file.orig_name = file_path.split('/').pop()
data.file.content = content data.file.content = content
socket.emit('file', data) socket.emit(event_name, data)
}); });
} }
function __watch_path_onsocket(event_name, socket, data, watch_path, updater) {
name = "watcher-" + event_name
socket.get(name, function (err, watcher) {
if (watcher) {
try {
watcher.unwatch()
} catch (errorWatchingDirectory) {
watcher.close()
}
}
try {
fs.stat(watch_path, function(err, stats) {
if (err)
return
if (stats.isDirectory()) {
watcher = fs.watch(watch_path, {persistent: true}, function (event, fileName) {
if(event == 'rename')
updater(event_name, socket, data)
})
}
else {
watcher = new Tail(watch_path)
watcher.on('line', function(new_content) {
data.file.new_content = new_content + '\n'
updater(event_name, socket, data)
})
}
socket.set(name, watcher)
})
} catch (err_watch) {}
})
}
function __file_newcontent(event_name, socket, data) {
socket.emit(event_name, data)
}
sender = { sender = {
distributions: function(socket) { distributions: function(socket) {
...@@ -102,21 +140,25 @@ sender = { ...@@ -102,21 +140,25 @@ sender = {
}); });
}, },
package_file_list: function(socket, data) { package_files_list: function(socket, data) {
__send_package_files_list(socket, data) event_name = 'package_files_list'
package_path = utils.get_package_path(data)
__watch_path_onsocket(event_name, socket, data, package_path, __send_package_files_list)
__send_package_files_list(event_name, socket, data)
}, },
distribution_packages: function(socket, data) { distribution_packages: function(socket, data) {
__send_distribution_packages(socket, data) event_name = 'distribution_packages'
distribution_path = path.join(config.debomatic.path, data.distribution.name, 'pool')
__watch_path_onsocket(event_name, socket, data, distribution_path, __send_distribution_packages)
__send_distribution_packages(event_name, socket, data)
}, },
file: function(socket, data) { file: function(socket, data) {
__send_file(socket, data) file_path = utils.get_file_path(data)
__watch_path_onsocket('file_newcontent', socket, data, file_path, __file_newcontent)
__send_file('file', socket, data)
}, },
file_newcontent: function(socket, data) {
socket.emit('file_newcontent', data);
}
} }
module.exports = sender module.exports = sender
...@@ -16,7 +16,7 @@ if (window.location.pathname == '/distribution') { ...@@ -16,7 +16,7 @@ if (window.location.pathname == '/distribution') {
Page_Distrubion.packages.set(data) Page_Distrubion.packages.set(data)
}) })
socket.on('package_file_list', function(data){ socket.on('package_files_list', function(data){
Page_Distrubion.files.set(data) Page_Distrubion.files.set(data)
}) })
......
...@@ -87,7 +87,7 @@ var Page_Distrubion = { ...@@ -87,7 +87,7 @@ var Page_Distrubion = {
if (! data) if (! data)
data = Utils.from_hash_to_data() data = Utils.from_hash_to_data()
if (Utils.check_data_package(data)) if (Utils.check_data_package(data))
socket.emit("get_package_file_list", data) socket.emit("get_package_files_list", data)
}, },
select: function(file) { select: function(file) {
$("#logs li").removeClass('active') $("#logs li").removeClass('active')
......
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