16a23e08bSopenharmony_ci/* 26a23e08bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 36a23e08bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46a23e08bSopenharmony_ci * you may not use this file except in compliance with the License. 56a23e08bSopenharmony_ci * You may obtain a copy of the License at 66a23e08bSopenharmony_ci * 76a23e08bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86a23e08bSopenharmony_ci * 96a23e08bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106a23e08bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116a23e08bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126a23e08bSopenharmony_ci * See the License for the specific language governing permissions and 136a23e08bSopenharmony_ci * limitations under the License. 146a23e08bSopenharmony_ci */ 156a23e08bSopenharmony_ci 166a23e08bSopenharmony_ciimport * as path from "path"; 176a23e08bSopenharmony_ciimport cluster from "cluster"; 186a23e08bSopenharmony_ciimport process from "process"; 196a23e08bSopenharmony_ciconst genAbcScript = "gen-abc.js"; 206a23e08bSopenharmony_ciconst FAIL = 1; 216a23e08bSopenharmony_ci 226a23e08bSopenharmony_ciif (process.env['arkEnvParams'] === undefined) { 236a23e08bSopenharmony_ci process.exit(FAIL); 246a23e08bSopenharmony_ci} 256a23e08bSopenharmony_ci 266a23e08bSopenharmony_cilet arkEnvParams = JSON.parse(process.env['arkEnvParams']); 276a23e08bSopenharmony_ciif (arkEnvParams['workerNumber'] !== undefined && 286a23e08bSopenharmony_ci arkEnvParams['splittedData'] !== undefined && 296a23e08bSopenharmony_ci arkEnvParams['cmdPrefix'] !== undefined) { 306a23e08bSopenharmony_ci const clusterNewApiVersion = 16; 316a23e08bSopenharmony_ci const currentNodeVersion = parseInt(process.version.split(".")[0]); 326a23e08bSopenharmony_ci const useNewApi = currentNodeVersion >= clusterNewApiVersion; 336a23e08bSopenharmony_ci 346a23e08bSopenharmony_ci let workerNumber = parseInt(arkEnvParams['workerNumber']); 356a23e08bSopenharmony_ci let splittedData = JSON.parse(arkEnvParams['splittedData']); 366a23e08bSopenharmony_ci let cmdPrefix = arkEnvParams['cmdPrefix']; 376a23e08bSopenharmony_ci 386a23e08bSopenharmony_ci if ((useNewApi && cluster.isPrimary) || (!useNewApi && cluster.isMaster)) { 396a23e08bSopenharmony_ci if (useNewApi) { 406a23e08bSopenharmony_ci cluster.setupPrimary({ 416a23e08bSopenharmony_ci exec: path.resolve(__dirname, genAbcScript), 426a23e08bSopenharmony_ci }); 436a23e08bSopenharmony_ci } else { 446a23e08bSopenharmony_ci cluster.setupMaster({ 456a23e08bSopenharmony_ci exec: path.resolve(__dirname, genAbcScript), 466a23e08bSopenharmony_ci }); 476a23e08bSopenharmony_ci } 486a23e08bSopenharmony_ci 496a23e08bSopenharmony_ci for (let i = 0; i < workerNumber; ++i) { 506a23e08bSopenharmony_ci const workerData = { 516a23e08bSopenharmony_ci inputs: JSON.stringify(splittedData[i]), 526a23e08bSopenharmony_ci cmd: cmdPrefix, 536a23e08bSopenharmony_ci }; 546a23e08bSopenharmony_ci cluster.fork(workerData); 556a23e08bSopenharmony_ci } 566a23e08bSopenharmony_ci cluster.on("exit", (worker, code, signal) => { 576a23e08bSopenharmony_ci if (code === FAIL) { 586a23e08bSopenharmony_ci process.exit(FAIL); 596a23e08bSopenharmony_ci } 606a23e08bSopenharmony_ci }); 616a23e08bSopenharmony_ci } 626a23e08bSopenharmony_ci} 63