Nikita Kruglickiy b5c524705b A
2021-11-29 22:17:45 +03:00

57 lines
1.2 KiB
Plaintext

-- @name koptilnya/libs/workers
WORKERS = {}
WORKERS_QUOTA = 0.7
local function canProcess()
local exp1 = (math.max(chip():getQuotaAverage(), chip():getQuotaUsed()) + (chip():getQuotaUsed() - math.max(chip():getQuotaAverage(), chip():getQuotaUsed())) * 0.01) / chip():getQuotaMax() < WORKERS_QUOTA
local exp2 = math.max(quotaTotalAverage(), quotaTotalUsed()) < quotaMax() * WORKERS_QUOTA
return exp1 and exp2
end
local function execWorker(worker)
local status
while canProcess() do
status = worker()
if status == 1 or status == 2 then
break
end
end
return status
end
local function procWorkers()
local i = 1
while i <= #WORKERS do
local status = execWorker(WORKERS[i])
if status == 2 then
table.remove(WORKERS, i)
elseif status == 1 then
i = i + 1
else
break
end
end
if #WORKERS == 0 then
hook.remove("think", "workers_think")
end
end
function addWorker(worker)
local status = execWorker(worker)
if status ~= 2 then
if #WORKERS == 0 then
hook.add("think", "workers_think", procWorkers)
end
WORKERS[#WORKERS + 1] = worker
end
end