back the original alarm interface

This commit is contained in:
Axel Kittenberger 2010-11-30 23:14:17 +00:00
parent 5bc66eb1f9
commit 84bc9de865
2 changed files with 87 additions and 69 deletions

View File

@ -5,30 +5,22 @@
-- how user custom alarms can be now. It will log
-- "Beep!" every 5 seconds.
--
settings.nodaemon = true
lalarm = {
init = function(inlet)
-- creates the first alarm in 5 seconds from now.
inlet.alarm(now() + 5, "Beep")
end,
-- called when alarms ring
alarm = function(inlet, timestamp, extra)
log("Normal", extra)
spawn(inlet.createBlanketEvent(), "/bin/echo", "hello")
-- creates a new alarm in 5 seconds after this one rang
inlet.alarm(timestamp + 5, extra)
end,
action = function(inlet)
-- just discard anything that happes in source dir.
inlet.discardEvent(inlet.getEvent())
end
}
local function noAction (inlet)
-- just discard any events that happes in source dir.
inlet.discardEvent(inlet.getEvent())
end
-----
-- Lsyncd needs to watch something, altough in this minimal example
-- it isnt used.
sync{source="/usr/local/etc/", lalarm }
-- Adds a watch to some not so large directory for this example.
local in1 = sync{source="/usr/local/etc/", action = noAction }
local function myAlarm(timestamp, extra)
log("Normal", extra.message)
spawn(extra.inlet.createBlanketEvent(), "/bin/echo", extra.message)
alarm(timestamp + 5, myAlarm, extra)
end
alarm(now() + 5, myAlarm, {inlet = in1, message = "Beep"})

View File

@ -988,27 +988,6 @@ local Sync = (function()
--
local nextDefaultName = 1
-----
-- Adds an user alarm.
-- Calls the user function at timestamp.
--
local function addAlarm(self, timestamp, extra)
local idx
for k, v in ipairs(self.alarms) do
if timestamp < v.timestamp then
idx = k
break
end
end
local a = {timestamp = timestamp,
extra = extra}
if idx then
table.insert(self.alarms, idx, a)
else
table.insert(self.alarms, a)
end
end
-----
-- Adds an exclude.
--
@ -1253,27 +1232,22 @@ local Sync = (function()
-- Returns the nearest alarm for this Sync.
--
local function getAlarm(self)
local alarm
if self.processes:size() >= self.config.maxProcesses then
return nil
end
-- first checks if more processses could be spawned
if self.processes:size() < self.config.maxProcesses then
-- finds the nearest delay waiting to be spawned
for _, d in ipairs(self.delays) do
if d.status == "wait" then
alarm = d.alarm
break
return alarm
end
end
end
-- return the earlier alarm, user alarms or events
if #self.alarms == 0 then
return alarm
elseif not alarm or self.alarms[1].timestamp < alarm then
return self.alarms[1].timestamp
else
return alarm
end
-- nothing to spawn
return nil
end
@ -1341,17 +1315,6 @@ local Sync = (function()
end
end
end
-- invokes user alarms
while #self.alarms > 0 and self.alarms[1].timestamp <= timestamp do
if type(self.config.alarm) == "function" then
self.config.alarm(self.inlet,
self.alarms[1].timestamp, self.alarms[1].extra)
else
log("Error", "registered alarm, but there is no alarm function")
end
table.remove(self.alarms, 1)
end
end
-----
@ -1420,7 +1383,6 @@ local Sync = (function()
local function new(config)
local s = {
-- fields
alarms = {},
config = config,
delays = CountArray.new(),
source = config.source,
@ -1428,7 +1390,6 @@ local Sync = (function()
excludes = Excludes.new(),
-- functions
addAlarm = addAlarm,
addBlanketDelay = addBlanketDelay,
addExclude = addExclude,
collect = collect,
@ -2231,6 +2192,60 @@ local StatusFile = (function()
return {write = write, getAlarm = getAlarm}
end)()
------
-- Lets the userscript make its own alarms
--
local UserAlarms = (function()
local alarms = {}
-----
-- Calls the user function at timestamp
--
local function alarm(timestamp, func, extra)
local idx
for k, v in ipairs(alarms) do
if timestamp < v.timestamp then
idx = k
break
end
end
local a = {timestamp = timestamp,
func = func,
extra = extra}
if idx then
table.insert(alarms, idx, a)
else
table.insert(alarms, a)
end
end
----
-- Retrieves the nearest alarm
--
local function getAlarm()
if #alarms == 0 then
return nil
else
return alarms[1].timestamp
end
end
-----
-- Calls user alarms
--
local function invoke(timestamp)
while #alarms > 0 and alarms[1].timestamp <= timestamp do
alarms[1].func(alarms[1].timestamp, alarms[1].extra)
table.remove(alarms, 1)
end
end
-- public interface
return { alarm = alarm,
getAlarm = getAlarm,
invoke = invoke }
end)()
--============================================================================
-- lsyncd runner plugs. These functions will be called from core.
--============================================================================
@ -2312,7 +2327,9 @@ function runner.cycle(timestamp)
for _, s in Syncs.iwalk() do
s:invokeActions(timestamp)
end
UserAlarms.invoke(timestamp)
if settings.statusFile then
StatusFile.write(timestamp)
end
@ -2624,6 +2641,8 @@ function runner.getAlarm()
end
-- checks if a statusfile write has been delayed
checkAlarm(StatusFile.getAlarm())
-- checks for an userAlarm
checkAlarm(UserAlarms.getAlarm())
log("Debug", "getAlarm returns: ",alarm)
return alarm
@ -2744,6 +2763,13 @@ function spawnShell(agent, command, ...)
return spawn(agent, "/bin/sh", "-c", command, "/bin/sh", ...)
end
-----
-- Calls func at timestamp.
-- Use now() to receive current timestamp
-- add seconds with '+' to it)
--
alarm = UserAlarms.alarm
-----
-- Comfort routine also for user.
-- Returns true if 'String' starts with 'Start'