• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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';
19import {
20  ESMODULE,
21  FAIL,
22  GEN_ABC_SCRIPT,
23  GEN_MODULE_ABC_SCRIPT,
24  JSBUNDLE
25} from './pre_define';
26
27if (process.env.arkEnvParams === undefined) {
28  process.exit(FAIL);
29}
30
31let arkEnvParams = JSON.parse(process.env.arkEnvParams);
32if (arkEnvParams.mode !== JSBUNDLE && arkEnvParams.mode !== ESMODULE) {
33  process.exit(FAIL);
34}
35
36if (arkEnvParams.workerNumber !== undefined &&
37  arkEnvParams.splittedData !== undefined &&
38  arkEnvParams.cmdPrefix !== undefined) {
39  let workerNumber: number = parseInt(arkEnvParams.workerNumber);
40  let splittedData: Object = JSON.parse(arkEnvParams.splittedData);
41  let cmdPrefix: string = arkEnvParams.cmdPrefix;
42
43  const clusterNewApiVersion: number = 16;
44  const currentNodeVersion: number = parseInt(process.version.split('.')[0]);
45  const useNewApi: boolean = currentNodeVersion >= clusterNewApiVersion;
46
47  if ((useNewApi && cluster.isPrimary) || (!useNewApi && cluster.isMaster)) {
48    let genAbcScript: string = GEN_ABC_SCRIPT;
49    if (arkEnvParams.mode === ESMODULE) {
50      genAbcScript = GEN_MODULE_ABC_SCRIPT;
51    }
52    if (useNewApi) {
53      cluster.setupPrimary({
54        exec: path.resolve(__dirname, genAbcScript)
55      });
56    } else {
57      cluster.setupMaster({
58        exec: path.resolve(__dirname, genAbcScript)
59      });
60    }
61
62    for (let i = 0; i < workerNumber; ++i) {
63      let workerData: Object = {
64        'inputs': JSON.stringify(splittedData[i]),
65        'cmd': cmdPrefix
66      };
67      if (arkEnvParams.mode === ESMODULE) {
68        let sn: number = i + 1;
69        let workerFileName: string = `filesInfo_${sn}.txt`;
70        workerData.workerFileName = workerFileName;
71        workerData.cachePath = arkEnvParams.cachePath;
72      }
73      cluster.fork(workerData);
74    }
75
76    cluster.on('exit', (worker, code, signal) => {
77      if (code === FAIL) {
78        process.exit(FAIL);
79      }
80    });
81  }
82}
83