2022-01-17 17:54:44 -07:00
|
|
|
#!/usr/bin/lua
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
|
|
|
|
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:
|
|
|
|
|
|
|
|
Additional use restrictions exist on the AREDN(TM) trademark and logo.
|
|
|
|
See AREDNLicense.txt for more info.
|
|
|
|
|
|
|
|
Attributions to the AREDN Project must be retained in the source code.
|
|
|
|
If importing this code into a new or existing project attribution
|
|
|
|
to the AREDN project must be added to the source code.
|
|
|
|
|
|
|
|
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")
|
|
|
|
require("aredn.log")
|
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)
|
2023-12-19 13:07:11 -07:00
|
|
|
local when = nixio.sysinfo().uptime + ticks
|
|
|
|
while true
|
|
|
|
do
|
2023-12-20 01:25:12 -07:00
|
|
|
if ticks >= 0 then
|
2023-12-19 13:07:11 -07:00
|
|
|
coroutine.yield(ticks)
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
2023-12-20 01:25:12 -07:00
|
|
|
ticks = when - nixio.sysinfo().uptime
|
2023-12-19 13:07:11 -07:00
|
|
|
end
|
2022-01-17 17:54:44 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function exit_app()
|
|
|
|
coroutine.yield('exit')
|
|
|
|
end
|
|
|
|
|
2022-12-22 13:22:49 -07:00
|
|
|
mainlog = aredn.log.open("/tmp/manager.log", 8000)
|
|
|
|
|
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
|
2022-12-22 13:22:49 -07:00
|
|
|
mainlog.prefix = task.name
|
2022-01-17 17:54:44 -07:00
|
|
|
local status, newdelay = coroutine.resume(task.routine)
|
|
|
|
if not status then
|
2022-12-22 13:22:49 -07:00
|
|
|
mainlog:write(newdelay) -- error message
|
|
|
|
mainlog:flush()
|
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
|
2022-12-22 13:22:49 -07:00
|
|
|
mainlog:write("Terminating manager task: " .. task.name)
|
|
|
|
mainlog:flush()
|
2022-01-17 17:54:44 -07:00
|
|
|
else
|
|
|
|
task.time = newdelay + os.time()
|
|
|
|
end
|
2022-12-22 13:22:49 -07:00
|
|
|
mainlog.prefix = nil
|
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
|