code beautification, message normalization

This commit is contained in:
Axel Kittenberger 2012-01-31 15:01:11 +01:00
parent d547cf600c
commit 8f3ae7d0a1
2 changed files with 161 additions and 174 deletions

View File

@ -9,7 +9,7 @@
-- It works closely together with the Lsyncd core in lsyncd.c. This means it -- It works closely together with the Lsyncd core in lsyncd.c. This means it
-- cannot be runned directly from the standard lua interpreter. -- cannot be runned directly from the standard lua interpreter.
--============================================================================ --============================================================================
-- require("profiler") -- require('profiler')
-- profiler.start() -- profiler.start()
----- -----
@ -1862,7 +1862,7 @@ local Inotify = (function()
-- creates a Create event for entry. -- creates a Create event for entry.
if raiseSync then if raiseSync then
local relative = splitPath(pd, syncRoots[raiseSync]) local relative = splitPath(pd, syncRoots[raiseSync])
if relative then raiseSync:delay("Create", raiseTime, relative) end if relative then raiseSync:delay('Create', raiseTime, relative) end
end end
-- adds syncs for subdirs -- adds syncs for subdirs
if isdir then addWatch(pd, true, raiseSync, raiseTime) end if isdir then addWatch(pd, true, raiseSync, raiseTime) end
@ -1870,10 +1870,10 @@ local Inotify = (function()
end end
----- -----
-- adds a Sync to receive events -- Adds a Sync to receive events.
-- --
-- @param sync Object to receive events -- sync: Object to receive events
-- @param rootdir root dir to watch -- rootdir: root dir to watch
-- --
local function addSync(sync, rootdir) local function addSync(sync, rootdir)
if syncRoots[sync] then if syncRoots[sync] then
@ -1886,12 +1886,13 @@ local Inotify = (function()
----- -----
-- Called when an event has occured. -- Called when an event has occured.
-- --
-- @param etype "Attrib", "Mofify", "Create", "Delete", "Move") -- etype: 'Attrib', 'Mofify', 'Create', 'Delete', 'Move'
-- @param wd watch descriptor (matches lsyncd.inotifyadd()) -- wd: watch descriptor, matches lsyncd.inotifyadd()
-- @param isdir true if filename is a directory -- isdir: true if filename is a directory
-- @param time time of event -- time: time of event
-- @param filename string filename without path -- filename: string filename without path
-- @param filename2 -- wd2: watch descriptor for target if it's a Move
-- filename2: string filename without path of Move target
-- --
local function event(etype, wd, isdir, time, filename, wd2, filename2) local function event(etype, wd, isdir, time, filename, wd2, filename2)
if isdir then if isdir then
@ -1913,16 +1914,15 @@ local Inotify = (function()
if path2 and filename2 then path2 = path2..filename2 end if path2 and filename2 then path2 = path2..filename2 end
if not path and path2 and etype == 'Move' then if not path and path2 and etype == 'Move' then
log("Inotify", "Move from deleted directory ",path2, log('Inotify', 'Move from deleted directory ',path2,' becomes Create.')
" becomes Create.")
path = path2 path = path2
path2 = nil path2 = nil
etype = "Create" etype = 'Create'
end end
if not path then if not path then
-- this is normal in case of deleted subdirs -- this is normal in case of deleted subdirs
log("Inotify", "event belongs to unknown watch descriptor.") log('Inotify', 'event belongs to unknown watch descriptor.')
return return
end end
@ -1989,6 +1989,7 @@ end)()
-- All fsevents specific implementation should be enclosed here. -- All fsevents specific implementation should be enclosed here.
-- --
local Fsevents = (function() local Fsevents = (function()
----- -----
-- A list indexed by sync's containing the root path this -- A list indexed by sync's containing the root path this
-- sync is interested in. -- sync is interested in.
@ -2469,7 +2470,7 @@ function runner.callError(message)
if not info then if not info then
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
end end
log('Error', 'Backtrace ', level - 1, ' :', info.short_src, ":", info.currentline) log('Error', 'Backtrace ',level - 1,' :',info.short_src,':',info.currentline)
level = level + 1 level = level + 1
end end
end end
@ -2480,14 +2481,10 @@ end
-- --
function runner.collectProcess(pid, exitcode) function runner.collectProcess(pid, exitcode)
processCount = processCount - 1 processCount = processCount - 1
if processCount < 0 then if processCount < 0 then error('negative number of processes!') end
error("negative number of processes!")
end
for _, s in Syncs.iwalk() do for _, s in Syncs.iwalk() do
if s:collect(pid, exitcode) then if s:collect(pid, exitcode) then return end
return
end
end end
end end
@ -2978,7 +2975,7 @@ function spawn(agent, binary, ...)
-- checks if spawn is called on already active event -- checks if spawn is called on already active event
if dol.status then if dol.status then
if dol.status ~= "wait" then if dol.status ~= 'wait' then
error('spawn() called on an non-waiting event', 2) error('spawn() called on an non-waiting event', 2)
end end
else -- is a list else -- is a list
@ -3154,7 +3151,7 @@ local default_rsync = {
-- d1/d2/d3/f1 needs filters -- d1/d2/d3/f1 needs filters
-- 'd1/', 'd1/d2/', 'd1/d2/d3/' and 'd1/d2/d3/f1' -- 'd1/', 'd1/d2/', 'd1/d2/d3/' and 'd1/d2/d3/f1'
for _, path in ipairs(paths) do for _, path in ipairs(paths) do
if path and path ~="" then if path and path ~= '' then
addToFilter(path) addToFilter(path)
local pp = string.match(path, '^(.*/)[^/]+/?') local pp = string.match(path, '^(.*/)[^/]+/?')
while pp do while pp do
@ -3197,7 +3194,7 @@ local default_rsync = {
config.source, config.source,
config.target) config.target)
else else
local exS = table.concat(excludes, "\n") local exS = table.concat(excludes, '\n')
log('Normal', 'recursive startup rsync: ',config.source, log('Normal', 'recursive startup rsync: ',config.source,
' -> ',config.target,' excluding\n',exS) ' -> ',config.target,' excluding\n',exS)
spawn(event, config.rsyncBinary, spawn(event, config.rsyncBinary,
@ -3295,7 +3292,7 @@ local default_rsyncssh = {
end end
end end
local sPaths = table.concat(paths, "\n") local sPaths = table.concat(paths, '\n')
local zPaths = table.concat(paths, config.xargs.delimiter) local zPaths = table.concat(paths, config.xargs.delimiter)
log('Normal', 'Deleting list\n', sPaths) log('Normal', 'Deleting list\n', sPaths)
spawn(elist, '/usr/bin/ssh', spawn(elist, '/usr/bin/ssh',
@ -3322,8 +3319,8 @@ local default_rsyncssh = {
paths[k] = string.sub(v, 1, -2) paths[k] = string.sub(v, 1, -2)
end end
end end
local sPaths = table.concat(paths, "\n") local sPaths = table.concat(paths, '\n')
local zPaths = table.concat(paths, "\000") local zPaths = table.concat(paths, '\000')
log('Normal', 'Rsyncing list\n', sPaths) log('Normal', 'Rsyncing list\n', sPaths)
spawn( spawn(
elist, config.rsyncBinary, elist, config.rsyncBinary,
@ -3343,52 +3340,45 @@ local default_rsyncssh = {
if not agent.isList and agent.etype == 'Init' then if not agent.isList and agent.etype == 'Init' then
local rc = rsync_exitcodes[exitcode] local rc = rsync_exitcodes[exitcode]
if rc == 'ok' then if rc == 'ok' then
log("Normal", "Startup of '",agent.source,"' finished.") log('Normal', 'Startup of "',agent.source,'" finished: ', exitcode)
elseif rc == "again" then elseif rc == 'again' then
if settings.insist then if settings.insist then
log("Normal", "Retrying startup of '",agent.source,"'.") log('Normal', 'Retrying startup of "',agent.source,'": ', exitcode)
else else
log("Error", log('Error', 'Temporary or permanent failure on startup of "',
"Temporary or permanent failure on startup. Terminating since not insisting."); agent.source, '". Terminating since "insist" is not set.');
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
end end
elseif rc == "die" then elseif rc == 'die' then
log("Error", "Failure on startup of '",agent.source,"'.") log('Error', 'Failure on startup of "',agent.source,'": ', exitcode)
else else
log("Error", "Unknown exitcode '",exticode,"' with a list") log('Error', 'Unknown exitcode on startup of "', agent.source,': "',exitcode)
rc = "die" rc = 'die'
end end
return rc return rc
end end
if agent.isList then if agent.isList then
local rc = rsync_exitcodes[exitcode] local rc = rsync_exitcodes[exitcode]
if rc == "ok" then if rc == 'ok' then log('Normal', 'Finished (list): ',exitcode)
log("Normal", "Finished a list = ",exitcode) elseif rc == 'again' then log('Normal', 'Retrying (list): ',exitcode)
elseif rc == "again" then elseif rc == 'die' then log('Error', 'Failure (list): ', exitcode)
log("Normal", "Retrying a list on exitcode = ",exitcode) else
elseif rc == "die" then log('Error', 'Unknown exitcode (list): ',exitcode)
log("Error", "Failure on list on exitcode = ",exitcode) rc = 'die'
else
log("Error", "Unknown exitcode on list = ",exitcode)
rc = "die"
end end
return rc return rc
else else
local rc = ssh_exitcodes[exitcode] local rc = ssh_exitcodes[exitcode]
if rc == "ok" then if rc == 'ok' then
log("Normal", "Finished ",agent.etype, log('Normal', 'Finished ',agent.etype,' ',agent.sourcePath,': ',exitcode)
" on ",agent.sourcePath," = ",exitcode) elseif rc == 'again' then
elseif rc == "again" then log('Normal', 'Retrying ',agent.etype,' ',agent.sourcePath,': ',exitcode)
log("Normal", "Retrying ",agent.etype, elseif rc == 'die' then
" on ",agent.sourcePath," = ",exitcode) log('Normal', 'Failure ',agent.etype,' ',agent.sourcePath,': ',exitcode)
elseif rc == "die" then
log("Normal", "Failure ",agent.etype,
" on ",agent.sourcePath," = ",exitcode)
else else
log("Error", "Unknown exitcode ",agent.etype, log('Error', 'Unknown exitcode ',agent.etype,' ',agent.sourcePath,': ',exitcode)
" on ",agent.sourcePath," = ",exitcode) rc = 'die'
rc = "die"
end end
return rc return rc
end end
@ -3400,31 +3390,32 @@ local default_rsyncssh = {
init = function(event) init = function(event)
local config = event.config local config = event.config
local inlet = event.inlet local inlet = event.inlet
local excludes = inlet.getExcludes(); local excludes = inlet.getExcludes()
local target = config.host .. ':' .. config.targetdir
if #excludes == 0 then if #excludes == 0 then
log("Normal", "recursive startup rsync: ", config.source, log('Normal', 'Recursive startup rsync: ',config.source,' -> ',target)
" -> ", config.host .. ":" .. config.targetdir)
spawn( spawn(
event, config.rsyncBinary, event, config.rsyncBinary,
"--delete", '--delete',
"-r", '-r',
config.rsyncOpts, config.rsyncOpts,
config.source, config.source,
config.host .. ":" .. config.targetdir target
) )
else else
local exS = table.concat(excludes, "\n") local exS = table.concat(excludes, '\n')
log("Normal", "recursive startup rsync: ", config.source, log('Normal', 'Recursive startup rsync: ',config.source,
" -> ", config.host .. ":" .. config.targetdir, " excluding\n") ' -> ',target,' with excludes.')
spawn( spawn(
event, config.rsyncBinary, event, config.rsyncBinary,
"<", exS, '<', exS,
"--exclude-from=-", '--exclude-from=-',
"--delete", '--delete',
"-r", '-r',
config.rsyncOpts, config.rsyncOpts,
config.source, config.source,
config.host .. ":" .. config.targetdir target
) )
end end
end, end,
@ -3433,34 +3424,24 @@ local default_rsyncssh = {
-- Checks the configuration. -- Checks the configuration.
-- --
prepare = function(config) prepare = function(config)
if config.rsyncOps then if not config.host then error('default.rsyncssh needs "host" configured', 4) end
if config.rsyncOpts ~= "-lts" then if not config.targetdir then error('default.rsyncssh needs "targetdir" configured', 4) end
error("'rsyncOpts' and 'rsyncOps' provided in config, decide for one.")
end
config.rsyncOpts = config.rsyncOps
end
if not config.host then
error("default.rsyncssh needs 'host' configured", 4)
end
if not config.targetdir then
error("default.rsyncssh needs 'targetdir' configured", 4)
end
-- appends a slash to the targetdir if missing -- appends a slash to the targetdir if missing
if string.sub(config.targetdir, -1) ~= "/" then if string.sub(config.targetdir, -1) ~= '/' then
config.targetdir = config.targetdir .. "/" config.targetdir = config.targetdir .. '/'
end end
end, end,
----- -----
-- The rsync binary called. -- The rsync binary called.
-- --
rsyncBinary = "/usr/bin/rsync", rsyncBinary = '/usr/bin/rsync',
----- -----
-- Calls rsync with this default short opts. -- Calls rsync with this default short opts.
-- --
rsyncOpts = "-lts", rsyncOpts = '-lts',
----- -----
-- allow processes -- allow processes
@ -3483,7 +3464,11 @@ local default_rsyncssh = {
-- available this is simpler than to build filters for rsync for this. -- available this is simpler than to build filters for rsync for this.
-- Default uses '0' as limiter, you might override this for old systems. -- Default uses '0' as limiter, you might override this for old systems.
-- --
xargs = {binary = "/usr/bin/xargs", delimiter = '\000', xparams = {"-0", "rm -rf"}} xargs = {
binary = '/usr/bin/xargs',
delimiter = '\000',
xparams = {'-0', 'rm -rf'}
}
} }
----- -----
@ -3498,47 +3483,53 @@ local default_direct = {
-- gets all events ready for syncing -- gets all events ready for syncing
local event, event2 = inlet.getEvent() local event, event2 = inlet.getEvent()
if event.etype == "Create" then if event.etype == 'Create' then
if event.isdir then if event.isdir then
spawn(event, spawn(
"/bin/mkdir", event,
"-p", '/bin/mkdir',
'-p',
event.targetPath event.targetPath
) )
else else
spawn(event, spawn(
"/bin/cp", event,
"-t", '/bin/cp',
'-t',
event.targetPathdir, event.targetPathdir,
event.sourcePath event.sourcePath
) )
end end
elseif event.etype == "Modify" then elseif event.etype == 'Modify' then
if event.isdir then if event.isdir then
error("Do not know how to handle 'Modify' on dirs") error("Do not know how to handle 'Modify' on dirs")
end end
spawn(event, spawn(event,
"/bin/cp", '/bin/cp',
"-t", '-t',
event.targetPathdir, event.targetPathdir,
event.sourcePath event.sourcePath
) )
elseif event.etype == "Delete" then elseif event.etype == 'Delete' then
local tp = event.targetPath local tp = event.targetPath
-- extra security check -- extra security check
if tp == "" or tp == "/" or not tp then if tp == '' or tp == '/' or not tp then
error("Refusing to erase your harddisk") error('Refusing to erase your harddisk!')
end end
spawn(event, "/bin/rm", "-rf", tp) spawn(event, '/bin/rm', '-rf', tp)
elseif event.etype == "Move" then elseif event.etype == 'Move' then
local tp = event.targetPath local tp = event.targetPath
-- extra security check -- extra security check
if tp == "" or tp == "/" or not tp then if tp == '' or tp == '/' or not tp then
error("Refusing to erase your harddisk") error('Refusing to erase your harddisk!')
end end
spawnShell(event, "/bin/mv $1 $2 || /bin/rm -rf $1", event.targetPath, event2.targetPath) spawnShell(
event,
'/bin/mv $1 $2 || /bin/rm -rf $1',
event.targetPath,
event2.targetPath)
else else
log("Warn", "ignored an event of type '", event.etype, "'") log('Warn', 'ignored an event of type "',event.etype, '"')
inlet.discardEvent(event) inlet.discardEvent(event)
end end
end, end,
@ -3549,23 +3540,23 @@ local default_direct = {
collect = function(agent, exitcode) collect = function(agent, exitcode)
local config = agent.config local config = agent.config
if not agent.isList and agent.etype == "Init" then if not agent.isList and agent.etype == 'Init' then
local rc = rsync_exitcodes[exitcode] local rc = rsync_exitcodes[exitcode]
if rc == "ok" then if rc == 'ok' then
log("Normal", "Startup of '",agent.source,"' finished.") log('Normal', 'Startup of "',agent.source,'" finished: ', exitcode)
elseif rc == "again" then elseif rc == 'again' then
if settings.insist then if settings.insist then
log("Normal", "Retrying startup of '",agent.source,"'.") log('Normal', 'Retrying startup of "',agent.source,'": ', exitcode)
else else
log("Error", log('Error', 'Temporary or permanent failure on startup of "',
"Temporary or permanent failure on startup. Terminating since not insisting."); agent.source, '". Terminating since "insist" is not set.');
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
end end
elseif rc == "die" then elseif rc == 'die' then
log("Error", "Failure on startup of '",agent.source,"'.") log('Error', 'Failure on startup of "',agent.source,'": ', exitcode)
else else
log("Error", "Unknown exitcode '",exticode,"' with a list") log('Error', 'Unknown exitcode on startup of "', agent.source,': "',exitcode)
rc = "die" rc = 'die'
end end
return rc return rc
end end
@ -3655,70 +3646,66 @@ default = {
if config.exitcodes then if config.exitcodes then
rc = config.exitcodes[exitcode] rc = config.exitcodes[exitcode]
elseif exitcode == 0 then elseif exitcode == 0 then
rc = "ok" rc = 'ok'
else else
rc = "die" rc = 'die'
end end
if not agent.isList and agent.etype == "Init" then -- TODO synchronize with similar code before
if rc == "ok" then if not agent.isList and agent.etype == 'Init' then
log("Normal", "Startup of '",agent.source,"' finished.") if rc == 'ok' then
return "ok" log('Normal', 'Startup of "',agent.source,'" finished.')
elseif rc == "again" then return 'ok'
log("Normal", "Retrying startup of '",agent.source,"'.") elseif rc == 'again' then
log('Normal', 'Retrying startup of "',agent.source,'".')
return "again" return "again"
elseif rc == "die" then elseif rc == 'die' then
log("Error", "Failure on startup of '",agent.source,"'.") log('Error', 'Failure on startup of "',agent.source,'".')
terminate(-1) -- ERRNO terminate(-1) -- ERRNO
else else
log("Error", "Unknown exitcode '",exitcode,"' on startup of '",agent.source,"'.") log('Error', 'Unknown exitcode "',exitcode,'" on startup of "',agent.source,'".')
return "die" return 'die'
end end
end end
if agent.isList then if agent.isList then
if rc == "ok" then if rc == 'ok' then log('Normal', 'Finished a list = ',exitcode)
log("Normal", "Finished a list = ",exitcode) elseif rc == 'again' then log('Normal', 'Retrying a list on exitcode = ',exitcode)
elseif rc == "again" then elseif rc == 'die' then log('Error', 'Failure with a list on exitcode = ',exitcode)
log("Normal", "Retrying a list on exitcode = ",exitcode)
elseif rc == "die" then
log("Error", "Failure with a list on exitcode = ",exitcode)
else else
log("Error", "Unknown exitcode '",exitcode,"' with a list") log('Error', 'Unknown exitcode "',exitcode,'" with a list')
rc = "die" rc = 'die'
end end
else else
if rc == "ok" then if rc == 'ok' then
log("Normal", "Retrying ",agent.etype, log('Normal', 'Retrying ',agent.etype,' on ',agent.sourcePath,' = ',exitcode)
" on ",agent.sourcePath," = ",exitcode) elseif rc == 'again' then
elseif rc == "again" then log('Normal', 'Finished ',agent.etype,' on ',agent.sourcePath,' = ',exitcode)
log("Normal", "Finished ",agent.etype, elseif rc == 'die' then
" on ",agent.sourcePath," = ",exitcode) log('Error', 'Failure with ',agent.etype,' on ',agent.sourcePath,' = ',exitcode)
elseif rc == "die" then
log("Error", "Failure with ",agent.etype,
" on ",agent.sourcePath," = ",exitcode)
else else
log("Normal", "Unknown exitcode '",exitcode,"' with ", agent.etype, log('Normal', 'Unknown exitcode "',exitcode,'" with ', agent.etype,
" on ",agent.sourcePath," = ",exitcode) ' on ',agent.sourcePath,' = ',exitcode)
rc = "die" rc = 'die'
end end
end end
return rc return rc
end, end,
----- -----
-- called on (re)initalizing of Lsyncd. -- called on (re)initialization of Lsyncd.
-- --
init = function(event) init = function(event)
local config = event.config local config = event.config
local inlet = event.inlet local inlet = event.inlet
-- user functions -- user functions
-- calls a startup if given by user script. -- calls a startup if given by user script.
if type(config.onStartup) == "function" then if type(config.onStartup) == 'function' then
local startup = config.onStartup(event) local startup = config.onStartup(event)
-- TODO honor some return codes of startup like "warmstart". -- TODO honor some return codes of startup like "warmstart".
end end
if event.status == "wait" then
if event.status == 'wait' then
-- user script did not spawn anything -- user script did not spawn anything
-- thus the blanket event is deleted again. -- thus the blanket event is deleted again.
inlet.discardEvent(event) inlet.discardEvent(event)

View File

@ -1,42 +1,42 @@
#!/usr/bin/lua #!/usr/bin/lua
-- a heavy duty test. -- a heavy duty test.
-- makes thousends of random changes to the source tree -- makes thousends of random changes to the source tree
require("posix")
dofile("tests/testlib.lua")
cwriteln("****************************************************************") require('posix')
cwriteln(" Testing default.direct with random data activity ") dofile('tests/testlib.lua')
cwriteln("****************************************************************")
cwriteln('****************************************************************')
cwriteln(' Testing default.direct with random data activity ')
cwriteln('****************************************************************')
local tdir, srcdir, trgdir = mktemps() local tdir, srcdir, trgdir = mktemps()
-- makes some startup data -- makes some startup data
churn(srcdir, 10) churn(srcdir, 10)
local logs = {"-log", "Exec"} local logs = {'-log', 'Exec'}
--logs = {"-log", "Delay", "-log", "Fsevents" } local pid = spawn(
local pid = spawn("./lsyncd", "-nodaemon", './lsyncd',
"-direct", srcdir, trgdir, unpack(logs)) '-nodaemon',
'-direct', srcdir, trgdir,
unpack(logs)
)
cwriteln("waiting for Lsyncd to startup") cwriteln('waiting for Lsyncd to startup')
posix.sleep(1) posix.sleep(1)
churn(srcdir, 500) churn(srcdir, 500)
cwriteln("waiting for Lsyncd to finish its jobs.") cwriteln('waiting for Lsyncd to finish its jobs.')
posix.sleep(10) posix.sleep(10)
cwriteln("killing the Lsyncd daemon") cwriteln('killing the Lsyncd daemon')
posix.kill(pid) posix.kill(pid)
local _, exitmsg, lexitcode = posix.wait(lpid) local _, exitmsg, lexitcode = posix.wait(lpid)
cwriteln("Exitcode of Lsyncd = ", exitmsg, " ", lexitcode) cwriteln('Exitcode of Lsyncd = ',exitmsg,' ',lexitcode)
exitcode = os.execute("diff -r "..srcdir.." "..trgdir) exitcode = os.execute('diff -r '..srcdir..' '..trgdir)
cwriteln("Exitcode of diff = '", exitcode, "'") cwriteln('Exitcode of diff = "', exitcode, '"')
if exitcode ~= 0 then
os.exit(1)
else
os.exit(0)
end
if exitcode ~= 0 then os.exit(1) else os.exit(0) end