modules: bug fixes, refactoring

This commit is contained in:
gSpot
2025-02-21 17:36:43 +03:00
parent 6c19812db5
commit 8ad9c5a086
13 changed files with 166 additions and 132 deletions

View File

@@ -5,7 +5,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=internet-detector
PKG_VERSION:=1.4.1
PKG_VERSION:=1.4.2
PKG_RELEASE:=1
PKG_MAINTAINER:=gSpot <https://github.com/gSpotx2f/luci-app-internet-detector>

View File

@@ -2,19 +2,21 @@
local unistd = require("posix.unistd")
local Module = {
name = "mod_network_restart",
runPrio = 30,
config = {},
syslog = function(level, msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 900,
attempts = 1,
iface = nil,
restartTimeout = 0,
status = nil,
_attemptsCounter = 0,
_deadCounter = 0,
name = "mod_network_restart",
runPrio = 30,
config = {},
syslog = function(level, msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 900,
attempts = 1,
iface = nil,
restartTimeout = 0,
status = nil,
_attemptsCounter = 0,
_deadCounter = 0,
_ifaceRestarting = false,
_ifaceRestartCounter = 0,
}
function Module:toggleFunc(flag)
@@ -74,32 +76,47 @@ function Module:init(t)
if t.restart_timeout ~= nil then
self.restartTimeout = tonumber(t.restart_timeout)
end
self._attemptsCounter = self.attempts
end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then
if self.attempts == 0 or self._attemptsCounter < self.attempts then
if self._deadCounter >= self.deadPeriod then
if self.iface then
self.syslog("info", string.format(
"%s: restarting network interface '%s'", self.name, self.iface))
self:ifaceDown()
unistd.sleep(self.restartTimeout)
self:ifaceUp()
else
self.syslog("info", string.format(
"%s: restarting network", self.name))
self:networkRestart()
end
self._deadCounter = 0
self._attemptsCounter = self._attemptsCounter + 1
else
self._deadCounter = self._deadCounter + timeDiff
end
if self._ifaceRestarting then
if self._ifaceRestartCounter >= self.restartTimeout then
self:ifaceUp()
self._ifaceRestarting = false
self._ifaceRestartCounter = 0
else
self._ifaceRestartCounter = self._ifaceRestartCounter + timeDiff
end
else
self._attemptsCounter = 0
self._deadCounter = 0
if currentStatus == 1 then
if self._attemptsCounter < self.attempts then
if self._deadCounter >= self.deadPeriod then
if self.iface then
self.syslog("info", string.format(
"%s: restarting network interface '%s'", self.name, self.iface))
self:ifaceDown()
if self.restartTimeout < 1 then
self:ifaceUp()
else
self._ifaceRestarting = true
end
else
self.syslog("info", string.format(
"%s: restarting network", self.name))
self:networkRestart()
end
self._deadCounter = 0
self._attemptsCounter = self._attemptsCounter + 1
else
self._deadCounter = self._deadCounter + timeDiff
end
end
else
self._attemptsCounter = 0
self._deadCounter = 0
end
self._ifaceRestartCounter = 0
end
end

View File

@@ -2,16 +2,18 @@
local unistd = require("posix.unistd")
local Module = {
name = "mod_reboot",
runPrio = 20,
config = {},
syslog = function(level, msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 3600,
forceRebootDelay = 300,
status = nil,
_deadCounter = 0,
name = "mod_reboot",
runPrio = 20,
config = {},
syslog = function(level, msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 3600,
forceRebootDelay = 300,
antiBootloopDelay = 300,
status = nil,
_deadCounter = 0,
_rebooted = true,
}
function Module:rebootDevice()
@@ -36,15 +38,17 @@ end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then
if self._deadCounter >= self.deadPeriod then
self:rebootDevice()
self._deadCounter = 0
else
self._deadCounter = self._deadCounter + timeDiff
if not self._rebooted then
if timeNow >= self.antiBootloopDelay and self._deadCounter >= self.deadPeriod then
self:rebootDevice()
self._rebooted = true
else
self._deadCounter = self._deadCounter + timeDiff
end
end
else
self._deadCounter = 0
self._rebooted = false
end
end

View File

@@ -15,8 +15,8 @@ local Module = {
status = nil,
_deadCounter = 0,
_aliveCounter = 0,
_upScriptExecuted = true,
_downScriptExecuted = true,
_upScriptExecuted = true,
}
function Module:runExternalScript(scriptPath)
@@ -42,23 +42,23 @@ end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow)
if currentStatus == 1 then
self._aliveCounter = 0
self._downScriptExecuted = false
if not self._upScriptExecuted then
self._aliveCounter = 0
self._upScriptExecuted = false
if not self._downScriptExecuted then
if self._deadCounter >= self.deadPeriod then
self:runExternalScript(self.downScript)
self._upScriptExecuted = true
self._downScriptExecuted = true
else
self._deadCounter = self._deadCounter + timeDiff
end
end
else
self._deadCounter = 0
self._upScriptExecuted = false
if not self._downScriptExecuted then
self._deadCounter = 0
self._downScriptExecuted = false
if not self._upScriptExecuted then
if self._aliveCounter >= self.alivePeriod then
self:runExternalScript(self.upScript)
self._downScriptExecuted = true
self._upScriptExecuted = true
else
self._aliveCounter = self._aliveCounter + timeDiff
end