Commit 359d06e7 authored by Leo Iannacone's avatar Leo Iannacone

moved watch_path_onsocket to utils

parent f6f16d2f
var fs = require('fs')
, path = require('path')
, Tail = require('tail').Tail
, config = require('./config.js')
, utils = require('./utils.js')
......@@ -78,47 +77,14 @@ function __send_distributions(event_name, socket, 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 __generic_handler(event_name, socket, data, watch_path, callback) {
__watch_path_onsocket(event_name, socket, data, config.debomatic.path, callback)
utils.watch_path_onsocket(event_name, socket, data, config.debomatic.path, callback)
callback(event_name, socket, data)
}
function __handler_get_file (socket, data) {
file_path = utils.get_file_path(data)
__watch_path_onsocket('file_newcontent', socket, data, file_path, __handler_file_newcontent)
utils.watch_path_onsocket('file_newcontent', socket, data, file_path, __handler_file_newcontent)
__send_file('file', socket, data)
}
......
var path = require('path')
, fs = require('fs')
, Tail = require('tail').Tail
, config = require('./config.js')
function __check_data_distribution(data) {
......@@ -52,6 +53,39 @@ function __get_files_list(dir, onlyDirectories, callback) {
});
}
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) {}
})
}
utils = {
check_data_distribution: function(data) {
return __check_data_distribution(data)
......@@ -73,6 +107,9 @@ utils = {
},
get_files_list: function(dir, onlyDirectories, callback) {
return __get_files_list(dir, onlyDirectories, callback)
},
watch_path_onsocket: function(event_name, socket, data, watch_path, updater) {
return __watch_path_onsocket(event_name, socket, data, watch_path, updater)
}
}
......
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