2022-01-17 17:54:44 -07:00
|
|
|
#!/usr/bin/lua
|
|
|
|
--[[
|
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Part of AREDN® -- Used for creating Amateur Radio Emergency Data Networks
|
2022-01-17 17:54:44 -07:00
|
|
|
Copyright (C) 2021 Tim Wilkinson
|
|
|
|
See Contributors file for additional contributors
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation version 3 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Additional Terms:
|
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Additional use restrictions exist on the AREDN® trademark and logo.
|
2022-01-17 17:54:44 -07:00
|
|
|
See AREDNLicense.txt for more info.
|
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Attributions to the AREDN® Project must be retained in the source code.
|
2022-01-17 17:54:44 -07:00
|
|
|
If importing this code into a new or existing project attribution
|
2024-05-29 01:45:25 -06:00
|
|
|
to the AREDN® project must be added to the source code.
|
2022-01-17 17:54:44 -07:00
|
|
|
|
|
|
|
You must not misrepresent the origin of the material contained within.
|
|
|
|
|
|
|
|
Modified versions must be modified to attribute to the original source
|
|
|
|
and be marked in reasonable ways as differentiate it from the original
|
|
|
|
version
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
package.path = package.path .. ";/usr/local/bin/?.lua"
|
|
|
|
|
|
|
|
require("uci")
|
|
|
|
require("nixio")
|
|
|
|
require("aredn.utils")
|
|
|
|
require("iwinfo")
|
|
|
|
require("aredn.hardware")
|
2024-04-01 23:15:45 -06:00
|
|
|
require("aredn.olsr")
|
2022-10-13 11:07:36 -06:00
|
|
|
require("luci.jsonc")
|
2023-12-19 00:07:52 -07:00
|
|
|
require("ubus")
|
2022-01-17 17:54:44 -07:00
|
|
|
|
|
|
|
-- aggressive gc on low memory devices
|
2022-10-13 11:07:36 -06:00
|
|
|
if nixio.sysinfo().totalram < 32 * 1024 * 1024 then
|
2022-01-17 17:54:44 -07:00
|
|
|
collectgarbage("setstepmul", 1000)
|
|
|
|
end
|
|
|
|
|
|
|
|
function wait_for_ticks(ticks)
|
2024-05-15 19:09:07 -06:00
|
|
|
if ticks <= 0 then
|
|
|
|
coroutine.yield(0)
|
|
|
|
else
|
|
|
|
local when = nixio.sysinfo().uptime + ticks
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
if ticks > 0 then
|
|
|
|
coroutine.yield(ticks)
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
ticks = when - nixio.sysinfo().uptime
|
2023-12-19 13:07:11 -07:00
|
|
|
end
|
|
|
|
end
|
2022-01-17 17:54:44 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function exit_app()
|
|
|
|
coroutine.yield('exit')
|
|
|
|
end
|
|
|
|
|
2022-03-24 20:52:32 -06:00
|
|
|
-- Load management tasks
|
|
|
|
local tasks = {}
|
|
|
|
for name in nixio.fs.dir("/usr/local/bin/mgr")
|
|
|
|
do
|
|
|
|
local task = name:match("^(.+)%.lua$")
|
|
|
|
if task then
|
2022-12-22 13:22:49 -07:00
|
|
|
tasks[#tasks + 1] = { name = task, app = require("mgr." .. task) }
|
2022-03-24 20:52:32 -06:00
|
|
|
end
|
|
|
|
end
|
2022-01-17 17:54:44 -07:00
|
|
|
|
|
|
|
for i, task in ipairs(tasks)
|
|
|
|
do
|
|
|
|
task.routine = coroutine.create(task.app)
|
|
|
|
task.time = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
for i, task in ipairs(tasks)
|
|
|
|
do
|
|
|
|
if task.time <= os.time() then
|
2024-05-06 00:11:57 -06:00
|
|
|
nixio.openlog("manager." .. task.name)
|
2022-01-17 17:54:44 -07:00
|
|
|
local status, newdelay = coroutine.resume(task.routine)
|
|
|
|
if not status then
|
2024-05-06 00:11:57 -06:00
|
|
|
nixio.syslog("err", newdelay)
|
2022-01-17 17:54:44 -07:00
|
|
|
task.routine = coroutine.create(task.app)
|
|
|
|
task.time = 120 + os.time() -- 2 minute restart delay
|
|
|
|
elseif not newdelay then
|
|
|
|
task.time = 60 + os.time() -- 1 minute default delay
|
|
|
|
elseif newdelay == "exit" then
|
|
|
|
task.routine = null
|
|
|
|
task.time = math.huge
|
2024-05-06 00:11:57 -06:00
|
|
|
nixio.syslog("notice", "Terminating manager task: " .. task.name)
|
2022-01-17 17:54:44 -07:00
|
|
|
else
|
|
|
|
task.time = newdelay + os.time()
|
|
|
|
end
|
2024-05-06 00:11:57 -06:00
|
|
|
nixio.closelog()
|
2022-01-17 17:54:44 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(tasks, function(a,b) return a.time < b.time end)
|
|
|
|
local delay = tasks[1].time - os.time()
|
|
|
|
if delay > 0 then
|
2022-10-08 14:30:39 -06:00
|
|
|
collectgarbage("collect")
|
|
|
|
delay = tasks[1].time - os.time()
|
|
|
|
if delay > 0 then
|
|
|
|
nixio.nanosleep(delay, 0)
|
|
|
|
end
|
2022-01-17 17:54:44 -07:00
|
|
|
end
|
|
|
|
end
|