Egg: 通过配置隐藏子进程窗口

Created on 27 May 2019  ·  10Comments  ·  Source: eggjs/egg

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

在windows上部署时,多个命令行窗口影响测试和客户部署操作,而且不够美观、简洁

What does the proposed API look like?

分别在下面两个位置添加上这个参数:windowsHide 可以达到隐藏 npm start 时打开的多个窗口,


  1. ```
    //egg-cluster\lib\master.js

forkAgentWorker() {
this.agentStartTime = Date.now();

const args = [ JSON.stringify(this.options) ];
const opt = {
  windowsHide: true 
};
2.
  ```
//cfork\index.js
  /**
   * fork worker with certain settings
   */
  function forkWorker(settings) {
    if (settings) {
      settings.windowsHide = true;
      cluster.settings = settings;
      cluster.setupMaster();
    } else {
      cluster.setupMaster({ windowsHide: true });
    }
    return cluster.fork(attachedEnv);
  }

我希望将这个功能编写到配置中

// config/config.default.js

// 通过配置cluster的windowsHide属性达到隐藏/显示   npm start 时打开的多个窗口
 config.cluster = {
    windowsHide: true,
  };

Most helpful comment

@atian25

1.

@qingdengyue https://nodejs.org/dist/latest-v10.x/docs/api/child_process.html#child_process_child_process_fork_modulepath_args_options

fork 好像是不支持的,只有 spwan 等才有

fork的options参数最终会传递给spwan的options参数,没有对特殊字段进行处理,所以fork也支持这个windowsHide参数,文档里没写
https://github.com/nodejs/node/blob/master/lib/child_process.js

2.

你试试启动命令直接传递 --windows-hide 看看,应该可以传递进去的

--windows-hide 方式的参数在 args 这个对象上 https://github.com/eggjs/egg-cluster/blob/d424239e56fec96d9e9c29e9bb1d96a46acc97b5/lib/master.js#L215,fork的options参数没有这个值

3.
cfork目前没有支持windowsHide这个参数,所以可能也需要支持一下。cluster组件已经支持了这个参数。
https://nodejs.org/dist/latest-v12.x/docs/api/cluster.html#cluster_cluster_settings

All 10 comments

image
Node 9.4.0 新增了原生的支持,然而这个参数不起作用(至少在我的 Win10 上不起作用)。这个参数稳定了,就方便了。

api上有这一段 child_process.fork() 方法是 child_process.spawn()的一个特例
我的理解是fork是从spawn上封装的,如果没有特意屏蔽额外参数,这个参数是可以传过去的

  1. Node Version: v10.12.0
  2. Egg Version: v2.2.1
  3. Plugin Name:
  4. Plugin Version:
  5. Platform: Windows 10
  6. Mini Showcase Repository:
    api上有这一段 child_process.fork() 方法是 child_process.spawn()
    child_process.spawn()是支持这个参数的。
    我的理解是fork是从spawn上封装的,如果没有特意屏蔽额外参数,这个参数是可以传过去的

我在本机实验是有效的

image text

你试试启动命令直接传递 --windows-hide 看看,应该可以传递进去的

const opt = {};

// add debug execArgv
const debugPort = process.env.EGG_AGENT_DEBUG_PORT || 5800;
if (this.options.isDebug) opt.execArgv = process.execArgv.concat([ --${semver.gte(process.version, '8.0.0') ? 'inspect' : 'debug'}-port=${debugPort} ]);

const agentWorker = childprocess.fork(this.getAgentWorkerFile(), args, opt);

根据这段代码可以看出,参数并没有传递到想要的位置。
我还原了修改之后,加上参数启动,窗口还是打开了

@atian25

1.

@qingdengyue https://nodejs.org/dist/latest-v10.x/docs/api/child_process.html#child_process_child_process_fork_modulepath_args_options

fork 好像是不支持的,只有 spwan 等才有

fork的options参数最终会传递给spwan的options参数,没有对特殊字段进行处理,所以fork也支持这个windowsHide参数,文档里没写
https://github.com/nodejs/node/blob/master/lib/child_process.js

2.

你试试启动命令直接传递 --windows-hide 看看,应该可以传递进去的

--windows-hide 方式的参数在 args 这个对象上 https://github.com/eggjs/egg-cluster/blob/d424239e56fec96d9e9c29e9bb1d96a46acc97b5/lib/master.js#L215,fork的options参数没有这个值

3.
cfork目前没有支持windowsHide这个参数,所以可能也需要支持一下。cluster组件已经支持了这个参数。
https://nodejs.org/dist/latest-v12.x/docs/api/cluster.html#cluster_cluster_settings

@qingdengyue

厉害厉害

欢迎提 PR

@atian25 修改好了,看看这样实现行不行~ :smile:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andylei18 picture andylei18  ·  35Comments

fengmk2 picture fengmk2  ·  44Comments

atian25 picture atian25  ·  68Comments

jtyjty99999 picture jtyjty99999  ·  52Comments

popomore picture popomore  ·  38Comments