@@ -78,29 +78,44 @@ jobs:
7878 const sanitized = contentDocs.map(sanitizeDoc);
7979 console.log(`Prepared ${sanitized.length} content documents (from ${allDocs.length} total lines) for dataset "dev"...`);
8080
81+ let activeToken = tokens[0];
8182 async function mutateBatch(mutations) {
82- let lastErr = "";
83- for ( const token of tokens) {
84- const res = await fetch(`https://${projectId}.api.sanity.io/v2021-06-07/data/mutate/dev?visibility=async &autoGenerateArrayKeys=true`, {
83+ for ( let attempt = 0; attempt < 5; attempt++) {
84+ const token = activeToken || tokens[attempt % tokens.length];
85+ const res = await fetch(`https://${projectId}.api.sanity.io/v2021-06-07/data/mutate/dev?visibility=deferred &autoGenerateArrayKeys=true`, {
8586 method: "POST",
8687 headers: {
8788 Authorization: `Bearer ${token}`,
8889 "Content-Type": "application/json",
8990 },
9091 body: JSON.stringify({ mutations }),
9192 });
92- if (res.ok) return;
93- lastErr = `HTTP ${res.status}: ${await res.text()}`;
93+ if (res.ok) {
94+ activeToken = token;
95+ return;
96+ }
97+ const errText = await res.text();
98+ if (res.status === 401 || res.status === 403) {
99+ activeToken = tokens.find((t) => t !== token) || token;
100+ continue;
101+ }
102+ if (res.status === 429 || res.status >= 500) {
103+ const delay = 1000 * Math.pow(2, attempt);
104+ console.warn(`Rate limited / server busy (HTTP ${res.status}), retrying in ${delay}ms...`);
105+ await new Promise((r) => setTimeout(r, delay));
106+ continue;
107+ }
108+ throw new Error(`Mutate batch failed: HTTP ${res.status}: ${errText}`);
94109 }
95- throw new Error(` Mutate batch failed: ${lastErr}` );
110+ throw new Error(" Mutate batch failed after 5 retries." );
96111 }
97112
98113 async function sendChunked(items, label) {
99114 let batch = [];
100115 let batchBytes = 0;
101116 let sent = 0;
102- const MAX_ITEMS = 50 ;
103- const MAX_BYTES = 1500000 ;
117+ const MAX_ITEMS = 200 ;
118+ const MAX_BYTES = 2500000 ;
104119
105120 for (const item of items) {
106121 const mutation = { createOrReplace: item };
0 commit comments