mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-06-07 19:00:51 +00:00
back the original alarm interface
This commit is contained in:
parent
5bc66eb1f9
commit
84bc9de865
|
@ -5,30 +5,22 @@
|
||||||
-- how user custom alarms can be now. It will log
|
-- how user custom alarms can be now. It will log
|
||||||
-- "Beep!" every 5 seconds.
|
-- "Beep!" every 5 seconds.
|
||||||
--
|
--
|
||||||
|
settings.nodaemon = true
|
||||||
|
|
||||||
lalarm = {
|
local function noAction (inlet)
|
||||||
init = function(inlet)
|
-- just discard any events that happes in source dir.
|
||||||
-- 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())
|
inlet.discardEvent(inlet.getEvent())
|
||||||
end
|
end
|
||||||
}
|
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Lsyncd needs to watch something, altough in this minimal example
|
-- Adds a watch to some not so large directory for this example.
|
||||||
-- it isnt used.
|
local in1 = sync{source="/usr/local/etc/", action = noAction }
|
||||||
sync{source="/usr/local/etc/", lalarm }
|
|
||||||
|
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"})
|
||||||
|
|
||||||
|
|
116
lsyncd.lua
116
lsyncd.lua
|
@ -988,27 +988,6 @@ local Sync = (function()
|
||||||
--
|
--
|
||||||
local nextDefaultName = 1
|
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.
|
-- Adds an exclude.
|
||||||
--
|
--
|
||||||
|
@ -1253,27 +1232,22 @@ local Sync = (function()
|
||||||
-- Returns the nearest alarm for this Sync.
|
-- Returns the nearest alarm for this Sync.
|
||||||
--
|
--
|
||||||
local function getAlarm(self)
|
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
|
-- first checks if more processses could be spawned
|
||||||
if self.processes:size() < self.config.maxProcesses then
|
if self.processes:size() < self.config.maxProcesses then
|
||||||
-- finds the nearest delay waiting to be spawned
|
-- finds the nearest delay waiting to be spawned
|
||||||
for _, d in ipairs(self.delays) do
|
for _, d in ipairs(self.delays) do
|
||||||
if d.status == "wait" then
|
if d.status == "wait" then
|
||||||
alarm = d.alarm
|
return alarm
|
||||||
break
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- return the earlier alarm, user alarms or events
|
-- nothing to spawn
|
||||||
if #self.alarms == 0 then
|
return nil
|
||||||
return alarm
|
|
||||||
elseif not alarm or self.alarms[1].timestamp < alarm then
|
|
||||||
return self.alarms[1].timestamp
|
|
||||||
else
|
|
||||||
return alarm
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -1341,17 +1315,6 @@ local Sync = (function()
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
-----
|
-----
|
||||||
|
@ -1420,7 +1383,6 @@ local Sync = (function()
|
||||||
local function new(config)
|
local function new(config)
|
||||||
local s = {
|
local s = {
|
||||||
-- fields
|
-- fields
|
||||||
alarms = {},
|
|
||||||
config = config,
|
config = config,
|
||||||
delays = CountArray.new(),
|
delays = CountArray.new(),
|
||||||
source = config.source,
|
source = config.source,
|
||||||
|
@ -1428,7 +1390,6 @@ local Sync = (function()
|
||||||
excludes = Excludes.new(),
|
excludes = Excludes.new(),
|
||||||
|
|
||||||
-- functions
|
-- functions
|
||||||
addAlarm = addAlarm,
|
|
||||||
addBlanketDelay = addBlanketDelay,
|
addBlanketDelay = addBlanketDelay,
|
||||||
addExclude = addExclude,
|
addExclude = addExclude,
|
||||||
collect = collect,
|
collect = collect,
|
||||||
|
@ -2231,6 +2192,60 @@ local StatusFile = (function()
|
||||||
return {write = write, getAlarm = getAlarm}
|
return {write = write, getAlarm = getAlarm}
|
||||||
end)()
|
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.
|
-- lsyncd runner plugs. These functions will be called from core.
|
||||||
--============================================================================
|
--============================================================================
|
||||||
|
@ -2313,6 +2328,8 @@ function runner.cycle(timestamp)
|
||||||
s:invokeActions(timestamp)
|
s:invokeActions(timestamp)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
UserAlarms.invoke(timestamp)
|
||||||
|
|
||||||
if settings.statusFile then
|
if settings.statusFile then
|
||||||
StatusFile.write(timestamp)
|
StatusFile.write(timestamp)
|
||||||
end
|
end
|
||||||
|
@ -2624,6 +2641,8 @@ function runner.getAlarm()
|
||||||
end
|
end
|
||||||
-- checks if a statusfile write has been delayed
|
-- checks if a statusfile write has been delayed
|
||||||
checkAlarm(StatusFile.getAlarm())
|
checkAlarm(StatusFile.getAlarm())
|
||||||
|
-- checks for an userAlarm
|
||||||
|
checkAlarm(UserAlarms.getAlarm())
|
||||||
|
|
||||||
log("Debug", "getAlarm returns: ",alarm)
|
log("Debug", "getAlarm returns: ",alarm)
|
||||||
return alarm
|
return alarm
|
||||||
|
@ -2744,6 +2763,13 @@ function spawnShell(agent, command, ...)
|
||||||
return spawn(agent, "/bin/sh", "-c", command, "/bin/sh", ...)
|
return spawn(agent, "/bin/sh", "-c", command, "/bin/sh", ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----
|
||||||
|
-- Calls func at timestamp.
|
||||||
|
-- Use now() to receive current timestamp
|
||||||
|
-- add seconds with '+' to it)
|
||||||
|
--
|
||||||
|
alarm = UserAlarms.alarm
|
||||||
|
|
||||||
-----
|
-----
|
||||||
-- Comfort routine also for user.
|
-- Comfort routine also for user.
|
||||||
-- Returns true if 'String' starts with 'Start'
|
-- Returns true if 'String' starts with 'Start'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user