making settings{} a function

This commit is contained in:
Axel Kittenberger 2012-10-08 09:10:03 +02:00
parent f9231a11b2
commit a82f4da7cd
2 changed files with 81 additions and 55 deletions

View File

@ -2571,7 +2571,7 @@ main1( int argc, char *argv[] )
lua_settable ( L, -3 ); lua_settable ( L, -3 );
} }
if( lua_pcall( L, 2, 1, -3 ) ) if( lua_pcall( L, 2, 1, -4 ) )
{ exit( -1 ); } { exit( -1 ); }
if( first_time ) if( first_time )

View File

@ -70,6 +70,17 @@ local processCount = 0
-- --
local clSettings = { } local clSettings = { }
--
-- Settings specified by config scripts.
--
local uSettings = { }
--
-- A copy of the settings function to see if the
-- user script replaced the settings() by a table
-- ( pre Lsyncd 2.1 style )
--
local settingsSafe
--============================================================================ --============================================================================
-- Lsyncd Prototypes -- Lsyncd Prototypes
@ -1926,8 +1937,8 @@ local Sync = ( function( )
for _, d in Queue.qpairs( self.delays ) do for _, d in Queue.qpairs( self.delays ) do
-- if reached the global limit return -- if reached the global limit return
if settings.maxProcesses and if uSettings.maxProcesses and
processCount >= settings.maxProcesses processCount >= uSettings.maxProcesses
then then
log('Alarm', 'at global process limit.') log('Alarm', 'at global process limit.')
return return
@ -2278,20 +2289,16 @@ local Syncs = ( function( )
} }
-- Lets settings override these values. -- Lets settings override these values.
if settings then for _, v in ipairs( inheritSettings ) do
for _, v in ipairs( inheritSettings ) do if uSettings[ v ] then
if settings[ v ] then config[ v ] = uSettings[ v ]
config[ v ] = settings[ v ]
end
end end
end end
-- Lets commandline override these values. -- Lets commandline override these values.
if clSettings then for _, v in ipairs( inheritSettings ) do
for _, v in ipairs( inheritSettings ) do if clSettings[ v ] then
if clSettings[ v ] then config[ v ] = clSettings[ v ]
config[ v ] = clSettings[ v ]
end
end end
end end
@ -2352,14 +2359,9 @@ local Syncs = ( function( )
terminate( -1 ) terminate( -1 )
end end
-- loads a default value for an option if not existent
if not settings then
settings = {}
end
-- the monitor to use -- the monitor to use
config.monitor = config.monitor =
settings.monitor or uSettings.monitor or
config.monitor or config.monitor or
Monitors.default( ) Monitors.default( )
@ -2520,7 +2522,8 @@ local Inotify = ( function( )
end end
-- registers the watch -- registers the watch
local inotifyMode = ( settings and settings.inotifyMode ) or ''; local inotifyMode = ( uSettings and uSettings.inotifyMode ) or '';
local wd = lsyncd.inotify.addwatch( path, inotifyMode) ; local wd = lsyncd.inotify.addwatch( path, inotifyMode) ;
if wd < 0 then if wd < 0 then
@ -3194,9 +3197,10 @@ local StatusFile = ( function( )
' )' ' )'
) )
--
-- takes care not write too often -- takes care not write too often
--
if settings.statusInterval > 0 then if uSettings.statusInterval > 0 then
-- already waiting? -- already waiting?
if alarm and timestamp < alarm then if alarm and timestamp < alarm then
@ -3213,8 +3217,10 @@ local StatusFile = ( function( )
-- determines when a next write will be possible -- determines when a next write will be possible
if not alarm then if not alarm then
local nextWrite = local nextWrite =
lastWritten and timestamp + settings.statusInterval lastWritten and timestamp +
uSettings.statusInterval
if nextWrite and timestamp < nextWrite then if nextWrite and timestamp < nextWrite then
log( log(
@ -3234,13 +3240,13 @@ local StatusFile = ( function( )
log( 'Statusfile', 'writing now' ) log( 'Statusfile', 'writing now' )
local f, err = io.open( settings.statusFile, 'w' ) local f, err = io.open( uSettings.statusFile, 'w' )
if not f then if not f then
log( log(
'Error', 'Error',
'Cannot open status file "' .. 'Cannot open status file "' ..
settings.statusFile .. uSettings.statusFile ..
'" :' .. '" :' ..
err err
) )
@ -3455,8 +3461,8 @@ function runner.cycle(
-- not at global limit -- not at global limit
-- --
if if
not settings.maxProcesses or not uSettings.maxProcesses or
processCount < settings.maxProcesses processCount < uSettings.maxProcesses
then then
local start = Syncs.getRound( ) local start = Syncs.getRound( )
@ -3479,7 +3485,7 @@ function runner.cycle(
UserAlarms.invoke( timestamp ) UserAlarms.invoke( timestamp )
if settings.statusFile then if uSettings.statusFile then
StatusFile.write( timestamp ) StatusFile.write( timestamp )
end end
@ -3548,6 +3554,7 @@ function runner.configure( args, monitors )
Monitors.initialize( monitors ) Monitors.initialize( monitors )
--
-- a list of all valid options -- a list of all valid options
-- --
-- first paramter is the number of parameters an option takes -- first paramter is the number of parameters an option takes
@ -3768,10 +3775,16 @@ end
-- --
function runner.initialize( firstTime ) function runner.initialize( firstTime )
-- if settings ~= settingsSafe then
-- creates settings if user didnt log(
-- 'Warn',
settings = settings or { } 'settings = { ... } is deprecated.\n'..
' please use settings{ ... } (without the equal sign)'
)
uSettings = settings
end
-- --
-- From this point on, no globals may be created anymore -- From this point on, no globals may be created anymore
@ -3781,9 +3794,12 @@ function runner.initialize( firstTime )
-- --
-- copies simple settings with numeric keys to 'key = true' settings. -- copies simple settings with numeric keys to 'key = true' settings.
-- --
for k, v in ipairs( settings ) do -- FIXME this can be removed when
-- Lsyncd 2.0.x backwards compatibility is dropped
--
for k, v in ipairs( uSettings ) do
if settings[ v ] then if uSettings[ v ] then
log( log(
'Error', 'Error',
'Double setting "' .. v.. '"' 'Double setting "' .. v.. '"'
@ -3791,7 +3807,8 @@ function runner.initialize( firstTime )
os.exit( -1 ) os.exit( -1 )
end end
settings[ v ]= true uSettings[ v ]= true
end end
-- --
@ -3799,7 +3816,7 @@ function runner.initialize( firstTime )
-- --
for k, v in pairs( clSettings ) do for k, v in pairs( clSettings ) do
if k ~= 'syncs' then if k ~= 'syncs' then
settings[ k ] = v uSettings[ k ] = v
end end
end end
@ -3807,7 +3824,7 @@ function runner.initialize( firstTime )
-- implicitly forces 'insist' on Lsyncd resets. -- implicitly forces 'insist' on Lsyncd resets.
-- --
if not firstTime then if not firstTime then
settings.insist = true uSettings.insist = true
end end
-- --
@ -3847,31 +3864,31 @@ function runner.initialize( firstTime )
end end
if settings.nodaemon then if uSettings.nodaemon then
lsyncd.configure( 'nodaemon' ) lsyncd.configure( 'nodaemon' )
end end
if settings.logfile then if uSettings.logfile then
lsyncd.configure( 'logfile', settings.logfile ) lsyncd.configure( 'logfile', uSettings.logfile )
end end
if settings.logident then if uSettings.logident then
lsyncd.configure( 'logident', settings.logident ) lsyncd.configure( 'logident', uSettings.logident )
end end
if settings.logfacility then if uSettings.logfacility then
lsyncd.configure( 'logfacility', settings.logfacility ) lsyncd.configure( 'logfacility', uSettings.logfacility )
end end
if settings.pidfile then if uSettings.pidfile then
lsyncd.configure( 'pidfile', settings.pidfile ) lsyncd.configure( 'pidfile', uSettings.pidfile )
end end
-- --
-- Transfers some defaults to settings -- Transfers some defaults to uSettings
-- --
if settings.statusInterval == nil then if uSettings.statusInterval == nil then
settings.statusInterval = default.statusInterval uSettings.statusInterval = default.statusInterval
end end
-- makes sure the user gave Lsyncd anything to do -- makes sure the user gave Lsyncd anything to do
@ -3991,8 +4008,8 @@ function runner.getAlarm( )
-- but only if the global process limit is not yet reached. -- but only if the global process limit is not yet reached.
-- --
if if
not settings.maxProcesses or not uSettings.maxProcesses or
processCount < settings.maxProcesses processCount < uSettings.maxProcesses
then then
for _, s in Syncs.iwalk( ) do for _, s in Syncs.iwalk( ) do
checkAlarm( s:getAlarm ( )) checkAlarm( s:getAlarm ( ))
@ -4182,8 +4199,8 @@ function spawn(
processCount = processCount + 1 processCount = processCount + 1
if if
settings.maxProcesses and uSettings.maxProcesses and
processCount > settings.maxProcesses processCount > uSettings.maxProcesses
then then
error( 'Spawned too much processes!' ) error( 'Spawned too much processes!' )
end end
@ -4281,9 +4298,18 @@ function string.ends( String, End )
end end
-- --
-- Provides a default empty settings table. -- The Lsyncd 2.1 settings call
-- --
settings = { } function settings( a1 )
for k, v in pairs( a1 ) do
if type( k ) ~= 'number' then
uSettings[ k ] = v
else
uSettings[ v ] = true
end
end
end
settingsSafe = settings
-- --
-- Returns the core the runners function interface. -- Returns the core the runners function interface.