From 9d191f54a8273013c8c15fecde93d604600cefcc Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 9 Feb 2023 21:17:35 +0200 Subject: [PATCH] Play nicer with pinata rate limits (#8995) * Play nicer with pinata rate limits * Update * Additional logging --- scripts/ipfsUpload.mjs | 57 ++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/scripts/ipfsUpload.mjs b/scripts/ipfsUpload.mjs index 3ab6fb9ad2..b56f8a04a2 100644 --- a/scripts/ipfsUpload.mjs +++ b/scripts/ipfsUpload.mjs @@ -22,6 +22,12 @@ const PINMETA = { name: DOMAIN }; const repo = `https://${process.env.GH_PAT}@github.com/${process.env.GITHUB_REPOSITORY}.git`; +async function wait (delay = 2500) { + return new Promise((resolve) => { + setTimeout(() => resolve(), delay); + }); +} + function createPinata () { try { return pinataSDK(process.env.PINATA_API_KEY, process.env.PINATA_SECRET_KEY); @@ -98,6 +104,8 @@ async function pin () { console.log(`Pinned ${result.IpfsHash}`); + await wait(); + return result.IpfsHash; } @@ -110,20 +118,27 @@ async function unpin (exclude) { const result = await pinata.pinList({ metadata: PINMETA, status: 'pinned' }); + await wait(); + + console.log('Available Pinata pins', result.rows); + if (result.count > 1) { - const filtered = result.rows + const hashes = result.rows .map((r) => r.ipfs_pin_hash) .filter((hash) => hash !== exclude); - if (filtered.length) { - await Promise.all( - filtered.map((hash) => - pinata - .unpin(hash) - .then(() => console.log(`Unpinned ${hash}`)) - .catch(console.error) - ) - ); + for (let i = 0; i < hashes.length; i++) { + const hash = hashes[i]; + + try { + await pinata.unpin(hash); + + console.log(`Unpinned ${hash}`); + } catch (error) { + console.error(`Failed unpinning ${hash}`, error); + } + + await wait(); } } } @@ -144,12 +159,22 @@ async function dnslink (hash) { .join('.') ); - await Promise.all(records.map((record) => - cloudflare( - { token: process.env.CF_API_TOKEN }, - { link: `/ipfs/${hash}`, record, zone: DOMAIN } - ) - )); + for (let i = 0; i < records.length; i++) { + const record = records[i]; + + try { + await cloudflare( + { token: process.env.CF_API_TOKEN }, + { link: `/ipfs/${hash}`, record, zone: DOMAIN } + ); + + console.log(`Updated dnslink ${record}`); + } catch (error) { + console.error(`Failed updating dnslink ${record}`, error); + } + + await wait(); + } console.log(`Dnslink ${hash} for ${records.join(', ')}`); }