insert-indexes.js (2023B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 let taskcluster = require("taskcluster-client"); 6 7 // Create instance of index client 8 let index = new taskcluster.Index({ 9 delayFactor: 750, // Good solid delay for background process 10 retries: 8, // A few extra retries for robustness 11 rootUrl: 12 process.env.TASKCLUSTER_PROXY_URL || process.env.TASKCLUSTER_ROOT_URL, 13 }); 14 15 // Create queue instance for fetching taskId 16 let queue = new taskcluster.Queue({ 17 delayFactor: 750, // Good solid delay for background process 18 retries: 8, // A few extra retries for robustness 19 rootUrl: 20 process.env.TASKCLUSTER_PROXY_URL || process.env.TASKCLUSTER_ROOT_URL, 21 }); 22 23 // Load input 24 let taskId = process.env.TARGET_TASKID; 25 let rank = parseInt(process.env.INDEX_RANK, 10); 26 let namespaces = process.argv.slice(2); 27 28 // Validate input 29 if (!taskId) { 30 console.log("Expected target task as environment variable: TARGET_TASKID"); 31 process.exit(1); 32 } 33 34 if (isNaN(rank)) { 35 console.log("Expected index rank as environment variable: INDEX_RANK"); 36 process.exit(1); 37 } 38 39 // Fetch task definition to get expiration and then insert into index 40 queue 41 .task(taskId) 42 .then(task => task.expires) 43 .then(expires => { 44 return Promise.all( 45 namespaces.map(namespace => { 46 console.log( 47 "Inserting %s into index (rank %d) under: %s", 48 taskId, 49 rank, 50 namespace 51 ); 52 return index.insertTask(namespace, { 53 taskId, 54 rank, 55 data: {}, 56 expires, 57 }); 58 }) 59 ); 60 }) 61 .then(() => { 62 console.log("indexing successfully completed."); 63 process.exit(0); 64 }) 65 .catch(err => { 66 console.log("Error:\n%s", err); 67 if (err.stack) { 68 console.log("Stack:\n%s", err.stack); 69 } 70 console.log("Properties:\n%j", err); 71 throw err; 72 }) 73 .catch(() => process.exit(1));