From 4b11bf9e7117f5bd5e69985d8185cfa509256ae4 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 31 May 2023 15:44:15 +0800 Subject: [PATCH] feat: custom env by options.env (#107) closes https://github.com/eggjs/egg-cluster/issues/106 refactor: generator to async await --- .eslintrc | 2 +- .github/workflows/codeql.yml | 3 - .github/workflows/nodejs.yml | 6 +- .github/workflows/release.yml | 4 +- CHANGELOG.md | 362 ++++++++++++++++++ History.md | 359 ----------------- README.md | 6 +- lib/agent_worker.js | 2 +- lib/app_worker.js | 2 +- lib/master.js | 17 +- lib/utils/messenger.js | 2 +- lib/utils/mode/impl/worker_threads/app.js | 2 +- lib/utils/options.js | 4 + lib/utils/terminate.js | 2 +- lib/utils/timer.js | 5 + package.json | 15 +- test/agent_worker.test.js | 28 +- test/app_worker.test.js | 90 +++-- test/fixtures/apps/app-die/app/router.js | 8 +- .../apps/app-error-listeners/app/router.js | 8 +- test/fixtures/apps/app-kill/app/router.js | 10 +- .../apps/app-listen-hostname/app/router.js | 6 +- .../apps/app-listen-path/app/router.js | 6 +- .../apps/app-listen-port/app/router.js | 10 +- test/fixtures/apps/app-server/app/router.js | 6 +- test/fixtures/apps/before-close/agent.js | 8 +- test/fixtures/apps/before-close/app.js | 8 +- .../cluster_mod_app/app/controller/home.js | 6 +- .../cluster_mod_sticky/app/controller/home.js | 6 +- test/fixtures/apps/egg-ready/app/router.js | 10 +- .../apps/frameworkapp/app/controller/home.js | 12 +- test/fixtures/apps/frameworkapp/app/router.js | 4 +- .../apps/https-server-config/app/router.js | 6 +- test/fixtures/apps/https-server/app/router.js | 6 +- test/fixtures/apps/pid/app/router.js | 6 +- .../apps/script-start/start-server.js | 6 +- .../apps/worker-close-timeout/agent.js | 8 +- .../fixtures/apps/worker-close-timeout/app.js | 8 +- test/https.test.js | 16 +- test/master.test.js | 214 +++++------ test/options.test.js | 22 +- 41 files changed, 641 insertions(+), 670 deletions(-) delete mode 100644 History.md create mode 100644 lib/utils/timer.js diff --git a/.eslintrc b/.eslintrc index f8bdcf3..9441b72 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,4 +3,4 @@ "parserOptions": { "ecmaVersion": 13 } -} \ No newline at end of file +} diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index e806315..045af21 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -17,9 +17,6 @@ on: pull_request: # The branches below must be a subset of the branches above branches: [ "master" ] - schedule: - - cron: '45 17 * * 1' - jobs: analyze: name: Analyze diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index aebe8bb..de2b11a 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -7,12 +7,10 @@ on: pull_request: branches: [ master ] - workflow_dispatch: {} - jobs: Job: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 + uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest, macos-latest' - version: '14, 16, 18' + version: '14, 16, 18, 20' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1612587..a4e1158 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,12 +4,10 @@ on: push: branches: [ master ] - workflow_dispatch: {} - jobs: release: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 + uses: node-modules/github-actions/.github/workflows/node-release.yml@master secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 53013d3..bcc6ee3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,3 +6,365 @@ ### Bug Fixes * worker_threads mode without ports ([#103](https://github.com/eggjs/egg-cluster/issues/103)) ([a088e99](https://github.com/eggjs/egg-cluster/commit/a088e9953ed269af584a5fec6304847690cbf347)) + +--- + + +2.0.0 / 2022-11-02 +================== + +**features** + * [[`04a1e85`](http://github.com/eggjs/egg-cluster/commit/04a1e8539c61e2fecc16591e290aea84dcabd740)] - feat: support worker_threads (#101) (hyj1991 <>) + +1.27.1 / 2021-11-15 +================== + +**others** + * [[`98688f0`](http://github.com/eggjs/egg-cluster/commit/98688f04d9d6536a37dacf9f68ff44b1fbffbd35)] - chore: log more info before master closing (hyj1991 <>) + +1.27.0 / 2021-04-05 +================== + +**features** + * [[`36f3a05`](https://github.com/eggjs/egg-cluster.git/commit/36f3a05e396ba1c82e4c1d961aca91d09800d294)] - feat: catch app/agent boot error to formater FrameworkError (#98) (mansonchor.github.com <>) + +1.26.0 / 2019-11-18 +================== + +**features** + * [[`63c3cd0`](http://github.com/eggjs/egg-cluster/commit/63c3cd0dcbd5d3ca8d6b6c5bf08c85f4dd698d25)] - feat: print process.env.HOST while egg started (#96) (zōng yǔ <>) + +1.25.0 / 2019-07-24 +================== + +**features** + * [[`7fca260`](http://github.com/eggjs/egg-cluster/commit/7fca2606ae4be84c69af47d64227ca474e67ea70)] - feat: suppport config.cluster.https (#90) (TZ | 天猪 <>) + +**fixes** + * [[`5547a87`](http://github.com/eggjs/egg-cluster/commit/5547a877078b13dadde8b7fef94347dc18272eff)] - fix: add ca opiton if https (#93) (dxd <>) + +1.24.0 / 2019-06-14 +================== + +**features** + * [[`4b47bec`](http://github.com/eggjs/egg-cluster/commit/4b47becda40f72d260f7f101791371c803968533)] - feat: add windowsHide support (#92) (QingDeng <>) + +1.23.3 / 2019-05-27 +================== + +**fixes** + * [[`0f7558b`](http://github.com/eggjs/egg-cluster/commit/0f7558b24b5d6e37259a02c62cf4b92d678f3ee0)] - fix: should not ready when call server.listen in sticky mode (#91) (killa <>) + +1.23.2 / 2019-04-08 +================== + +**fixes** + * [[`de95e90`](http://github.com/eggjs/egg-cluster/commit/de95e907637aa6aa3b143e37fcccf457d9f53df8)] - fix: should destroy socket when receives an RST after the three-way handshake. (#89) (ngot <>) + +1.23.1 / 2019-03-12 +================== + +**others** + * [[`45d956f`](http://github.com/eggjs/egg-cluster/commit/45d956f9f29610da9316e5c5e8335c608e985739)] - chore: update all dependencies (#88) (Yiyu He <>) + +1.23.0 / 2019-03-04 +================== + +**features** + * [[`8740538`](http://github.com/eggjs/egg-cluster/commit/874053871bd8f28d27c8f82aae1bc3a1b8f4e98d)] - feat: save pid file (#87) (TZ | 天猪 <>) + +1.22.2 / 2018-12-04 +================== + +**fixes** + * [[`99d0c6b`](http://github.com/eggjs/egg-cluster/commit/99d0c6b6927add7536f505ba7e128a3c5c33475d)] - fix: master should not send to parent when egg-script is closed (#84) (killa <>) + +**others** + * [[`5920799`](http://github.com/eggjs/egg-cluster/commit/5920799dd7db9ea107723dd4794c2223463c2d8c)] - docs: remove `options.typescript` (#82) (吖猩 <>) + +1.22.1 / 2018-10-19 +=================== + + * fix: should not start app when agent start fail (#81) + +1.22.0 / 2018-10-10 +================== + +**features** + * [[`2b662a5`](http://github.com/eggjs/egg-cluster/commit/2b662a5d5a86cebf1096648afe473a96ce09b498)] - feat: kill all sub processes when exit (#79) (Yiyu He <>) + +1.21.0 / 2018-09-28 +=================== + + * feat: graceful exit when boot failed (#78) + +1.20.0 / 2018-09-17 +=================== + + * feat: kill agent after workers are killed (#76) + * docs: fix typo (#77) + +1.19.1 / 2018-08-27 +================== + +**fixes** + * [[`b0c8d19`](http://github.com/eggjs/egg-cluster/commit/b0c8d19e08495c596045eada460590562a4150be)] - fix: fix messenger#sendTo (#74) (killa <>) + +1.19.0 / 2018-07-26 +=================== + + * feat: support options.require (#73) + * feat: replace worker file from outside (#72) + +1.18.0 / 2018-06-12 +=================== + + * feat: support config agent debug port (#70) + +1.17.0 / 2018-06-06 +================== + +**features** + * [[`134bd4c`](http://github.com/eggjs/egg-cluster/commit/134bd4c15361018747f6bc6c13748a8e60fc8b62)] - feat: not start check in local (#71) (Axes <>) + +1.16.1 / 2018-05-16 +=================== + + * fix: use --inspect-port after 8.x (#69) + * fix: remove useless unittest (#65) + * fix: master close log print timeout error (#64) + +1.16.0 / 2018-03-28 +=================== + + * fix: wait 5s when master exit (#60) + * feat: add typescript support (#61) + +1.15.0 / 2018-03-05 +================== + +**features** + * [[`7f0be22`](http://github.com/eggjs/egg-cluster/commit/7f0be221808a29fd049fac4c0c53da8d48ce6e6e)] - feat: support beforeClose on other env (#59) (Haoliang Gao <>) + +**others** + * [[`e0deece`](http://github.com/eggjs/egg-cluster/commit/e0deece60a04bdeeb9841fbb751d49d94a5b0828)] - test: fix unstable test (#58) (Haoliang Gao <>) + +1.14.0 / 2018-02-05 +================== + +**features** + * [[`102a0ad`](http://github.com/eggjs/egg-cluster/commit/102a0adc8577ada8ff8383ba935f7973f215999f)] - feat: https options (#57) (TZ | 天猪 <>) + +1.13.1 / 2018-01-05 +================== + +**others** + * [[`53ed359`](http://github.com/eggjs/egg-cluster/commit/53ed359e32b395105d859de7ea4bc564fe3e9af1)] - chore: log runtime versions (#56) (TZ | 天猪 <>) + +1.13.0 / 2017-12-05 +================== + +**features** + * [[`366d9bb`](http://github.com/eggjs/egg-cluster/commit/366d9bbb40db2b920258210f51b0a15fe224974c)] - feat: add worker manager and check worker/agent status (#54) (Yiyu He <>) + +1.12.6 / 2017-11-21 +================== + +**fixes** + * [[`ccdbf9f`](http://github.com/eggjs/egg-cluster/commit/ccdbf9f34a9eb38333a7a12ab0ec22e3b1bea344)] - fix: should send current worker pids when agent restart (#53) (Haoliang Gao <>) + +1.12.5 / 2017-11-16 +================== + +**fixes** + * [[`c683b67`](http://github.com/eggjs/egg-cluster/commit/c683b678c2474fca1e9dad8101525c9661177486)] - fix: crash when socket destroyed during connecting (#50) (Hengfei Zhuang <>) + +**others** + * [[`242835e`](http://github.com/eggjs/egg-cluster/commit/242835e4948b2b2f76352683aeee4bb2bd84186c)] - test: `don't fork` has been printed to stdout (#52) (Haoliang Gao <>) + +1.12.4 / 2017-10-29 +================== + +**others** + * [[`b768018`](http://github.com/eggjs/egg-cluster/commit/b768018b77e1c6f80e8cfd9e9c38e63ec44a473d)] - refactor: use utility to read json (#49) (Haoliang Gao <>) + +1.12.3 / 2017-10-12 +================== + +**fixes** + * [[`4b80f33`](http://github.com/eggjs/egg-cluster/commit/4b80f33d272b374e005f3dd2f9dc39865f0d3688)] - fix: master should exit when EADDRINUSE error (#48) (Haoliang Gao <>) + +1.12.2 / 2017-09-28 +=================== + +**fixes** + * [[`0c886a4`](https://github.com/eggjs/egg-cluster/commit/0c886a43ecac25e79de910cb04778efc77aab9a4)] - fix: should disable worker refork (#46) (fengmk2 <>) + +1.12.1 / 2017-09-22 +================== + +**fixes** + * [[`30d120d`](http://github.com/eggjs/egg-cluster/commit/30d120dd292d79a25113c951664d18480dde2a00)] - fix: should send egg-ready when app/agent restarted after launched (#45) (Haoliang Gao <>) + +1.12.0 / 2017-09-14 +================== + +**features** + * [[`56bff0d`](http://github.com/eggjs/egg-cluster/commit/56bff0dda04074cbd7259b2652954832647a9a61)] - feat: agent debugPort 5856 -> 5800 (TZ <>) + +1.11.2 / 2017-09-13 +================== + +**features** + * [[`61b5b66`](http://github.com/eggjs/egg-cluster/commit/61b5b660146b5ef3795f7f2fe4d61195ca6d93c1)] - feat: simplify debug error when kill by vscode (TZ <>) + * [[`308d9fd`](http://github.com/eggjs/egg-cluster/commit/308d9fde230122dd1d85d9563dd64c5103f8a7df)] - feat: debug & egg-ready message (TZ <>) + * [[`a4fa5f5`](http://github.com/eggjs/egg-cluster/commit/a4fa5f5171226354d02760edbfc20db8eca3f9e1)] - feat: delete default port options that has defined in egg (#40) (Haoliang Gao <>) + * [[`09ccc5a`](http://github.com/eggjs/egg-cluster/commit/09ccc5ab62bbac2a214d80589dcc1695adfb6b90)] - feat: revert to 1.9.2 (TZ <>) + +**others** + * [[`ab76a19`](http://github.com/eggjs/egg-cluster/commit/ab76a19a8275906df757d2a9e585ccd89c62f6a5)] - test: improve cov (TZ <>) + +1.11.1 / 2017-09-11 +================== + +**fixes** + * [[`8d46f20`](http://github.com/eggjs/egg-cluster/commit/8d46f20c04967647a1991736d2db2db0202790a2)] - fix: only set options.debugProtocol at debug mode (#42) (TZ | 天猪 <>) + +1.11.0 / 2017-09-08 +================== + +**features** + * [[`49bd949`](http://github.com/eggjs/egg-cluster/commit/49bd949a5271f0db86de6c4111fefbd6613017e1)] - feat: delete default port options that has defined in egg (#40) (Haoliang Gao <>) + +**others** + * [[`0561ce7`](http://github.com/eggjs/egg-cluster/commit/0561ce7bd69151dd66b1e24352f4f0b632591a39)] - refactor: support debug options (#41) (TZ | 天猪 <>) + * [new tag] 1.10.0 -> 1.10.0 + + +1.10.0 / 2017-09-07 +================== + +**others** + * [[`0561ce7`](http://github.com/eggjs/egg-cluster/commit/0561ce7bd69151dd66b1e24352f4f0b632591a39)] - refactor: support debug options (#41) (TZ | 天猪 <>) + +1.9.2 / 2017-08-30 +================== + +**fixes** + * [[`7277b00`](http://github.com/eggjs/egg-cluster/commit/7277b00516905f0e26c78c063b7f84044c069b6d)] - fix: debug status detect should support inspect (#39) (TZ | 天猪 <>) + +1.9.1 / 2017-08-28 +================== + + * fix: sleep 100ms to make sure SIGTERM send to the child processes (#37) + * test: fix test that should mock the default port (#38) + +1.9.0 / 2017-07-27 +================== + + * feat: add listen config (#34) + * refactor: disable console (#36) + * deps: upgrade eslint (#35) + * refactor: set agent worker and app worker console level (#33) + * deps: upgrade dependencies (#32) + +1.8.0 / 2017-06-12 +================== + + * feat: use graceful-process to refactor app and agent worker (#30) + * test: sleep 20s to wait for agent process start (#29) + +1.7.0 / 2017-06-09 +================== + + * feat: reduce info logs on local env (#28) + +1.6.4 / 2017-05-28 +================== + + * fix: agent should exit on disconnect event whatever master kill with SIGKILL (#27) + +1.6.3 / 2017-05-22 +================== + + * fix: fix typo (#24) + * fix: start error should log what happend (#26) + * fix: fix deperated api (#25) + * deps: upgrade dependencies (#22) + +1.6.2 / 2017-03-22 +================== + + * fix: should print logger when agent start error (#20) + +1.6.1 / 2017-03-03 +================== + + * fix: sticky logic error (#19) + * feat: use egg-utils (#18) + +1.6.0 / 2017-03-01 +================== + + * feat: add options framework (#17) + +1.5.0 / 2017-02-21 +================== + + * feat: exit when error emitted during start (#16) + +1.4.0 / 2017-02-13 +================== + + * feat:add sticky cluster mode (#14) + * test: add test for agent debug port (#13) + +1.3.0 / 2017-01-20 +================== + + * feat: get clusterPort (#12) + +1.2.0 / 2016-12-26 +================== + + * feat: npm publish files limit (#10) + +1.1.0 / 2016-12-20 +================== + + * deps: upgrade dependencies + * refactor: options should be passed through + * feat: print env when start (#8) + +1.0.0 / 2016-10-12 +================== + + * feat: exit if worker start timeout (#6) + +0.2.0 / 2016-10-12 +================== + + * feat: when debug mode, master should exit when worker die (#7) + * test: fix testcase (#5) + +0.1.0 / 2016-08-16 +================== + + * feat: [BREAKING_CHANGE] master won't load config (#4) + * test: add test cases (#3) + +0.0.4 / 2016-07-16 +================== + + * fix: remove antx loader (#2) + +0.0.3 / 2016-07-16 +================== + + * fix: loader version (#1) + * fix: logger + +0.0.2 / 2016-07-15 +================== + + * init code diff --git a/History.md b/History.md deleted file mode 100644 index 35628f2..0000000 --- a/History.md +++ /dev/null @@ -1,359 +0,0 @@ - -2.0.0 / 2022-11-02 -================== - -**features** - * [[`04a1e85`](http://github.com/eggjs/egg-cluster/commit/04a1e8539c61e2fecc16591e290aea84dcabd740)] - feat: support worker_threads (#101) (hyj1991 <>) - -1.27.1 / 2021-11-15 -================== - -**others** - * [[`98688f0`](http://github.com/eggjs/egg-cluster/commit/98688f04d9d6536a37dacf9f68ff44b1fbffbd35)] - chore: log more info before master closing (hyj1991 <>) - -1.27.0 / 2021-04-05 -================== - -**features** - * [[`36f3a05`](https://github.com/eggjs/egg-cluster.git/commit/36f3a05e396ba1c82e4c1d961aca91d09800d294)] - feat: catch app/agent boot error to formater FrameworkError (#98) (mansonchor.github.com <>) - -1.26.0 / 2019-11-18 -================== - -**features** - * [[`63c3cd0`](http://github.com/eggjs/egg-cluster/commit/63c3cd0dcbd5d3ca8d6b6c5bf08c85f4dd698d25)] - feat: print process.env.HOST while egg started (#96) (zōng yǔ <>) - -1.25.0 / 2019-07-24 -================== - -**features** - * [[`7fca260`](http://github.com/eggjs/egg-cluster/commit/7fca2606ae4be84c69af47d64227ca474e67ea70)] - feat: suppport config.cluster.https (#90) (TZ | 天猪 <>) - -**fixes** - * [[`5547a87`](http://github.com/eggjs/egg-cluster/commit/5547a877078b13dadde8b7fef94347dc18272eff)] - fix: add ca opiton if https (#93) (dxd <>) - -1.24.0 / 2019-06-14 -================== - -**features** - * [[`4b47bec`](http://github.com/eggjs/egg-cluster/commit/4b47becda40f72d260f7f101791371c803968533)] - feat: add windowsHide support (#92) (QingDeng <>) - -1.23.3 / 2019-05-27 -================== - -**fixes** - * [[`0f7558b`](http://github.com/eggjs/egg-cluster/commit/0f7558b24b5d6e37259a02c62cf4b92d678f3ee0)] - fix: should not ready when call server.listen in sticky mode (#91) (killa <>) - -1.23.2 / 2019-04-08 -================== - -**fixes** - * [[`de95e90`](http://github.com/eggjs/egg-cluster/commit/de95e907637aa6aa3b143e37fcccf457d9f53df8)] - fix: should destroy socket when receives an RST after the three-way handshake. (#89) (ngot <>) - -1.23.1 / 2019-03-12 -================== - -**others** - * [[`45d956f`](http://github.com/eggjs/egg-cluster/commit/45d956f9f29610da9316e5c5e8335c608e985739)] - chore: update all dependencies (#88) (Yiyu He <>) - -1.23.0 / 2019-03-04 -================== - -**features** - * [[`8740538`](http://github.com/eggjs/egg-cluster/commit/874053871bd8f28d27c8f82aae1bc3a1b8f4e98d)] - feat: save pid file (#87) (TZ | 天猪 <>) - -1.22.2 / 2018-12-04 -================== - -**fixes** - * [[`99d0c6b`](http://github.com/eggjs/egg-cluster/commit/99d0c6b6927add7536f505ba7e128a3c5c33475d)] - fix: master should not send to parent when egg-script is closed (#84) (killa <>) - -**others** - * [[`5920799`](http://github.com/eggjs/egg-cluster/commit/5920799dd7db9ea107723dd4794c2223463c2d8c)] - docs: remove `options.typescript` (#82) (吖猩 <>) - -1.22.1 / 2018-10-19 -=================== - - * fix: should not start app when agent start fail (#81) - -1.22.0 / 2018-10-10 -================== - -**features** - * [[`2b662a5`](http://github.com/eggjs/egg-cluster/commit/2b662a5d5a86cebf1096648afe473a96ce09b498)] - feat: kill all sub processes when exit (#79) (Yiyu He <>) - -1.21.0 / 2018-09-28 -=================== - - * feat: graceful exit when boot failed (#78) - -1.20.0 / 2018-09-17 -=================== - - * feat: kill agent after workers are killed (#76) - * docs: fix typo (#77) - -1.19.1 / 2018-08-27 -================== - -**fixes** - * [[`b0c8d19`](http://github.com/eggjs/egg-cluster/commit/b0c8d19e08495c596045eada460590562a4150be)] - fix: fix messenger#sendTo (#74) (killa <>) - -1.19.0 / 2018-07-26 -=================== - - * feat: support options.require (#73) - * feat: replace worker file from outside (#72) - -1.18.0 / 2018-06-12 -=================== - - * feat: support config agent debug port (#70) - -1.17.0 / 2018-06-06 -================== - -**features** - * [[`134bd4c`](http://github.com/eggjs/egg-cluster/commit/134bd4c15361018747f6bc6c13748a8e60fc8b62)] - feat: not start check in local (#71) (Axes <>) - -1.16.1 / 2018-05-16 -=================== - - * fix: use --inspect-port after 8.x (#69) - * fix: remove useless unittest (#65) - * fix: master close log print timeout error (#64) - -1.16.0 / 2018-03-28 -=================== - - * fix: wait 5s when master exit (#60) - * feat: add typescript support (#61) - -1.15.0 / 2018-03-05 -================== - -**features** - * [[`7f0be22`](http://github.com/eggjs/egg-cluster/commit/7f0be221808a29fd049fac4c0c53da8d48ce6e6e)] - feat: support beforeClose on other env (#59) (Haoliang Gao <>) - -**others** - * [[`e0deece`](http://github.com/eggjs/egg-cluster/commit/e0deece60a04bdeeb9841fbb751d49d94a5b0828)] - test: fix unstable test (#58) (Haoliang Gao <>) - -1.14.0 / 2018-02-05 -================== - -**features** - * [[`102a0ad`](http://github.com/eggjs/egg-cluster/commit/102a0adc8577ada8ff8383ba935f7973f215999f)] - feat: https options (#57) (TZ | 天猪 <>) - -1.13.1 / 2018-01-05 -================== - -**others** - * [[`53ed359`](http://github.com/eggjs/egg-cluster/commit/53ed359e32b395105d859de7ea4bc564fe3e9af1)] - chore: log runtime versions (#56) (TZ | 天猪 <>) - -1.13.0 / 2017-12-05 -================== - -**features** - * [[`366d9bb`](http://github.com/eggjs/egg-cluster/commit/366d9bbb40db2b920258210f51b0a15fe224974c)] - feat: add worker manager and check worker/agent status (#54) (Yiyu He <>) - -1.12.6 / 2017-11-21 -================== - -**fixes** - * [[`ccdbf9f`](http://github.com/eggjs/egg-cluster/commit/ccdbf9f34a9eb38333a7a12ab0ec22e3b1bea344)] - fix: should send current worker pids when agent restart (#53) (Haoliang Gao <>) - -1.12.5 / 2017-11-16 -================== - -**fixes** - * [[`c683b67`](http://github.com/eggjs/egg-cluster/commit/c683b678c2474fca1e9dad8101525c9661177486)] - fix: crash when socket destroyed during connecting (#50) (Hengfei Zhuang <>) - -**others** - * [[`242835e`](http://github.com/eggjs/egg-cluster/commit/242835e4948b2b2f76352683aeee4bb2bd84186c)] - test: `don't fork` has been printed to stdout (#52) (Haoliang Gao <>) - -1.12.4 / 2017-10-29 -================== - -**others** - * [[`b768018`](http://github.com/eggjs/egg-cluster/commit/b768018b77e1c6f80e8cfd9e9c38e63ec44a473d)] - refactor: use utility to read json (#49) (Haoliang Gao <>) - -1.12.3 / 2017-10-12 -================== - -**fixes** - * [[`4b80f33`](http://github.com/eggjs/egg-cluster/commit/4b80f33d272b374e005f3dd2f9dc39865f0d3688)] - fix: master should exit when EADDRINUSE error (#48) (Haoliang Gao <>) - -1.12.2 / 2017-09-28 -=================== - -**fixes** - * [[`0c886a4`](https://github.com/eggjs/egg-cluster/commit/0c886a43ecac25e79de910cb04778efc77aab9a4)] - fix: should disable worker refork (#46) (fengmk2 <>) - -1.12.1 / 2017-09-22 -================== - -**fixes** - * [[`30d120d`](http://github.com/eggjs/egg-cluster/commit/30d120dd292d79a25113c951664d18480dde2a00)] - fix: should send egg-ready when app/agent restarted after launched (#45) (Haoliang Gao <>) - -1.12.0 / 2017-09-14 -================== - -**features** - * [[`56bff0d`](http://github.com/eggjs/egg-cluster/commit/56bff0dda04074cbd7259b2652954832647a9a61)] - feat: agent debugPort 5856 -> 5800 (TZ <>) - -1.11.2 / 2017-09-13 -================== - -**features** - * [[`61b5b66`](http://github.com/eggjs/egg-cluster/commit/61b5b660146b5ef3795f7f2fe4d61195ca6d93c1)] - feat: simplify debug error when kill by vscode (TZ <>) - * [[`308d9fd`](http://github.com/eggjs/egg-cluster/commit/308d9fde230122dd1d85d9563dd64c5103f8a7df)] - feat: debug & egg-ready message (TZ <>) - * [[`a4fa5f5`](http://github.com/eggjs/egg-cluster/commit/a4fa5f5171226354d02760edbfc20db8eca3f9e1)] - feat: delete default port options that has defined in egg (#40) (Haoliang Gao <>) - * [[`09ccc5a`](http://github.com/eggjs/egg-cluster/commit/09ccc5ab62bbac2a214d80589dcc1695adfb6b90)] - feat: revert to 1.9.2 (TZ <>) - -**others** - * [[`ab76a19`](http://github.com/eggjs/egg-cluster/commit/ab76a19a8275906df757d2a9e585ccd89c62f6a5)] - test: improve cov (TZ <>) - -1.11.1 / 2017-09-11 -================== - -**fixes** - * [[`8d46f20`](http://github.com/eggjs/egg-cluster/commit/8d46f20c04967647a1991736d2db2db0202790a2)] - fix: only set options.debugProtocol at debug mode (#42) (TZ | 天猪 <>) - -1.11.0 / 2017-09-08 -================== - -**features** - * [[`49bd949`](http://github.com/eggjs/egg-cluster/commit/49bd949a5271f0db86de6c4111fefbd6613017e1)] - feat: delete default port options that has defined in egg (#40) (Haoliang Gao <>) - -**others** - * [[`0561ce7`](http://github.com/eggjs/egg-cluster/commit/0561ce7bd69151dd66b1e24352f4f0b632591a39)] - refactor: support debug options (#41) (TZ | 天猪 <>) - * [new tag] 1.10.0 -> 1.10.0 - - -1.10.0 / 2017-09-07 -================== - -**others** - * [[`0561ce7`](http://github.com/eggjs/egg-cluster/commit/0561ce7bd69151dd66b1e24352f4f0b632591a39)] - refactor: support debug options (#41) (TZ | 天猪 <>) - -1.9.2 / 2017-08-30 -================== - -**fixes** - * [[`7277b00`](http://github.com/eggjs/egg-cluster/commit/7277b00516905f0e26c78c063b7f84044c069b6d)] - fix: debug status detect should support inspect (#39) (TZ | 天猪 <>) - -1.9.1 / 2017-08-28 -================== - - * fix: sleep 100ms to make sure SIGTERM send to the child processes (#37) - * test: fix test that should mock the default port (#38) - -1.9.0 / 2017-07-27 -================== - - * feat: add listen config (#34) - * refactor: disable console (#36) - * deps: upgrade eslint (#35) - * refactor: set agent worker and app worker console level (#33) - * deps: upgrade dependencies (#32) - -1.8.0 / 2017-06-12 -================== - - * feat: use graceful-process to refactor app and agent worker (#30) - * test: sleep 20s to wait for agent process start (#29) - -1.7.0 / 2017-06-09 -================== - - * feat: reduce info logs on local env (#28) - -1.6.4 / 2017-05-28 -================== - - * fix: agent should exit on disconnect event whatever master kill with SIGKILL (#27) - -1.6.3 / 2017-05-22 -================== - - * fix: fix typo (#24) - * fix: start error should log what happend (#26) - * fix: fix deperated api (#25) - * deps: upgrade dependencies (#22) - -1.6.2 / 2017-03-22 -================== - - * fix: should print logger when agent start error (#20) - -1.6.1 / 2017-03-03 -================== - - * fix: sticky logic error (#19) - * feat: use egg-utils (#18) - -1.6.0 / 2017-03-01 -================== - - * feat: add options framework (#17) - -1.5.0 / 2017-02-21 -================== - - * feat: exit when error emitted during start (#16) - -1.4.0 / 2017-02-13 -================== - - * feat:add sticky cluster mode (#14) - * test: add test for agent debug port (#13) - -1.3.0 / 2017-01-20 -================== - - * feat: get clusterPort (#12) - -1.2.0 / 2016-12-26 -================== - - * feat: npm publish files limit (#10) - -1.1.0 / 2016-12-20 -================== - - * deps: upgrade dependencies - * refactor: options should be passed through - * feat: print env when start (#8) - -1.0.0 / 2016-10-12 -================== - - * feat: exit if worker start timeout (#6) - -0.2.0 / 2016-10-12 -================== - - * feat: when debug mode, master should exit when worker die (#7) - * test: fix testcase (#5) - -0.1.0 / 2016-08-16 -================== - - * feat: [BREAKING_CHANGE] master won't load config (#4) - * test: add test cases (#3) - -0.0.4 / 2016-07-16 -================== - - * fix: remove antx loader (#2) - -0.0.3 / 2016-07-16 -================== - - * fix: loader version (#1) - * fix: logger - -0.0.2 / 2016-07-15 -================== - - * init code diff --git a/README.md b/README.md index 874be13..66967c7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # egg-cluster [![NPM version][npm-image]][npm-url] +[![CI](https://github.com/eggjs/egg-cluster/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-cluster/actions/workflows/nodejs.yml) [![Test coverage][codecov-image]][codecov-url] [![Known Vulnerabilities][snyk-image]][snyk-url] [![npm download][download-image]][download-url] @@ -14,14 +15,14 @@ [download-image]: https://img.shields.io/npm/dm/egg-cluster.svg?style=flat-square [download-url]: https://npmjs.org/package/egg-cluster -Cluster Manager for Egg +Cluster Manager for EggJS --- ## Install ```bash -$ npm i egg-cluster --save +npm i egg-cluster --save ``` ## Usage @@ -57,6 +58,7 @@ startCluster(options, () => { | pidFile | `String` | will save master pid to this file | | startMode | `String` | default is 'process', use 'worker_threads' to start the app & agent worker by worker_threads | | ports | `Array` | startup port of each app worker, such as: [7001, 7002, 7003], only effects when the startMode is 'worker_threads' | +| env | `String` | custom env, default is process.env.EGG_SERVER_ENV | ## Env diff --git a/lib/agent_worker.js b/lib/agent_worker.js index 5d5fcb5..678e70b 100644 --- a/lib/agent_worker.js +++ b/lib/agent_worker.js @@ -24,7 +24,7 @@ if (options.startMode === 'worker_threads') { AgentWorker = require('./utils/mode/impl/process/agent').AgentWorker; } -const debug = require('debug')('egg-cluster'); +const debug = require('util').debuglog('egg-cluster'); const ConsoleLogger = require('egg-logger').EggConsoleLogger; const consoleLogger = new ConsoleLogger({ level: process.env.EGG_AGENT_WORKER_LOGGER_LEVEL }); diff --git a/lib/app_worker.js b/lib/app_worker.js index 40eda9b..0a0d82a 100644 --- a/lib/app_worker.js +++ b/lib/app_worker.js @@ -17,7 +17,7 @@ if (options.startMode === 'worker_threads') { } const fs = require('fs'); -const debug = require('debug')('egg-cluster'); +const debug = require('util').debuglog('egg-cluster'); const ConsoleLogger = require('egg-logger').EggConsoleLogger; const consoleLogger = new ConsoleLogger({ level: process.env.EGG_APP_WORKER_LOGGER_LEVEL, diff --git a/lib/master.js b/lib/master.js index 2e17b17..882ecbb 100644 --- a/lib/master.js +++ b/lib/master.js @@ -10,7 +10,6 @@ const ready = require('get-ready'); const GetFreePort = require('detect-port'); const ConsoleLogger = require('egg-logger').EggConsoleLogger; const utility = require('utility'); -const { mkdirp } = require('mz-modules'); const Manager = require('./utils/manager'); const parseOptions = require('./utils/options'); @@ -37,6 +36,7 @@ class Master extends EventEmitter { * - {Object} [https] https options, { key, cert, ca }, full path * - {Array|String} [require] will inject into worker/agent process * - {String} [pidFile] will save master pid to this file + * - {String} [env] custom env, default is process.env.EGG_SERVER_ENV */ constructor(options) { super(); @@ -46,7 +46,7 @@ class Master extends EventEmitter { ready.mixin(this); - this.isProduction = isProduction(); + this.isProduction = isProduction(this.options); this.agentWorkerIndex = 0; this.closed = false; this[REAL_PORT] = this.options.port; @@ -56,7 +56,7 @@ class Master extends EventEmitter { this.isStarted = false; this.logger = new ConsoleLogger({ level: process.env.EGG_MASTER_LOGGER_LEVEL || 'INFO' }); this.logMethod = 'info'; - if (process.env.EGG_SERVER_ENV === 'local' || process.env.NODE_ENV === 'development') { + if (this.options.env === 'local' || process.env.NODE_ENV === 'development') { this.logMethod = 'debug'; } @@ -84,7 +84,7 @@ class Master extends EventEmitter { this.log('[master] start with options: %j', this.options); } this.log('[master] start with env: isProduction: %s, EGG_SERVER_ENV: %s, NODE_ENV: %s', - this.isProduction, process.env.EGG_SERVER_ENV, process.env.NODE_ENV); + this.isProduction, this.options.env, process.env.NODE_ENV); const startTime = Date.now(); @@ -150,7 +150,7 @@ class Master extends EventEmitter { // write pid to file if provided if (this.options.pidFile) { - mkdirp.sync(path.dirname(this.options.pidFile)); + fs.mkdirSync(path.dirname(this.options.pidFile), { recursive: true }); fs.writeFileSync(this.options.pidFile, process.pid.toString(), 'utf-8'); } @@ -586,10 +586,9 @@ class Master extends EventEmitter { module.exports = Master; -function isProduction() { - const serverEnv = process.env.EGG_SERVER_ENV; - if (serverEnv) { - return serverEnv !== 'local' && serverEnv !== 'unittest'; +function isProduction(options) { + if (options.env) { + return options.env !== 'local' && options.env !== 'unittest'; } return process.env.NODE_ENV === 'production'; } diff --git a/lib/utils/messenger.js b/lib/utils/messenger.js index 7c95b4e..5b72e98 100644 --- a/lib/utils/messenger.js +++ b/lib/utils/messenger.js @@ -1,6 +1,6 @@ 'use strict'; -const debug = require('debug')('egg-cluster:messenger'); +const debug = require('util').debuglog('egg-cluster:messenger'); const workerThreads = require('worker_threads'); diff --git a/lib/utils/mode/impl/worker_threads/app.js b/lib/utils/mode/impl/worker_threads/app.js index 85191a9..bf6404e 100644 --- a/lib/utils/mode/impl/worker_threads/app.js +++ b/lib/utils/mode/impl/worker_threads/app.js @@ -1,6 +1,6 @@ 'use strict'; -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../../timer'); const workerThreads = require('worker_threads'); const { BaseAppWorker, BaseAppUtils } = require('../../base/app'); diff --git a/lib/utils/options.js b/lib/utils/options.js index 4af846a..3999e47 100644 --- a/lib/utils/options.js +++ b/lib/utils/options.js @@ -18,11 +18,15 @@ module.exports = function(options) { https: false, startMode: 'process', ports: [], + env: null, }; options = extend(defaults, options); if (!options.workers) { options.workers = os.cpus().length; } + if (!options.env && process.env.EGG_SERVER_ENV) { + options.env = process.env.EGG_SERVER_ENV; + } const pkgPath = path.join(options.baseDir, 'package.json'); assert(fs.existsSync(pkgPath), `${pkgPath} should exist`); diff --git a/lib/utils/terminate.js b/lib/utils/terminate.js index 302aa2f..557d898 100644 --- a/lib/utils/terminate.js +++ b/lib/utils/terminate.js @@ -1,6 +1,6 @@ 'use strict'; -const sleep = require('mz-modules/sleep'); +const { sleep } = require('./timer'); const awaitEvent = require('await-event'); const pstree = require('ps-tree'); diff --git a/lib/utils/timer.js b/lib/utils/timer.js new file mode 100644 index 0000000..46cf972 --- /dev/null +++ b/lib/utils/timer.js @@ -0,0 +1,5 @@ +exports.sleep = ms => { + return new Promise(resolve => { + setTimeout(resolve, ms); + }); +}; diff --git a/package.json b/package.json index 5628775..980c080 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "scripts": { "lint": "eslint .", "test": "npm run lint -- --fix && npm run test-local", - "test-local": "egg-bin test", - "cov": "egg-bin cov --prerequire --timeout 100000", + "test-local": "egg-bin test --ts false", + "cov": "egg-bin cov --prerequire --timeout 100000 --ts false", "ci": "npm run lint && npm run cov" }, "files": [ @@ -33,15 +33,13 @@ "await-event": "^2.1.0", "cfork": "^1.7.1", "cluster-reload": "^1.0.2", - "debug": "^4.1.1", "depd": "^2.0.0", "detect-port": "^1.3.0", - "egg-logger": "^2.3.2", + "egg-logger": "^3.3.0", "egg-utils": "^2.4.1", "get-ready": "^2.0.1", "graceful-process": "^1.2.0", "is-type-of": "^1.2.1", - "mz-modules": "^2.1.0", "ps-tree": "^1.2.0", "semver": "^5.6.0", "sendmessage": "^1.1.0", @@ -51,16 +49,15 @@ "address": "^1.0.3", "coffee": "^5.2.1", "egg": "^3.9.0", - "egg-bin": "^5.6.1", + "egg-bin": "^6.4.0", "egg-errors": "^2.2.0", "egg-mock": "^5.4.0", "eslint": "^8.26.0", "eslint-config-egg": "^12.1.0", - "mz": "^2.7.0", "pedding": "^1.1.0", "supertest": "^4.0.0", - "ts-node": "^6.0.3", - "typescript": "^2.7.2" + "ts-node": "^10.9.1", + "typescript": "^5.0.4" }, "engines": { "node": ">= 14.0.0" diff --git a/test/agent_worker.test.js b/test/agent_worker.test.js index 9cb7c7f..91fcd24 100644 --- a/test/agent_worker.test.js +++ b/test/agent_worker.test.js @@ -1,11 +1,9 @@ -'use strict'; - const assert = require('assert'); const path = require('path'); const coffee = require('coffee'); const mm = require('egg-mock'); -const fs = require('mz/fs'); -const sleep = require('mz-modules/sleep'); +const { readFile } = require('fs/promises'); +const { sleep } = require('../lib/utils/timer'); const utils = require('./utils'); describe('test/agent_worker.test.js', () => { @@ -49,9 +47,9 @@ describe('test/agent_worker.test.js', () => { .end(); }); - it('should refork new agent_worker after app started', function* () { + it('should refork new agent_worker after app started', async () => { app = utils.cluster('apps/agent-die'); - yield app + await app // .debug() .expect('stdout', /\[master\] egg started on http:\/\/127.0.0.1:\d+/) .end(); @@ -61,7 +59,7 @@ describe('test/agent_worker.test.js', () => { action: 'kill-agent', }); - yield sleep(20000); + await sleep(20000); app.expect('stderr', /\[master\] agent_worker#1:\d+ died/); app.expect('stdout', /\[master\] try to start a new agent_worker after 1s .../); @@ -69,16 +67,16 @@ describe('test/agent_worker.test.js', () => { app.notExpect('stdout', /app_worker#2/); }); - it('should exit agent_worker when master die in accident', function* () { + it('should exit agent_worker when master die in accident', async () => { app = utils.cluster('apps/agent-die'); - yield app + await app // .debug() .expect('stdout', /\[master\] egg started on http:\/\/127.0.0.1:\d+/) .end(); // kill -9 master app.process.kill('SIGKILL'); - yield sleep(5000); + await sleep(5000); app.expect('stderr', /\[app_worker\] receive disconnect event in cluster fork mode, exitedAfterDisconnect:false/) .expect('stderr', /\[agent_worker\] receive disconnect event on child_process fork mode, exiting with code:110/) .expect('stderr', /\[agent_worker\] exit with code:110/); @@ -128,12 +126,12 @@ describe('test/agent_worker.test.js', () => { }); // process.send is not exist if started by spawn - it('master should not die if spawn error', function* () { + it('master should not die if spawn error', async () => { app = coffee.spawn('node', [ utils.getFilepath('apps/agent-die/start.js') ]); // app.debug(); app.close = () => app.proc.kill(); - yield sleep(3000); + await sleep(3000); app.emit('close', 0); app.expect('stderr', /Error: Cannot find module/); app.notExpect('stderr', /TypeError: process.send is not a function/); @@ -147,9 +145,9 @@ describe('test/agent_worker.test.js', () => { }); after(() => app.close()); - it('should support custom logger in agent', function* () { - yield sleep(1500); - const content = yield fs.readFile( + it('should support custom logger in agent', async () => { + await sleep(1500); + const content = await readFile( path.join(__dirname, 'fixtures/apps/custom-logger/logs/monitor.log'), 'utf8'); assert(content === 'hello monitor!\n'); }); diff --git a/test/app_worker.test.js b/test/app_worker.test.js index 7a48b39..12db96b 100644 --- a/test/app_worker.test.js +++ b/test/app_worker.test.js @@ -1,13 +1,11 @@ -'use strict'; - +const assert = require('assert'); +const { rm } = require('fs/promises'); const mm = require('egg-mock'); -const sleep = require('mz-modules/sleep'); -const rimraf = require('mz-modules/rimraf'); const request = require('supertest'); const urllib = require('urllib'); const address = require('address'); -const assert = require('assert'); const utils = require('./utils'); +const { sleep } = require('../lib/utils/timer'); describe('test/app_worker.test.js', () => { let app; @@ -83,16 +81,16 @@ describe('test/app_worker.test.js', () => { .end(); }); - it('should remove error listener after ready', function* () { + it('should remove error listener after ready', async () => { app = utils.cluster('apps/app-error-listeners'); - yield app.ready(); - yield app.httpRequest() + await app.ready(); + await app.httpRequest() .get('/') .expect({ beforeReady: 1, afterReady: 1, }); - yield app.close(); + await app.close(); }); it('should ignore listen to other port', done => { @@ -111,13 +109,13 @@ describe('test/app_worker.test.js', () => { }); after(mm.restore); - it('should restart', function* () { - yield app.httpRequest() + it('should restart', async () => { + await app.httpRequest() .get('/exit') .expect(200); // wait app worker restart - yield sleep(10000); + await sleep(10000); app.expect('stdout', /app_worker#1:\d+ disconnect/); app.expect('stdout', /app_worker#2:\d+ started/); @@ -133,16 +131,16 @@ describe('test/app_worker.test.js', () => { }); after(mm.restore); - it('should restart', function* () { + it('should restart', async () => { try { - yield app.httpRequest() + await app.httpRequest() .get('/exit'); } catch (_) { // ignore } // wait app worker restart - yield sleep(10000); + await sleep(10000); app.expect('stdout', /app_worker#1:\d+ disconnect/); app.expect('stderr', /don't fork new work/); @@ -158,16 +156,16 @@ describe('test/app_worker.test.js', () => { }); after(mm.restore); - it('should exit', function* () { + it('should exit', async () => { try { - yield app.httpRequest() + await app.httpRequest() .get('/kill?signal=SIGKILL'); } catch (_) { // ignore } // wait app worker restart - yield sleep(10000); + await sleep(10000); app.expect('stdout', /app_worker#1:\d+ disconnect/); app.expect('stderr', /don't fork new work/); @@ -194,63 +192,63 @@ describe('test/app_worker.test.js', () => { mm.env('default'); }); afterEach(mm.restore); - afterEach(() => rimraf(sockFile)); + afterEach(() => rm(sockFile, { force: true, recursive: true })); - it('should error then port is not specified', function* () { + it('should error then port is not specified', async () => { app = utils.cluster('apps/app-listen-without-port'); // app.debug(); - yield app.ready(); + await app.ready(); app.expect('code', 1); app.expect('stderr', /port should be number, but got null/); }); - it('should use port in config', function* () { + it('should use port in config', async () => { app = utils.cluster('apps/app-listen-port'); // app.debug(); - yield app.ready(); + await app.ready(); app.expect('code', 0); app.expect('stdout', /egg started on http:\/\/127.0.0.1:17010/); - yield request('http://0.0.0.0:17010') + await request('http://0.0.0.0:17010') .get('/') .expect('done') .expect(200); - yield request('http://127.0.0.1:17010') + await request('http://127.0.0.1:17010') .get('/') .expect('done') .expect(200); - yield request('http://localhost:17010') + await request('http://localhost:17010') .get('/') .expect('done') .expect(200); - yield request('http://127.0.0.1:17010') + await request('http://127.0.0.1:17010') .get('/port') .expect('17010') .expect(200); }); - it('should use hostname in config', function* () { + it('should use hostname in config', async () => { const url = address.ip() + ':17010'; app = utils.cluster('apps/app-listen-hostname'); // app.debug(); - yield app.ready(); + await app.ready(); app.expect('code', 0); app.expect('stdout', new RegExp(`egg started on http://${url}`)); - yield request(url) + await request(url) .get('/') .expect('done') .expect(200); try { - const response = yield urllib.request('http://127.0.0.1:17010', { dataType: 'text' }); + const response = await urllib.request('http://127.0.0.1:17010', { dataType: 'text' }); assert(response.status === 200); assert(response.data === 'done'); throw new Error('should not run'); @@ -259,40 +257,40 @@ describe('test/app_worker.test.js', () => { } }); - it('should use path in config', function* () { + it('should use path in config', async () => { app = utils.cluster('apps/app-listen-path'); // app.debug(); - yield app.ready(); + await app.ready(); app.expect('code', 0); app.expect('stdout', new RegExp(`egg started on ${sockFile}`)); const sock = encodeURIComponent(sockFile); - yield request(`http+unix://${sock}`) + await request(`http+unix://${sock}`) .get('/') .expect('done') .expect(200); }); }); - it('should exit when EADDRINUSE', function* () { + it('should exit when EADDRINUSE', async () => { mm.env('default'); app = utils.cluster('apps/app-server', { port: 17001 }); // app.debug(); - yield app.ready(); + await app.ready(); let app2; try { app2 = utils.cluster('apps/app-server', { port: 17001 }); app2.debug(); - yield app2.ready(); + await app2.ready(); app2.expect('code', 1); app2.expect('stderr', /\[app_worker] server got error: bind EADDRINUSE null:17001, code: EADDRINUSE/); app2.expect('stdout', /don't fork/); } finally { - yield app2.close(); + await app2.close(); } }); @@ -301,35 +299,35 @@ describe('test/app_worker.test.js', () => { mm.env('default'); }); - it('should refork when app_worker exit', function* () { + it('should refork when app_worker exit', async () => { app = utils.cluster('apps/app-die'); // app.debug(); - yield app.ready(); + await app.ready(); - yield app.httpRequest() + await app.httpRequest() .get('/exit') .expect(200); - yield sleep(10000); + await sleep(10000); app.expect('stdout', /app_worker#1:\d+ started at \d+/); app.expect('stderr', /new worker:\d+ fork/); app.expect('stdout', /app_worker#1:\d+ disconnect/); app.expect('stdout', /app_worker#2:\d+ started at \d+/); - yield app.httpRequest() + await app.httpRequest() .get('/exit') .expect(200); - yield sleep(10000); + await sleep(10000); app.expect('stdout', /app_worker#3:\d+ started at \d+/); }); - it('should not refork when starting', function* () { + it('should not refork when starting', async () => { app = utils.cluster('apps/app-start-error'); // app.debug(); - yield app.ready(); + await app.ready(); app.expect('stdout', /don't fork/); app.expect('stderr', /app_worker#1:\d+ start fail/); diff --git a/test/fixtures/apps/app-die/app/router.js b/test/fixtures/apps/app-die/app/router.js index 27612bd..f3e0124 100644 --- a/test/fixtures/apps/app-die/app/router.js +++ b/test/fixtures/apps/app-die/app/router.js @@ -1,10 +1,8 @@ -'use strict'; - -module.exports = function(app) { - app.get('/exit', function* () { +module.exports = app => { + app.get('/exit', ctx => { setTimeout(() => { throw new Error('exit'); }, 10); - this.body = 'exit'; + ctx.body = 'exit'; }); }; diff --git a/test/fixtures/apps/app-error-listeners/app/router.js b/test/fixtures/apps/app-error-listeners/app/router.js index ec214e4..8d3d3a2 100644 --- a/test/fixtures/apps/app-error-listeners/app/router.js +++ b/test/fixtures/apps/app-error-listeners/app/router.js @@ -1,8 +1,6 @@ -'use strict'; - -module.exports = function(app) { - app.get('/', function* () { - this.body = { +module.exports = app => { + app.get('/', ctx => { + ctx.body = { beforeReady: app.beforeReady, afterReady: app.listeners('error').length, }; diff --git a/test/fixtures/apps/app-kill/app/router.js b/test/fixtures/apps/app-kill/app/router.js index 8d1870a..9e89d5f 100644 --- a/test/fixtures/apps/app-kill/app/router.js +++ b/test/fixtures/apps/app-kill/app/router.js @@ -1,9 +1,7 @@ -'use strict'; - -module.exports = function(app) { - app.get('/kill', function* () { - const signal = this.query.signal && this.query.signal.toUpperCase(); - this.logger.info('kill by signal', signal, process.execArgv); +module.exports = app => { + app.get('/kill', ctx => { + const signal = ctx.query.signal && ctx.query.signal.toUpperCase(); + ctx.logger.info('kill by signal', signal, process.execArgv); process.kill(process.pid, signal); }); }; diff --git a/test/fixtures/apps/app-listen-hostname/app/router.js b/test/fixtures/apps/app-listen-hostname/app/router.js index fe75741..4c30986 100644 --- a/test/fixtures/apps/app-listen-hostname/app/router.js +++ b/test/fixtures/apps/app-listen-hostname/app/router.js @@ -1,7 +1,5 @@ -'use strict'; - module.exports = app => { - app.get('/', function* () { - this.body = 'done'; + app.get('/', ctx => { + ctx.body = 'done'; }); }; diff --git a/test/fixtures/apps/app-listen-path/app/router.js b/test/fixtures/apps/app-listen-path/app/router.js index fe75741..4c30986 100644 --- a/test/fixtures/apps/app-listen-path/app/router.js +++ b/test/fixtures/apps/app-listen-path/app/router.js @@ -1,7 +1,5 @@ -'use strict'; - module.exports = app => { - app.get('/', function* () { - this.body = 'done'; + app.get('/', ctx => { + ctx.body = 'done'; }); }; diff --git a/test/fixtures/apps/app-listen-port/app/router.js b/test/fixtures/apps/app-listen-port/app/router.js index 59e0067..26e4658 100644 --- a/test/fixtures/apps/app-listen-port/app/router.js +++ b/test/fixtures/apps/app-listen-port/app/router.js @@ -1,11 +1,9 @@ -'use strict'; - module.exports = app => { - app.get('/', function* () { - this.body = 'done'; + app.get('/', ctx => { + ctx.body = 'done'; }); - app.get('/port', function* () { - this.body = this.app._options.port; + app.get('/port', ctx => { + ctx.body = ctx.app._options.port; }); }; diff --git a/test/fixtures/apps/app-server/app/router.js b/test/fixtures/apps/app-server/app/router.js index b500704..2b0220f 100644 --- a/test/fixtures/apps/app-server/app/router.js +++ b/test/fixtures/apps/app-server/app/router.js @@ -1,7 +1,5 @@ -'use strict'; - module.exports = app => { - app.get('/', function* () { - this.body = this.app.serverEmit; + app.get('/', ctx => { + ctx.body = ctx.app.serverEmit; }); }; diff --git a/test/fixtures/apps/before-close/agent.js b/test/fixtures/apps/before-close/agent.js index d7531d8..1342e23 100644 --- a/test/fixtures/apps/before-close/agent.js +++ b/test/fixtures/apps/before-close/agent.js @@ -1,11 +1,9 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../../../lib/utils/timer'); module.exports = agent => { - agent.beforeClose(function* () { + agent.beforeClose(async () => { console.log('agent closing'); - yield sleep(10); + await sleep(10); console.log('agent closed'); }); }; diff --git a/test/fixtures/apps/before-close/app.js b/test/fixtures/apps/before-close/app.js index bd63471..2dc0855 100644 --- a/test/fixtures/apps/before-close/app.js +++ b/test/fixtures/apps/before-close/app.js @@ -1,11 +1,9 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../../../lib/utils/timer'); module.exports = app => { - app.beforeClose(function* () { + app.beforeClose(async () => { console.log('app closing'); - yield sleep(10); + await sleep(10); console.log('app closed'); }); }; diff --git a/test/fixtures/apps/cluster_mod_app/app/controller/home.js b/test/fixtures/apps/cluster_mod_app/app/controller/home.js index 58a4ac1..83beafe 100644 --- a/test/fixtures/apps/cluster_mod_app/app/controller/home.js +++ b/test/fixtures/apps/cluster_mod_app/app/controller/home.js @@ -1,5 +1,3 @@ -'use strict'; - -exports.index = function* () { - this.body = 'hi cluster'; +exports.index = ctx => { + ctx.body = 'hi cluster'; }; diff --git a/test/fixtures/apps/cluster_mod_sticky/app/controller/home.js b/test/fixtures/apps/cluster_mod_sticky/app/controller/home.js index 58a4ac1..83beafe 100644 --- a/test/fixtures/apps/cluster_mod_sticky/app/controller/home.js +++ b/test/fixtures/apps/cluster_mod_sticky/app/controller/home.js @@ -1,5 +1,3 @@ -'use strict'; - -exports.index = function* () { - this.body = 'hi cluster'; +exports.index = ctx => { + ctx.body = 'hi cluster'; }; diff --git a/test/fixtures/apps/egg-ready/app/router.js b/test/fixtures/apps/egg-ready/app/router.js index 4158ab4..393eeab 100644 --- a/test/fixtures/apps/egg-ready/app/router.js +++ b/test/fixtures/apps/egg-ready/app/router.js @@ -1,15 +1,13 @@ -'use strict'; - module.exports = app => { - app.get('/exception-app', function* () { + app.get('/exception-app', ctx => { setTimeout(() => { throw new Error('error'); }, 1); - this.body = 'done'; + ctx.body = 'done'; }); - app.get('/exception-agent', function* () { + app.get('/exception-agent', ctx => { app.messenger.sendToAgent('throw'); - this.body = 'done'; + ctx.body = 'done'; }); }; diff --git a/test/fixtures/apps/frameworkapp/app/controller/home.js b/test/fixtures/apps/frameworkapp/app/controller/home.js index d145ebd..eae6484 100644 --- a/test/fixtures/apps/frameworkapp/app/controller/home.js +++ b/test/fixtures/apps/frameworkapp/app/controller/home.js @@ -1,9 +1,7 @@ -'use strict'; - -module.exports = function* () { - this.body = { - frameworkCore: !!this.app.framework, - frameworkPlugin: !!this.app.custom, - frameworkAgent: !!this.app.agent, +module.exports = async ctx => { + ctx.body = { + frameworkCore: !!ctx.app.framework, + frameworkPlugin: !!ctx.app.custom, + frameworkAgent: !!ctx.app.agent, }; }; diff --git a/test/fixtures/apps/frameworkapp/app/router.js b/test/fixtures/apps/frameworkapp/app/router.js index b6cca73..fa70390 100644 --- a/test/fixtures/apps/frameworkapp/app/router.js +++ b/test/fixtures/apps/frameworkapp/app/router.js @@ -1,5 +1,3 @@ -'use strict'; - -module.exports = function(app) { +module.exports = app => { app.get('/', app.controller.home); }; diff --git a/test/fixtures/apps/https-server-config/app/router.js b/test/fixtures/apps/https-server-config/app/router.js index 1b162a9..b969d5d 100644 --- a/test/fixtures/apps/https-server-config/app/router.js +++ b/test/fixtures/apps/https-server-config/app/router.js @@ -1,7 +1,5 @@ -'use strict'; - module.exports = app => { - app.get('/', function* () { - this.body = 'https server config'; + app.get('/', ctx => { + ctx.body = 'https server config'; }); }; diff --git a/test/fixtures/apps/https-server/app/router.js b/test/fixtures/apps/https-server/app/router.js index 6111889..647a918 100644 --- a/test/fixtures/apps/https-server/app/router.js +++ b/test/fixtures/apps/https-server/app/router.js @@ -1,7 +1,5 @@ -'use strict'; - module.exports = app => { - app.get('/', function* () { - this.body = 'https server'; + app.get('/', ctx => { + ctx.body = 'https server'; }); }; diff --git a/test/fixtures/apps/pid/app/router.js b/test/fixtures/apps/pid/app/router.js index e894ebe..7f6ce3e 100644 --- a/test/fixtures/apps/pid/app/router.js +++ b/test/fixtures/apps/pid/app/router.js @@ -1,7 +1,5 @@ -'use strict'; - -module.exports = function(app) { - app.get('/exit', function* () { +module.exports = app => { + app.get('/exit', async () => { process.exit(1); }); }; diff --git a/test/fixtures/apps/script-start/start-server.js b/test/fixtures/apps/script-start/start-server.js index 471e576..b51ad58 100644 --- a/test/fixtures/apps/script-start/start-server.js +++ b/test/fixtures/apps/script-start/start-server.js @@ -1,9 +1,7 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../../../lib/utils/timer'); const utils = require('../../../utils'); -(async function() { +(async () => { const app = utils.cluster('apps/agent-exit'); app.debug(); await app.end(); diff --git a/test/fixtures/apps/worker-close-timeout/agent.js b/test/fixtures/apps/worker-close-timeout/agent.js index 37dd93b..b12a6b7 100644 --- a/test/fixtures/apps/worker-close-timeout/agent.js +++ b/test/fixtures/apps/worker-close-timeout/agent.js @@ -1,13 +1,11 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../../../lib/utils/timer'); module.exports = app => { const timeout = process.env.EGG_MASTER_CLOSE_TIMEOUT || 5000; - app.beforeClose(function* () { + app.beforeClose(async () => { app.logger.info('agent worker start close: ' + Date.now()); - yield sleep(timeout * 2); + await sleep(timeout * 2); app.logger.info('agent worker: never called after timeout'); }); }; diff --git a/test/fixtures/apps/worker-close-timeout/app.js b/test/fixtures/apps/worker-close-timeout/app.js index e6f055c..9daf114 100644 --- a/test/fixtures/apps/worker-close-timeout/app.js +++ b/test/fixtures/apps/worker-close-timeout/app.js @@ -1,13 +1,11 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../../../lib/utils/timer'); module.exports = app => { const timeout = process.env.EGG_MASTER_CLOSE_TIMEOUT || 5000; - app.beforeClose(function* () { + app.beforeClose(async () => { app.logger.info('app worker start close', Date.now(), timeout); - yield sleep(timeout * 2); + await sleep(timeout * 2); app.logger.info('app worker never called after timeout'); }); }; diff --git a/test/https.test.js b/test/https.test.js index 1b9500c..dda1800 100644 --- a/test/https.test.js +++ b/test/https.test.js @@ -1,9 +1,7 @@ -'use strict'; - const assert = require('assert'); +const path = require('path'); const mm = require('egg-mock'); const urllib = require('urllib'); -const path = require('path'); const utils = require('./utils'); describe('test/https.test.js', () => { @@ -14,7 +12,7 @@ describe('test/https.test.js', () => { describe('start https server with cluster options', () => { afterEach(() => app && app.close()); - it('should success with status 200', function* () { + it('should success with status 200', async () => { const baseDir = path.join(__dirname, 'fixtures/apps/https-server'); const options = { @@ -27,9 +25,9 @@ describe('test/https.test.js', () => { }, }; app = utils.cluster('apps/https-server', options); - yield app.ready(); + await app.ready(); - const response = yield urllib.request('https://127.0.0.1:8443', { + const response = await urllib.request('https://127.0.0.1:8443', { dataType: 'text', rejectUnauthorized: false, }); @@ -43,7 +41,7 @@ describe('test/https.test.js', () => { describe('start https server with app config cluster', () => { afterEach(() => app && app.close()); - it('should success with status 200', function* () { + it('should success with status 200', async () => { const baseDir = path.join(__dirname, 'fixtures/apps/https-server-config'); const options = { baseDir, @@ -51,9 +49,9 @@ describe('test/https.test.js', () => { }; app = utils.cluster('apps/https-server-config', options); - yield app.ready(); + await app.ready(); - const response = yield urllib.request('https://127.0.0.1:8443', { + const response = await urllib.request('https://127.0.0.1:8443', { dataType: 'text', rejectUnauthorized: false, }); diff --git a/test/master.test.js b/test/master.test.js index 3ada999..b991057 100644 --- a/test/master.test.js +++ b/test/master.test.js @@ -1,16 +1,15 @@ -'use strict'; - const path = require('path'); -const mm = require('egg-mock'); const assert = require('assert'); +const fs = require('fs'); +const { rm } = require('fs/promises'); +const cp = require('child_process'); const pedding = require('pedding'); -const { mkdirp, rimraf, sleep } = require('mz-modules'); +const mm = require('egg-mock'); const request = require('supertest'); const semver = require('semver'); const awaitEvent = require('await-event'); -const fs = require('mz/fs'); -const cp = require('child_process'); const utils = require('./utils'); +const { sleep } = require('../lib/utils/timer'); describe('test/master.test.js', () => { let app; @@ -82,7 +81,7 @@ describe('test/master.test.js', () => { describe('close master', () => { afterEach(() => app.close()); - it('master will close agent and app worker', function* () { + it('master will close agent and app worker', async () => { mm.env('local'); mm(process.env, 'EGG_APP_WORKER_LOGGER_LEVEL', 'INFO'); mm(process.env, 'EGG_AGENT_WORKER_LOGGER_LEVEL', 'INFO'); @@ -90,7 +89,7 @@ describe('test/master.test.js', () => { app = utils.cluster('apps/master-worker-started'); // app.debug(); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); @@ -103,7 +102,7 @@ describe('test/master.test.js', () => { // 2017-05-27 21:24:38,106 INFO 59066 [agent_worker] receive signal SIGTERM, exiting with code:0 // 2017-05-27 21:24:38,107 INFO 59066 [agent_worker] exit with code:0 app.proc.kill('SIGTERM'); - yield sleep(6000); + await sleep(6000); assert(app.proc.killed === true); app.expect('stdout', /INFO \d+ \[master\] master is killed by signal SIGTERM, closing/); app.expect('stdout', /\[master\] system memory: total \d+, free \d+/); @@ -119,12 +118,12 @@ describe('test/master.test.js', () => { app.expect('stdout', /INFO \d+ \[master\] wait 5000ms/); }); - it('master kill by SIGKILL and agent, app worker exit too', function* () { + it('master kill by SIGKILL and agent, app worker exit too', async () => { mm.env('local'); app = utils.cluster('apps/master-worker-started'); // app.debug(); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); @@ -135,7 +134,7 @@ describe('test/master.test.js', () => { // 2017-05-28 00:08:19,109 ERROR 59501 [agent_worker] exit with code:110 app.proc.kill('SIGKILL'); - yield sleep(6000); + await sleep(6000); assert(app.proc.killed === true); app.notExpect('stdout', /\[master\] master is killed by signal SIGTERM, closing/); app.notExpect('stdout', /\[master\] close done, exiting with code:0/); @@ -145,12 +144,12 @@ describe('test/master.test.js', () => { app.expect('stderr', /\[agent_worker\] exit with code:110/); }); - it('master kill by SIGKILL and exit multi workers', function* () { + it('master kill by SIGKILL and exit multi workers', async () => { mm.env('local'); app = utils.cluster('apps/master-worker-started', { workers: 4 }); // app.debug(); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); @@ -161,7 +160,7 @@ describe('test/master.test.js', () => { // 2017-05-28 00:08:19,109 ERROR 59501 [agent_worker] exit with code:110 app.proc.kill('SIGKILL'); - yield sleep(6000); + await sleep(6000); assert(app.proc.killed === true); app.notExpect('stdout', /\[master\] master is killed by signal SIGTERM, closing/); app.notExpect('stdout', /\[master\] close done, exiting with code:0/); @@ -171,12 +170,12 @@ describe('test/master.test.js', () => { app.expect('stderr', /\[agent_worker\] exit with code:110/); }); - it('use SIGTERM close master', function* () { + it('use SIGTERM close master', async () => { mm.env('local'); app = utils.cluster('apps/master-worker-started'); // app.debug(); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); @@ -190,7 +189,7 @@ describe('test/master.test.js', () => { // 2017-05-28 00:14:33,047 INFO 59715 [agent_worker] receive signal SIGTERM, exiting with code:0 // 2017-05-28 00:14:33,048 INFO 59715 [agent_worker] exit with code:0 app.proc.kill('SIGTERM'); - yield sleep(6000); + await sleep(6000); assert(app.proc.killed === true); app.expect('stdout', /\[master\] master is killed by signal SIGTERM, closing/); app.expect('stdout', /\[master\] system memory: total \d+, free \d+/); @@ -198,18 +197,18 @@ describe('test/master.test.js', () => { app.expect('stdout', /\[master\] exit with code:0/); }); - it('use SIGQUIT close master', function* () { + it('use SIGQUIT close master', async () => { mm.env('local'); app = utils.cluster('apps/master-worker-started'); // app.debug(); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); app.proc.kill('SIGQUIT'); - yield sleep(6000); + await sleep(6000); assert(app.proc.killed === true); app.expect('stdout', /\[master\] master is killed by signal SIGQUIT, closing/); @@ -218,19 +217,19 @@ describe('test/master.test.js', () => { app.expect('stdout', /\[master\] exit with code:0/); }); - it('use SIGINT close master', function* () { + it('use SIGINT close master', async () => { mm.env('local'); app = utils.cluster('apps/master-worker-started'); // app.debug(); - yield app + await app .expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); app.proc.kill('SIGINT'); - yield sleep(6000); + await sleep(6000); assert(app.proc.killed === true); app.expect('stdout', /\[master\] master is killed by signal SIGINT, closing/); @@ -239,7 +238,7 @@ describe('test/master.test.js', () => { app.expect('stdout', /\[master\] exit with code:0/); }); - it('should close when set EGG_MASTER_CLOSE_TIMEOUT', function* () { + it('should close when set EGG_MASTER_CLOSE_TIMEOUT', async () => { mm.env('local'); mm(process.env, 'EGG_APP_WORKER_LOGGER_LEVEL', 'INFO'); mm(process.env, 'EGG_AGENT_WORKER_LOGGER_LEVEL', 'INFO'); @@ -248,19 +247,19 @@ describe('test/master.test.js', () => { app = utils.cluster('apps/master-worker-started'); // app.debug(); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); app.proc.kill('SIGTERM'); - yield sleep(2000); + await sleep(2000); assert(app.proc.killed === true); app.expect('stdout', /INFO \d+ \[master\] exit with code:0/); app.expect('stdout', /INFO \d+ \[master\] wait 1000ms/); }); - it('kill order', function* () { + it('kill order', async () => { mm.env('local'); mm(process.env, 'EGG_APP_WORKER_LOGGER_LEVEL', 'INFO'); mm(process.env, 'EGG_AGENT_WORKER_LOGGER_LEVEL', 'INFO'); @@ -269,13 +268,13 @@ describe('test/master.test.js', () => { mm(process.env, 'EGG_AGENT_CLOSE_TIMEOUT', 1000); app = utils.cluster('apps/worker-close-timeout'); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); app.proc.kill('SIGTERM'); - yield awaitEvent(app.proc, 'exit'); + await awaitEvent(app.proc, 'exit'); app.expect('stdout', /INFO \d+ \[master\] exit with code:0/); app.expect('stdout', /INFO \d+ \[master\] wait 1000ms/); @@ -291,19 +290,19 @@ describe('test/master.test.js', () => { assert(!/agent worker never called after timeout/.test(app.stdout)); }); - it('close master will terminate all sub processes', function* () { + it('close master will terminate all sub processes', async () => { mm.env('local'); app = utils.cluster('apps/sub-process'); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) // .debug() .expect('stdout', /egg started/) .expect('code', 0) .end(); - yield sleep(3000); + await sleep(3000); app.proc.kill('SIGTERM'); - yield sleep(5000); + await sleep(5000); assert(app.proc.killed === true); app.expect('stdout', /worker1 \[\d+\] started/); app.expect('stdout', /worker2 \[\d+\] started/); @@ -324,19 +323,19 @@ describe('test/master.test.js', () => { assert(!alive(pid2)); }); - it('close master will terminate all sub processes with sigkill', function* () { + it('close master will terminate all sub processes with sigkill', async () => { mm.env('local'); app = utils.cluster('apps/sub-process-sigkill'); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) // .debug() .expect('stdout', /egg started/) .expect('code', 0) .end(); - yield sleep(5000); + await sleep(5000); app.proc.kill('SIGTERM'); - yield sleep(8000); + await sleep(8000); assert(app.proc.killed === true); app.expect('stdout', /worker1 \[\d+\] started/); app.expect('stdout', /worker2 \[\d+\] started/); @@ -363,46 +362,46 @@ describe('test/master.test.js', () => { const runDir = path.join(__dirname, './fixtures/apps/master-worker-started/run'); const pidFile = path.join(runDir, './pid'); - beforeEach(() => rimraf(runDir)); + beforeEach(() => rm(runDir, { force: true, recursive: true })); afterEach(() => app.close()); - it('master should write pid file and delete', function* () { + it('master should write pid file and delete', async () => { app = utils.cluster('apps/master-worker-started', { pidFile }); // app.debug(); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); assert(fs.existsSync(pidFile)); - const pid = yield fs.readFile(pidFile, 'utf-8'); + const pid = fs.readFileSync(pidFile, 'utf-8'); assert(pid === String(app.process.pid)); app.proc.kill('SIGTERM'); - yield sleep(6000); + await sleep(6000); app.expect('stdout', /\[master\] exit with code:0/); assert(!fs.existsSync(pidFile)); }); - it('master should ignore fail when delete pid file ', function* () { + it('master should ignore fail when delete pid file ', async () => { app = utils.cluster('apps/master-worker-started', { pidFile }); // app.debug(); - yield app.expect('stdout', /egg start/) + await app.expect('stdout', /egg start/) .expect('stdout', /egg started/) .expect('code', 0) .end(); assert(fs.existsSync(pidFile)); - const pid = yield fs.readFile(pidFile, 'utf-8'); + const pid = fs.readFileSync(pidFile, 'utf-8'); assert(pid === String(app.process.pid)); // delete fs.unlinkSync(pidFile); app.proc.kill('SIGTERM'); - yield sleep(6000); + await sleep(6000); app.expect('stdout', /\[master\] exit with code:0/); assert(!fs.existsSync(pidFile)); }); @@ -411,11 +410,11 @@ describe('test/master.test.js', () => { describe('Messenger', () => { afterEach(() => app.close()); - it('parent -> app/agent', function* () { + it('parent -> app/agent', async () => { app = utils.cluster('apps/messenger'); // app.debug(); - yield app.end(); + await app.end(); app.proc.send({ action: 'parent2app', @@ -428,7 +427,7 @@ describe('test/master.test.js', () => { to: 'agent', }); - yield sleep(1000); + await sleep(1000); app.expect('stdout', /parent -> agent/); app.expect('stdout', /parent -> app/); }); @@ -447,39 +446,39 @@ describe('test/master.test.js', () => { }, 1); }); - it('should app <-> agent', function* () { + it('should app <-> agent', async () => { app = utils.cluster('apps/messenger'); // app.debug(); - yield app.end(); + await app.end(); - yield sleep(10000); + await sleep(10000); app.expect('stdout', /app -> agent/); app.expect('stdout', /agent -> app/); app.expect('stdout', /app: agent2appbystring/); app.expect('stdout', /agent: app2agentbystring/); }); - it('should send multi app worker', function* () { + it('should send multi app worker', async () => { app = utils.cluster('apps/send-to-multiapp', { workers: 4 }); // app.debug(); - yield app.end(); - yield sleep(1000); + await app.end(); + await sleep(1000); app.expect('stdout', /\d+ '?got'?/); }); - it('sendTo should work', function* () { + it('sendTo should work', async () => { app = utils.cluster('apps/messenger'); // app.debug(); - yield app.end(); + await app.end(); app.proc.on('message', console.log); - yield sleep(1000); + await sleep(1000); app.expect('stdout', /app sendTo agent done/); app.expect('stdout', /agent sendTo agent done/); app.expect('stdout', /app sendTo app done/); app.expect('stdout', /agent sendTo app done/); }); - it('egg-script exit', function* () { + it('egg-script exit', async () => { app = { close: () => {}, }; @@ -498,7 +497,7 @@ describe('test/master.test.js', () => { p.on('message', msg => { masterPid = msg; }); - yield sleep(10000); + await sleep(10000); process.kill(masterPid); process.kill(p.pid); fs.closeSync(errFd); @@ -559,12 +558,12 @@ describe('test/master.test.js', () => { return app.ready(); }); - it('should restart 4 workers', function* () { + it('should restart 4 workers', async () => { app.process.send({ to: 'master', action: 'reload-worker', }); - yield sleep(20000); + await sleep(20000); app.expect('stdout', /app_worker#4:\d+ disconnect/); app.expect('stdout', /app_worker#8:\d+ started/); }); @@ -589,30 +588,30 @@ describe('test/master.test.js', () => { }); after(() => app.close()); - it('app/agent should recieve egg-ready', function* () { + it('app/agent should recieve egg-ready', async () => { // work for message sent - yield sleep(5000); + await sleep(5000); assert(readyMsg.match(/parent: port=\d+, address=http:\/\/127.0.0.1:\d+/)); app.expect('stdout', /agent receive egg-ready, with 1 workers/); app.expect('stdout', /app receive egg-ready, worker 1/); }); - it('should recieve egg-ready when app restart', function* () { - yield request(app.callback()) + it('should recieve egg-ready when app restart', async () => { + await request(app.callback()) .get('/exception-app') .expect(200); - yield sleep(5000); + await sleep(5000); app.expect('stdout', /app receive egg-ready, worker 2/); }); - it('should recieve egg-ready when agent restart', function* () { - yield request(app.callback()) + it('should recieve egg-ready when agent restart', async () => { + await request(app.callback()) .get('/exception-agent') .expect(200); - yield sleep(5000); + await sleep(5000); const matched = app.stdout.match(/agent receive egg-ready/g); assert(matched.length === 2); @@ -629,36 +628,35 @@ describe('test/master.test.js', () => { }); after(() => app.close()); - it('should every app worker will get message', function* () { - yield sleep(1000); + it('should every app worker will get message', async () => { + await sleep(1000); // start two workers app.expect('stdout', /#1 agent get 1 workers \[ \d+ \]/); app.expect('stdout', /#2 agent get 2 workers \[ \d+, \d+ \]/); }); - it('agent should get update message after app died', function* () { + it('agent should get update message after app died', async () => { try { - yield app.httpRequest() - .get('/exit') - .end(); + await app.httpRequest() + .get('/exit'); } catch (_) { // ignore } - yield sleep(9000); + await sleep(9000); // oh, one worker dead app.expect('stdout', /#3 agent get 1 workers \[ \d+ \]/); // never mind, fork new worker app.expect('stdout', /#4 agent get 2 workers \[ \d+, \d+ \]/); }); - it('agent should get message when agent restart', function* () { + it('agent should get message when agent restart', async () => { app.process.send({ to: 'agent', action: 'kill-agent', }); - yield sleep(9000); + await sleep(9000); app.expect('stdout', /#1 agent get 2 workers \[ \d+, \d+ \]/); }); }); @@ -674,13 +672,13 @@ describe('test/master.test.js', () => { }); after(() => app.close()); - it('agent start should get message', function* () { + it('agent start should get message', async () => { app.process.send({ to: 'agent', action: 'kill-agent', }); - yield sleep(9000); + await sleep(9000); app.expect('stdout', /#1 app get 0 workers \[\]/); app.expect('stdout', /#2 app get 1 workers \[ \d+ \]/); }); @@ -756,9 +754,9 @@ describe('test/master.test.js', () => { return app.ready(); }); - it('parent should recieve debug', function* () { + it('parent should recieve debug', async () => { // work for message sent - yield sleep(5000); + await sleep(5000); app.expect('stdout', /agent receive egg-ready, with 2 workers/); app.expect('stdout', /app receive egg-ready/); assert(result.agent.debugPort === 5800); @@ -791,9 +789,9 @@ describe('test/master.test.js', () => { return app.ready(); }); - it('parent should recieve debug', function* () { + it('parent should recieve debug', async () => { // work for message sent - yield sleep(5000); + await sleep(5000); app.expect('stdout', /agent receive egg-ready, with 2 workers/); app.expect('stdout', /app receive egg-ready/); assert(result.agent.debugPort === 5800); @@ -822,9 +820,9 @@ describe('test/master.test.js', () => { return app.ready(); }); - it('parent should not recieve debug', function* () { + it('parent should not recieve debug', async () => { // work for message sent - yield sleep(5000); + await sleep(5000); app.expect('stdout', /agent receive egg-ready, with 1 workers/); app.expect('stdout', /app receive egg-ready/); assert(!result); @@ -852,9 +850,9 @@ describe('test/master.test.js', () => { return app.ready(); }); - it('should not log err', function* () { + it('should not log err', async () => { // work for message sent - yield sleep(6000); + await sleep(6000); app.expect('stderr', /\[master] app_worker#.*signal: SIGKILL/); app.expect('stderr', /\[master] worker kill by debugger, exiting/); app.expect('stdout', /\[master] exit with code:0/); @@ -882,49 +880,49 @@ describe('test/master.test.js', () => { }); describe('agent and worker exception', () => { - it('should not exit when local env', function* () { + it('should not exit when local env', async () => { mm.env('local'); app = utils.cluster('apps/check-status'); // app.debug(); - yield app.ready(); - yield fs.writeFile(path.join(app.baseDir, 'logs/started'), ''); + await app.ready(); + fs.writeFileSync(path.join(app.baseDir, 'logs/started'), ''); - yield sleep(30000); + await sleep(30000); // process should exist assert(app.process.exitCode === null); app.process.kill('SIGINT'); }); - it('should exit when no agent after check 3 times', function* () { + it('should exit when no agent after check 3 times', async () => { mm.env('prod'); app = utils.cluster('apps/check-status'); // app.debug(); - yield app.ready(); - mkdirp.sync(path.join(app.baseDir, 'logs')); - yield fs.writeFile(path.join(app.baseDir, 'logs/started'), ''); + await app.ready(); + fs.mkdirSync(path.join(app.baseDir, 'logs'), { recursive: true }); + fs.writeFileSync(path.join(app.baseDir, 'logs/started'), ''); // kill agent worker and will exit when start app.process.send({ to: 'agent', action: 'kill' }); - yield awaitEvent(app.proc, 'exit'); + await awaitEvent(app.proc, 'exit'); assert(app.stderr.includes('nodejs.ClusterWorkerExceptionError: [master] 0 agent and 1 worker(s) alive, exit to avoid unknown state')); assert(app.stderr.includes('[master] exit with code:1')); }); - it('should exit when no app after check 3 times', function* () { + it('should exit when no app after check 3 times', async () => { mm.env('prod'); app = utils.cluster('apps/check-status'); // app.debug(); - yield app.ready(); - mkdirp.sync(path.join(app.baseDir, 'logs')); - yield fs.writeFile(path.join(app.baseDir, 'logs/started'), ''); + await app.ready(); + fs.mkdirSync(path.join(app.baseDir, 'logs'), { recursive: true }); + fs.writeFileSync(path.join(app.baseDir, 'logs/started'), ''); // kill app worker and wait checking app.process.send({ to: 'app', action: 'kill' }); - yield awaitEvent(app.proc, 'exit'); + await awaitEvent(app.proc, 'exit'); assert(app.stderr.includes('nodejs.ClusterWorkerExceptionError: [master] 1 agent and 0 worker(s) alive, exit to avoid unknown state')); assert(app.stderr.includes('[master] exit with code:1')); @@ -932,14 +930,14 @@ describe('test/master.test.js', () => { }); describe('beforeClose', () => { - it('should wait app close', function* () { + it('should wait app close', async () => { mm.env('local'); app = utils.cluster('apps/before-close'); // app.debug(); - yield app.ready(); + await app.ready(); - yield app.close(); - yield sleep(5000); + await app.close(); + await sleep(5000); app.expect('stdout', /app closing/); app.expect('stdout', /app closed/); diff --git a/test/options.test.js b/test/options.test.js index 9b6f578..08568a8 100644 --- a/test/options.test.js +++ b/test/options.test.js @@ -1,7 +1,5 @@ -'use strict'; - const path = require('path'); -const assert = require('assert'); +const { strict: assert } = require('assert'); const os = require('os'); const mm = require('egg-mock'); const parseOptions = require('../lib/utils/options'); @@ -97,6 +95,24 @@ describe('test/options.test.js', () => { }); }); + describe('env', () => { + it('default env is null', () => { + const options = parseOptions({}); + assert.equal(options.env, null); + }); + + it('custom env = prod', () => { + const options = parseOptions({ env: 'prod' }); + assert.equal(options.env, 'prod'); + }); + + it('default env set to process.env.EGG_SERVER_ENV', () => { + mm(process.env, 'EGG_SERVER_ENV', 'prod'); + const options = parseOptions({}); + assert.equal(options.env, 'prod'); + }); + }); + describe('options', () => { let app; before(() => {