1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import * as path from "path"; 17import cluster from "cluster"; 18import process from "process"; 19const genAbcScript = "gen-abc.js"; 20const FAIL = 1; 21 22if (process.env['arkEnvParams'] === undefined) { 23 process.exit(FAIL); 24} 25 26let arkEnvParams = JSON.parse(process.env['arkEnvParams']); 27if (arkEnvParams['workerNumber'] !== undefined && 28 arkEnvParams['splittedData'] !== undefined && 29 arkEnvParams['cmdPrefix'] !== undefined) { 30 const clusterNewApiVersion = 16; 31 const currentNodeVersion = parseInt(process.version.split(".")[0]); 32 const useNewApi = currentNodeVersion >= clusterNewApiVersion; 33 34 let workerNumber = parseInt(arkEnvParams['workerNumber']); 35 let splittedData = JSON.parse(arkEnvParams['splittedData']); 36 let cmdPrefix = arkEnvParams['cmdPrefix']; 37 38 if ((useNewApi && cluster.isPrimary) || (!useNewApi && cluster.isMaster)) { 39 if (useNewApi) { 40 cluster.setupPrimary({ 41 exec: path.resolve(__dirname, genAbcScript), 42 }); 43 } else { 44 cluster.setupMaster({ 45 exec: path.resolve(__dirname, genAbcScript), 46 }); 47 } 48 49 for (let i = 0; i < workerNumber; ++i) { 50 const workerData = { 51 inputs: JSON.stringify(splittedData[i]), 52 cmd: cmdPrefix, 53 }; 54 cluster.fork(workerData); 55 } 56 cluster.on("exit", (worker, code, signal) => { 57 if (code === FAIL) { 58 process.exit(FAIL); 59 } 60 }); 61 } 62} 63