i'm sorry i did not provide my code.
i was already using resursing inside my function
i did searches in subfilders with await Promise.all
const promises = subfolders_to_search.map((item) => searchRecurse(item));
await Promise.all(promises);
Solution: When i changed to just awaiting each subfolder i was able to abort like intended.
i set a 'global' variable when abort is detected so i do not need to propagate the abort back.
but i wonder why it works when doing:
await searchRecurse(item)
and not with:
const promises = subfolders_to_search.map((item) => searchRecurse(item));
await Promise.all(promises);
Any idea?
Complete working code here:
flow.set("SEARCH_ABORT", false)
if (context.get("SEARCHING") == true){
node.warn("already SEARCHING")
return null
}
context.set("SEARCHING", true)
msg.aborted = false
node.status({fill:"orange",shape:"ring",text:"Searching"});
let search_root = msg.payload
let search_for = msg.topic.toLowerCase()
let search_array = search_for.split("+")
node.warn(search_array)
let results = [] // not realy used for anything else than a count
function search(str,search_array){
if (msg.aborted == true) return false
let found = true
for (let search_term of search_array){
if (!str.includes(search_term) || search_term == "") {
found = false
break
}
}
return found
}
async function searchRecurse(path){
//await timersPromises.scheduler.yield(); this has no effect
//node.status({ fill: "orange", shape: "ring", text: "Searching: " + path}); // this uses network and cpu, disabled
if (!path.endsWith("/")) {
path += "/"
}
if (flow.get("SEARCH_ABORT") == true){
msg.aborted = true
//node.warn("Aborted")
return // only returns out of this function, not parent functions
}
const entries = await fs.promises.readdir(path, { withFileTypes: true });
let subfolders_to_search = []
for (let entry of entries) {
if (entry.isFile()) {
let name = entry.name
let found = search(name.toLowerCase(),search_array) // pathLowered +
if (found){
let match = {
name: name,
path
}
results.push(match)
node.send({ payload: match });
}
} else if (entry.isDirectory()) {
let name = entry.name
let found = search( name.toLowerCase(), search_array)
if (found) {
let match = {
name: name,
path,
type:"folder"
}
results.push(match)
node.send({ payload: match });
}
// await each subfolder here.
await searchRecurse(path + entry.name)
//no need to add to this
subfolders_to_search.push(path + entry.name)
} else {
}
}
// dont do this. makes it unable to abort
//const promises = subfolders_to_search.map((item) => searchRecurse(item));
//await Promise.all(promises);
}
await searchRecurse(search_root)
if (msg.aborted == true){
node.warn("Aborted")
//flow.set("SEARCH_ABORT", false) // do not set to false here. used after join node to stop results in flight
node.status({ fill: "blue", shape: "ring", text: "Aborted. Found " + results.length + " Files"});
msg.payload = {
type:"search_aborted",
message: "Aborted. Found " + results.length + " Files",
count:results.length
}
}else{
node.status({ fill: "green", shape: "ring", text: "Done. Found " + results.length + " Files"});
msg.payload = {
type:"search_done",
message: "Done. Found " + results.length + " Files",
count:results.length
}
}
context.set("SEARCHING", false)
return [null,msg];
by the way. results are fead trough a join node before going to a websocket, after this abort is checked to stop any results that might be in flight in the join node when aborting