refactor parallel code
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
local itemDB = require('core.itemDB')
|
||||
local Tasks = require('milo.taskRunner')
|
||||
local Util = require('util')
|
||||
|
||||
local fs = _G.fs
|
||||
local parallel = _G.parallel
|
||||
local turtle = _G.turtle
|
||||
|
||||
local Craft = {
|
||||
@@ -37,12 +37,18 @@ end
|
||||
|
||||
function Craft.clearGrid(storage)
|
||||
local success = true
|
||||
local tasks = Tasks()
|
||||
|
||||
for index, slot in pairs(storage.turtleInventory.adapter.list()) do
|
||||
if storage:import(storage.turtleInventory, index, slot.count, slot) ~= slot.count then
|
||||
success = false
|
||||
end
|
||||
tasks:add(function()
|
||||
if storage:import(storage.turtleInventory, index, slot.count, slot) ~= slot.count then
|
||||
success = false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
tasks:run()
|
||||
|
||||
return success
|
||||
end
|
||||
|
||||
@@ -133,10 +139,11 @@ local function turtleCraft(recipe, storage, request, count)
|
||||
end
|
||||
|
||||
local failed
|
||||
local fns = { }
|
||||
local tasks = Tasks()
|
||||
|
||||
for k,v in pairs(recipe.ingredients) do
|
||||
local item = splitKey(v)
|
||||
table.insert(fns, function()
|
||||
tasks:add(function()
|
||||
if storage:export(storage.turtleInventory, k, count, item) ~= count then
|
||||
request.status = 'rescan needed ?'
|
||||
request.statusCode = Craft.STATUS_ERROR
|
||||
@@ -146,7 +153,8 @@ local function turtleCraft(recipe, storage, request, count)
|
||||
end)
|
||||
end
|
||||
|
||||
parallel.waitForAll(table.unpack(fns))
|
||||
tasks:run()
|
||||
|
||||
if failed then
|
||||
Craft.clearGrid(storage)
|
||||
return
|
||||
|
||||
35
milo/apis/taskRunner.lua
Normal file
35
milo/apis/taskRunner.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
local class = require('class')
|
||||
|
||||
local parallel = _G.parallel
|
||||
|
||||
local TaskRunner = class()
|
||||
|
||||
function TaskRunner:init(args)
|
||||
self.tasks = { }
|
||||
self.errorMsg = 'Task failed: '
|
||||
|
||||
for k,v in pairs(args or { }) do
|
||||
self[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
function TaskRunner:add(fn)
|
||||
table.insert(self.tasks, function()
|
||||
local s, m = pcall(fn)
|
||||
if not s and m then
|
||||
self:onError(m)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function TaskRunner:run()
|
||||
if #self.tasks > 0 then
|
||||
parallel.waitForAll(table.unpack(self.tasks))
|
||||
end
|
||||
end
|
||||
|
||||
function TaskRunner:onError(msg)
|
||||
_G._debug(msg.errorMsg .. msg)
|
||||
end
|
||||
|
||||
return TaskRunner
|
||||
@@ -1,7 +1,6 @@
|
||||
local itemDB = require('core.itemDB')
|
||||
local Milo = require('milo')
|
||||
|
||||
local parallel = _G.parallel
|
||||
local Tasks = require('milo.taskRunner')
|
||||
|
||||
local ExportTask = {
|
||||
name = 'exporter',
|
||||
@@ -13,86 +12,81 @@ local function filter(a)
|
||||
end
|
||||
|
||||
function ExportTask:cycle(context)
|
||||
local tasks = { }
|
||||
local tasks = Tasks({
|
||||
errorMsg = 'EXPORTER error: ',
|
||||
})
|
||||
|
||||
for node in context.storage:filterActive('machine', filter) do
|
||||
table.insert(tasks, function()
|
||||
local s, m = pcall(function()
|
||||
for _, entry in pairs(node.exports) do
|
||||
tasks:add(function()
|
||||
for _, entry in pairs(node.exports) do
|
||||
|
||||
if not entry.filter then
|
||||
-- exports must have a filter
|
||||
-- TODO: validate in exportView
|
||||
break
|
||||
if not entry.filter then
|
||||
-- exports must have a filter
|
||||
-- TODO: validate in exportView
|
||||
break
|
||||
end
|
||||
|
||||
local function exportSingleSlot()
|
||||
local slot = node.adapter.getItemMeta(entry.slot)
|
||||
|
||||
if slot and slot.count == slot.maxCount then
|
||||
return
|
||||
end
|
||||
|
||||
local function exportSingleSlot()
|
||||
local slot = node.adapter.getItemMeta(entry.slot)
|
||||
|
||||
if slot and slot.count == slot.maxCount then
|
||||
return
|
||||
end
|
||||
|
||||
if slot then
|
||||
-- something is in the slot, find what we can export
|
||||
for key in pairs(entry.filter) do
|
||||
local filterItem = itemDB:splitKey(key)
|
||||
if (slot.name == filterItem.name and
|
||||
(entry.ignoreDamage or slot.damage == filterItem.damage) and
|
||||
(entry.ignoreNbtHash or slot.nbtHash == filterItem.nbtHash)) then
|
||||
|
||||
local items = Milo:getMatches(filterItem, entry)
|
||||
local _, item = next(items)
|
||||
if item then
|
||||
local count = math.min(item.count, slot.maxCount - slot.count)
|
||||
context.storage:export(node, entry.slot, count, item)
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
-- slot is empty - export first matching item we have in storage
|
||||
if slot then
|
||||
-- something is in the slot, find what we can export
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(itemDB:splitKey(key), entry)
|
||||
local _, item = next(items)
|
||||
if item then
|
||||
local count = math.min(item.count, itemDB:getMaxCount(item))
|
||||
context.storage:export(node, entry.slot, count, item)
|
||||
local filterItem = itemDB:splitKey(key)
|
||||
if (slot.name == filterItem.name and
|
||||
(entry.ignoreDamage or slot.damage == filterItem.damage) and
|
||||
(entry.ignoreNbtHash or slot.nbtHash == filterItem.nbtHash)) then
|
||||
|
||||
local items = Milo:getMatches(filterItem, entry)
|
||||
local _, item = next(items)
|
||||
if item then
|
||||
local count = math.min(item.count, slot.maxCount - slot.count)
|
||||
context.storage:export(node, entry.slot, count, item)
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
-- slot is empty - export first matching item we have in storage
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(itemDB:splitKey(key), entry)
|
||||
local _, item = next(items)
|
||||
if item then
|
||||
local count = math.min(item.count, itemDB:getMaxCount(item))
|
||||
context.storage:export(node, entry.slot, count, item)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function exportItems()
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(itemDB:splitKey(key), entry)
|
||||
for _,item in pairs(items) do
|
||||
if context.storage:export(node, nil, item.count, item) == 0 then
|
||||
-- TODO: really shouldn't break here as there may be room in other slots
|
||||
-- leaving for now for performance reasons
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function exportItems()
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(itemDB:splitKey(key), entry)
|
||||
for _,item in pairs(items) do
|
||||
if context.storage:export(node, nil, item.count, item) == 0 then
|
||||
-- TODO: really shouldn't break here as there may be room in other slots
|
||||
-- leaving for now for performance reasons
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if type(entry.slot) == 'number' then
|
||||
exportSingleSlot()
|
||||
else
|
||||
exportItems()
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
if not s and m then
|
||||
_G._debug('EXPORTER error: ' .. m)
|
||||
if type(entry.slot) == 'number' then
|
||||
exportSingleSlot()
|
||||
else
|
||||
exportItems()
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
if #tasks > 0 then
|
||||
parallel.waitForAll(table.unpack(tasks))
|
||||
end
|
||||
|
||||
tasks:run()
|
||||
end
|
||||
|
||||
Milo:registerTask(ExportTask)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
local itemDB = require('core.itemDB')
|
||||
local Milo = require('milo')
|
||||
|
||||
local parallel = _G.parallel
|
||||
local Tasks = require('milo.taskRunner')
|
||||
|
||||
local ImportTask = {
|
||||
name = 'importer',
|
||||
@@ -13,73 +12,67 @@ local function filter(a)
|
||||
end
|
||||
|
||||
function ImportTask:cycle(context)
|
||||
local tasks = { }
|
||||
local tasks = Tasks({
|
||||
errorMsg = 'IMPORT error: '
|
||||
})
|
||||
|
||||
for node in context.storage:filterActive('machine', filter) do
|
||||
table.insert(tasks, function()
|
||||
local s, m = pcall(function()
|
||||
for _, entry in pairs(node.imports) do
|
||||
tasks:add(function()
|
||||
for _, entry in pairs(node.imports) do
|
||||
|
||||
local function itemMatchesFilter(item)
|
||||
if not entry.ignoreDamage and not entry.ignoreNbtHash then
|
||||
local key = itemDB:makeKey(item)
|
||||
return entry.filter[key]
|
||||
end
|
||||
|
||||
for key in pairs(entry.filter) do
|
||||
local v = itemDB:splitKey(key)
|
||||
if item.name == v.name and
|
||||
(entry.ignoreDamage or item.damage == v.damage) and
|
||||
(entry.ignoreNbtHash or item.nbtHash == v.nbtHash) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
local function itemMatchesFilter(item)
|
||||
if not entry.ignoreDamage and not entry.ignoreNbtHash then
|
||||
local key = itemDB:makeKey(item)
|
||||
return entry.filter[key]
|
||||
end
|
||||
|
||||
local function matchesFilter(item)
|
||||
if not entry.filter then
|
||||
for key in pairs(entry.filter) do
|
||||
local v = itemDB:splitKey(key)
|
||||
if item.name == v.name and
|
||||
(entry.ignoreDamage or item.damage == v.damage) and
|
||||
(entry.ignoreNbtHash or item.nbtHash == v.nbtHash) then
|
||||
return true
|
||||
end
|
||||
|
||||
if entry.blacklist then
|
||||
return not itemMatchesFilter(item)
|
||||
end
|
||||
|
||||
return itemMatchesFilter(item)
|
||||
end
|
||||
|
||||
local list = node.adapter.list()
|
||||
|
||||
local function importSlot(slotNo)
|
||||
local item = itemDB:get(list[slotNo], function()
|
||||
return node.adapter.getItemMeta(slotNo)
|
||||
end)
|
||||
if item and matchesFilter(item) then
|
||||
context.storage:import(node, slotNo, item.count, item)
|
||||
end
|
||||
end
|
||||
|
||||
if type(entry.slot) == 'number' then
|
||||
if list[entry.slot] then
|
||||
importSlot(entry.slot)
|
||||
end
|
||||
else
|
||||
for i in pairs(list) do
|
||||
importSlot(i)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
if not s and m then
|
||||
_G._debug('IMPORTER error: ' .. m)
|
||||
local function matchesFilter(item)
|
||||
if not entry.filter then
|
||||
return true
|
||||
end
|
||||
|
||||
if entry.blacklist then
|
||||
return not itemMatchesFilter(item)
|
||||
end
|
||||
|
||||
return itemMatchesFilter(item)
|
||||
end
|
||||
|
||||
local list = node.adapter.list()
|
||||
|
||||
local function importSlot(slotNo)
|
||||
local item = itemDB:get(list[slotNo], function()
|
||||
return node.adapter.getItemMeta(slotNo)
|
||||
end)
|
||||
if item and matchesFilter(item) then
|
||||
context.storage:import(node, slotNo, item.count, item)
|
||||
end
|
||||
end
|
||||
|
||||
if type(entry.slot) == 'number' then
|
||||
if list[entry.slot] then
|
||||
importSlot(entry.slot)
|
||||
end
|
||||
else
|
||||
for i in pairs(list) do
|
||||
importSlot(i)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
if #tasks > 0 then
|
||||
parallel.waitForAll(table.unpack(tasks))
|
||||
end
|
||||
tasks:run()
|
||||
end
|
||||
|
||||
Milo:registerTask(ImportTask)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
local Milo = require('milo')
|
||||
|
||||
local parallel = _G.parallel
|
||||
local Milo = require('milo')
|
||||
local Tasks = require('milo.taskRunner')
|
||||
|
||||
local InputChest = {
|
||||
name = 'input',
|
||||
@@ -8,29 +7,24 @@ local InputChest = {
|
||||
}
|
||||
|
||||
function InputChest:cycle(context)
|
||||
local tasks = { }
|
||||
for node in context.storage:filterActive('input') do
|
||||
table.insert(tasks, function()
|
||||
local s, m = pcall(function()
|
||||
for slot, item in pairs(node.adapter.list()) do
|
||||
local s, m = pcall(function()
|
||||
context.storage:import(node, slot, item.count, item)
|
||||
end)
|
||||
if not s and m then
|
||||
_G._debug('INPUT error: ' .. m)
|
||||
end
|
||||
end
|
||||
end)
|
||||
local tasks = Tasks({
|
||||
errorMsg = 'INPUT error: '
|
||||
})
|
||||
|
||||
if not s and m then
|
||||
_G._debug('INPUT error: ' .. m)
|
||||
for node in context.storage:filterActive('input') do
|
||||
local s, m = pcall(function()
|
||||
for slot, item in pairs(node.adapter.list()) do
|
||||
tasks:add(function()
|
||||
context.storage:import(node, slot, item.count, item)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
if not s and m then
|
||||
_G._debug('INPUT error: ' .. m)
|
||||
end
|
||||
end
|
||||
|
||||
if #tasks > 0 then
|
||||
parallel.waitForAll(table.unpack(tasks))
|
||||
end
|
||||
tasks:run()
|
||||
end
|
||||
|
||||
Milo:registerTask(InputChest)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
local Event = require('event')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Milo = require('milo')
|
||||
local Tasks = require('milo.taskRunner')
|
||||
local UI = require('ui')
|
||||
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
local colors = _G.colors
|
||||
local device = _G.device
|
||||
|
||||
--[[ Configuration Screen ]]
|
||||
local wizardPage = UI.WizardPage {
|
||||
@@ -76,16 +76,20 @@ local function filter(a)
|
||||
end
|
||||
|
||||
function task:cycle(context)
|
||||
local tasks = Tasks()
|
||||
|
||||
for node in context.storage:filterActive('trashcan', filter) do
|
||||
Event.onTimeout(0, function() -- run on a background thread
|
||||
pcall(function()
|
||||
for k in pairs(node.adapter.list()) do
|
||||
local direction = node.dropDirection or 'down'
|
||||
pcall(function()
|
||||
for k in pairs(node.adapter.list()) do
|
||||
local direction = node.dropDirection or 'down'
|
||||
tasks:add(function()
|
||||
node.adapter.drop(k, 64, direction)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
tasks:run()
|
||||
end
|
||||
|
||||
Milo:registerTask(task)
|
||||
|
||||
Reference in New Issue
Block a user