try catch

This commit is contained in:
ale 2024-09-21 03:07:51 +02:00
parent 886953f221
commit f27ab20679

View File

@ -68,17 +68,22 @@ module.exports = async (client, apex, app) => {
requestPart(`https://${instance}/api/v1/instance/peers`) requestPart(`https://${instance}/api/v1/instance/peers`)
]) ])
if (instancelocated.length === 0) { if (instancelocated.length === 0) {
await client.index({ try {
index: constant.index, await client.index({
body: { index: constant.index,
instance, body: {
api, instance,
nodeinfo, api,
blocks, nodeinfo,
peers, blocks,
last: new Date() peers,
} last: new Date()
}) }
})
} catch (e) {
console.error(e)
return
}
app.locals.created++ app.locals.created++
return await util.sendFederatedMessage(constant.nick, 'New Fediblock Instance', `Fediblock Instance ${instance} with ${json.length} blocks - https://${constant.apexdomain}#${instance}`, getAccount(api)) return await util.sendFederatedMessage(constant.nick, 'New Fediblock Instance', `Fediblock Instance ${instance} with ${json.length} blocks - https://${constant.apexdomain}#${instance}`, getAccount(api))
} else { } else {
@ -89,18 +94,23 @@ module.exports = async (client, apex, app) => {
|| !instancelocated[0]._source.api || !instancelocated[0]._source.api
|| !instancelocated[0]._source.nodeinfo || !instancelocated[0]._source.nodeinfo
|| !instancelocated[0]._source.peers) { || !instancelocated[0]._source.peers) {
await client.update({ try {
index: constant.index, await client.update({
id: instancelocated[0]._id, index: constant.index,
doc: { id: instancelocated[0]._id,
api: api ? api : instancelocated[0]._source.api, doc: {
nodeinfo: nodeinfo ? nodeinfo : instancelocated[0]._source.nodeinfo, api: api ? api : instancelocated[0]._source.api,
blocks: blocks && blocks.length > 0 ? blocks : elasticinstance, nodeinfo: nodeinfo ? nodeinfo : instancelocated[0]._source.nodeinfo,
peers: peers && peers.length > 0 ? peers : instancelocated[0]._source.peers, blocks: blocks && blocks.length > 0 ? blocks : elasticinstance,
last: new Date() peers: peers && peers.length > 0 ? peers : instancelocated[0]._source.peers,
}, last: new Date()
doc_as_upsert: true },
}) doc_as_upsert: true
})
} catch (e) {
console.error(e)
return
}
app.locals.updated++ app.locals.updated++
if (instancelocated[0]._source.api && instancelocated[0]._source.api.uri && instancelocated[0]._source.api.contact_account && instancelocated[0]._source.api.contact_account.acct) { if (instancelocated[0]._source.api && instancelocated[0]._source.api.uri && instancelocated[0]._source.api.contact_account && instancelocated[0]._source.api.contact_account.acct) {
const difference = blocks.filter(block => block.domain && block.domain.trim().length > 0 && !elasticinstance.some(instance => block.domain === instance.domain)) const difference = blocks.filter(block => block.domain && block.domain.trim().length > 0 && !elasticinstance.some(instance => block.domain === instance.domain))
@ -127,18 +137,22 @@ module.exports = async (client, apex, app) => {
return return
} }
}, },
scanPart = async (instancesall, index) => { scanPart = async function* (instancesall, index) {
for (const instance of instancesall) { try {
try { for (const instance of instancesall) {
app.locals.scan.emit('data', 'data: ' + (app.locals.scannum > 0 ? '(' + app.locals.scannum-- + ':' + (index + 1) + '): ' + instance : ': ' + instance) + '\n\n') try {
app.locals.peers++ app.locals.scan.emit('data', 'data: ' + (app.locals.scannum > 0 ? '(' + app.locals.scannum-- + ':' + (index + 1) + '): ' + instance : ': ' + instance) + '\n\n')
await scanInstance(instance) app.locals.peers++
} catch (e) { await scanInstance(instance)
console.error(e) } catch (e) {
console.error(e)
}
} }
yield 0
} catch (e) {
console.error(e)
yield 1
} }
global.gc()
return
}, },
scanIndex = async (server, instancesall) => { scanIndex = async (server, instancesall) => {
try { try {
@ -241,7 +255,11 @@ module.exports = async (client, apex, app) => {
const chunk = instancessorted.slice(i, i + chunkSize) const chunk = instancessorted.slice(i, i + chunkSize)
parts.push(chunk) parts.push(chunk)
} }
await Promise.all(parts.map(async (p, i) => await scanPart(p, i))) await Promise.all(parts.map(async (p, i) => {
for await (const exit of scanPart(p, i)) {
console.log('Done: ' + server + ' - ' + i + ' parts with exit ' + exit + '.')
}
}))
return await scanReturn() return await scanReturn()
} else { } else {
return await scanReturn() return await scanReturn()