mod_network_restart, mod_modem_restart, mod_user_scripts: New option attempt_interval.

This commit is contained in:
gSpot
2025-06-08 16:15:58 +03:00
parent 75652f5e7d
commit 0e65c8a059
13 changed files with 288 additions and 180 deletions

View File

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

View File

@@ -22,11 +22,13 @@ config instance 'internet'
option mod_network_restart_disconnected_at_startup '0'
option mod_network_restart_dead_period '900'
option mod_network_restart_attempts '1'
option mod_network_restart_restart_timeout '0'
option mod_network_restart_attempt_interval '15'
option mod_network_restart_device_timeout '0'
option mod_modem_restart_enabled '0'
option mod_modem_restart_disconnected_at_startup '0'
option mod_modem_restart_dead_period '600'
option mod_modem_restart_attempts '1'
option mod_modem_restart_attempt_interval '15'
option mod_modem_restart_iface_timeout '0'
option mod_modem_restart_any_band '0'
option mod_public_ip_enabled '0'
@@ -45,9 +47,11 @@ config instance 'internet'
option mod_user_scripts_enabled '0'
option mod_user_scripts_alive_period '0'
option mod_user_scripts_up_script_attempts '1'
option mod_user_scripts_up_script_attempt_interval '15'
option mod_user_scripts_connected_at_startup '0'
option mod_user_scripts_dead_period '0'
option mod_user_scripts_down_script_attempts '1'
option mod_user_scripts_down_script_attempt_interval '15'
option mod_user_scripts_disconnected_at_startup '0'
option mod_regular_script_enabled '0'
option mod_regular_script_inet_state '2'

View File

@@ -2,25 +2,27 @@
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,
restartTimeout = 0,
status = nil,
_attemptsCounter = 0,
_deadCounter = 0,
_networkRestarted = false,
_ifaceRestarting = false,
_ifaceRestartCounter = 0,
_netIfaces = {},
_netDevices = {},
_netItemsNum = 0,
_disconnectedAtStartup = false,
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,
attemptInterval = 15,
deviceTimeout = 0,
status = nil,
_attemptsCounter = 0,
_attemptIntervalCounter = 0,
_deadCounter = 0,
_firstAttempt = true,
_ifaceRestarting = false,
_ifaceRestartCounter = 0,
_netIfaces = {},
_netDevices = {},
_netItemsNum = 0,
_disconnectedAtStartup = false,
}
function Module:toggleDevices(flag)
@@ -72,15 +74,17 @@ function Module:init(t)
self._netItemsNum = self._netItemsNum + 1
end
end
if t.attempts ~= nil then
self.attempts = tonumber(t.attempts)
end
if t.dead_period ~= nil then
self.deadPeriod = tonumber(t.dead_period)
end
if t.restart_timeout ~= nil then
self.restartTimeout = tonumber(t.restart_timeout)
if t.attempts ~= nil then
self.attempts = tonumber(t.attempts)
end
if t.attempt_interval ~= nil then
self.attemptInterval = tonumber(t.attempt_interval)
end
if t.device_timeout ~= nil then
self.deviceTimeout = tonumber(t.device_timeout)
end
if tonumber(t.disconnected_at_startup) == 1 then
self._disconnectedAtStartup = true
@@ -98,7 +102,7 @@ function Module:networkRestartFunc()
self.name, table.concat(self._netDevices, ", ")))
end
self:netItemsDown()
if self.restartTimeout < 1 then
if self.deviceTimeout < 1 then
self:netItemsUp()
else
self._ifaceRestarting = true
@@ -115,7 +119,7 @@ end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
if self._ifaceRestarting then
if self._ifaceRestartCounter >= self.restartTimeout then
if self._ifaceRestartCounter >= self.deviceTimeout then
self:netItemsUp()
self._ifaceRestarting = false
self._ifaceRestartCounter = 0
@@ -124,24 +128,25 @@ function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
end
else
if currentStatus == 1 then
if not self._networkRestarted then
if self._disconnectedAtStartup and (self.attempts == 0 or self._attemptsCounter < self.attempts) then
if self._deadCounter >= self.deadPeriod then
if self._disconnectedAtStartup and self._deadCounter >= self.deadPeriod then
if self.attempts == 0 or self._attemptsCounter < self.attempts then
if self._firstAttempt or self._attemptIntervalCounter >= self.attemptInterval then
self:networkRestartFunc()
self._networkRestarted = true
self._deadCounter = 0
self._attemptIntervalCounter = 0
self._firstAttempt = false
else
self._deadCounter = self._deadCounter + timeDiff
self._attemptIntervalCounter = self._attemptIntervalCounter + timeDiff
end
end
elseif inetChecked and (self.attempts == 0 or self._attemptsCounter < self.attempts) then
self:networkRestartFunc()
else
self._deadCounter = self._deadCounter + timeDiff
end
else
self._attemptsCounter = 0
self._deadCounter = 0
self._disconnectedAtStartup = true
self._networkRestarted = false
self._attemptsCounter = 0
self._attemptIntervalCounter = 0
self._deadCounter = 0
self._disconnectedAtStartup = true
self._firstAttempt = true
end
self._ifaceRestartCounter = 0
end

View File

@@ -2,27 +2,31 @@
local unistd = require("posix.unistd")
local Module = {
name = "mod_user_scripts",
runPrio = 80,
config = {},
syslog = function(level, msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 0,
alivePeriod = 0,
upScript = "",
downScript = "",
upScriptAttempts = 1,
downScriptAttempts = 1,
status = nil,
_deadCounter = 0,
_aliveCounter = 0,
_upScriptAttemptsCounter = 0,
_downScriptAttemptsCounter = 0,
_upScriptExecuted = false,
_downScriptExecuted = false,
_disconnectedAtStartup = false,
_connectedAtStartup = false,
name = "mod_user_scripts",
runPrio = 80,
config = {},
syslog = function(level, msg) return true end,
writeValue = function(filePath, str) return false end,
readValue = function(filePath) return nil end,
deadPeriod = 0,
alivePeriod = 0,
upScript = "",
downScript = "",
upScriptAttempts = 1,
upScriptAttemptInterval = 15,
downScriptAttempts = 1,
downScriptAttemptInterval = 15,
status = nil,
_deadCounter = 0,
_aliveCounter = 0,
_upScriptAttemptsCounter = 0,
_upScriptAttemptIntervalCounter = 0,
_downScriptAttemptsCounter = 0,
_downScriptAttemptIntervalCounter = 0,
_upScriptFirstAttempt = true,
_downScriptFirstAttempt = true,
_disconnectedAtStartup = false,
_connectedAtStartup = false,
}
function Module:runExternalScript(scriptPath)
@@ -41,9 +45,15 @@ function Module:init(t)
if t.up_script_attempts ~= nil then
self.upScriptAttempts = tonumber(t.up_script_attempts)
end
if t.up_script_attempt_interval ~= nil then
self.upScriptAttemptInterval = tonumber(t.up_script_attempt_interval)
end
if t.down_script_attempts ~= nil then
self.downScriptAttempts = tonumber(t.down_script_attempts)
end
if t.down_script_attempt_interval ~= nil then
self.downScriptAttemptInterval = tonumber(t.down_script_attempt_interval)
end
if self.config.configDir then
self.upScript = string.format(
"%s/up-script.%s", self.config.configDir, self.config.serviceConfig.instance)
@@ -74,41 +84,44 @@ end
function Module:run(currentStatus, lastStatus, timeDiff, timeNow, inetChecked)
if currentStatus == 1 then
self._upScriptAttemptsCounter = 0
self._aliveCounter = 0
self._connectedAtStartup = true
self._upScriptExecuted = false
if not self._downScriptExecuted then
if self._disconnectedAtStartup and (self.downScriptAttempts == 0 or self._downScriptAttemptsCounter < self.downScriptAttempts) then
if self._deadCounter >= self.deadPeriod then
self._upScriptAttemptsCounter = 0
self._upScriptAttemptIntervalCounter = 0
self._aliveCounter = 0
self._connectedAtStartup = true
self._upScriptFirstAttempt = true
if self._disconnectedAtStartup and self._deadCounter >= self.deadPeriod then
if self.downScriptAttempts == 0 or self._downScriptAttemptsCounter < self.downScriptAttempts then
if self._downScriptFirstAttempt or self._downScriptAttemptIntervalCounter >= self.downScriptAttemptInterval then
self:runDownScriptFunc()
self._downScriptExecuted = true
self._deadCounter = 0
self._downScriptAttemptIntervalCounter = 0
self._downScriptFirstAttempt = false
else
self._deadCounter = self._deadCounter + timeDiff
self._downScriptAttemptIntervalCounter = self._downScriptAttemptIntervalCounter + timeDiff
end
end
elseif inetChecked and (self.downScriptAttempts == 0 or self._downScriptAttemptsCounter < self.downScriptAttempts) then
self:runDownScriptFunc()
else
self._deadCounter = self._deadCounter + timeDiff
end
elseif currentStatus == 0 then
self._downScriptAttemptsCounter = 0
self._deadCounter = 0
self._disconnectedAtStartup = true
self._downScriptExecuted = false
if not self._upScriptExecuted then
if self._connectedAtStartup and (self.upScriptAttempts == 0 or self._upScriptAttemptsCounter < self.upScriptAttempts) then
if self._aliveCounter >= self.alivePeriod then
self._downScriptAttemptsCounter = 0
self._downScriptAttemptIntervalCounter = 0
self._deadCounter = 0
self._disconnectedAtStartup = true
self._downScriptFirstAttempt = true
if self._connectedAtStartup and self._aliveCounter >= self.alivePeriod then
if self.upScriptAttempts == 0 or self._upScriptAttemptsCounter < self.upScriptAttempts then
if self._upScriptFirstAttempt or self._upScriptAttemptIntervalCounter >= self.upScriptAttemptInterval then
self:runUpScriptFunc()
self._upScriptExecuted = true
self._aliveCounter = 0
self._upScriptAttemptIntervalCounter = 0
self._upScriptFirstAttempt = false
else
self._aliveCounter = self._aliveCounter + timeDiff
self._upScriptAttemptIntervalCounter = self._upScriptAttemptIntervalCounter + timeDiff
end
end
elseif inetChecked and (self.upScriptAttempts == 0 or self._upScriptAttemptsCounter < self.upScriptAttempts) then
self:runUpScriptFunc()
else
self._aliveCounter = self._aliveCounter + timeDiff
end
end
end