日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Node腳本遭遇異常時如何安全退出

一個 Node 相關(guān)的項目中,總是少不了跑腳本。跑一個腳本拉取配置、處理一些數(shù)據(jù)以及定時任務(wù)更是家常便飯。

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比松嶺網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式松嶺網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋松嶺地區(qū)。費用合理售后完善,10年實體公司更值得信賴。

在一些重要流程中能夠看到腳本的身影:

  • CI,用以測試、質(zhì)量保障及部署等
  • Docker,用以構(gòu)建鏡像
  • Cron,用以定時任務(wù)

如果在這些重要流程中腳本出錯無法及時發(fā)現(xiàn)問題,將有可能引發(fā)更加隱蔽的問題。

最近觀察項目鏡像構(gòu)建,會偶爾發(fā)現(xiàn)一兩個鏡像雖然構(gòu)建成功,但容器卻跑不起來的情況?!妇科湓颍且驗?Exit Code 的問題」。

Exit Code

什么是 exit code?

exit code 代表一個進(jìn)程的返回碼,通過系統(tǒng)調(diào)用 exit_group 來觸發(fā)。在 POSIX 中,0 代表正常的返回碼,1-255 代表異常返回碼,一般主動拋出的錯誤碼都是 1。在 Node 應(yīng)用中使用 process.exitCode = 1 來代表因不期望的異常而中斷。

這里有一張關(guān)于異常碼的附表 Appendix E. Exit Codes With Special Meanings[1]。

異常碼在操作系統(tǒng)中隨處可見,以下是一個關(guān)于 cat 命令的異常以及它的 exit code,并使用 strace 追蹤系統(tǒng)調(diào)用。

 
 
 
  1. $ cat a 
  2. cat: a: No such file or directory 
  3.  
  4. # 使用 strace 查看 cat 的系統(tǒng)調(diào)用 
  5. # -e 只顯示 write 與 exit_group 的系統(tǒng)調(diào)用 
  6. $ strace -e write,exit_group cat a 
  7. write(2, "cat: ", 5cat: )                    = 5 
  8. write(2, "a", 1a)                        = 1 
  9. write(2, ": No such file or directory", 27: No such file or directory) = 27 
  10. write(2, "\n", 1 
  11. )                       = 1 
  12. exit_group(1)                           = ? 
  13. +++ exited with 1 +++ 

從系統(tǒng)調(diào)用的最后一行可以看出,該進(jìn)行的 exit code 是 1,并把錯誤信息輸出到 stderr (標(biāo)準(zhǔn)錯誤的 fd 為 2) 中

如何查看 exit code

從 strace 中可以來判斷進(jìn)程的 exit code,但是不夠方便過于冗余,特別身處 shell 編程環(huán)境中。

「有一種簡單的方法,通過 echo $? 來確認(rèn)返回碼」

 
 
 
  1. $ cat a 
  2. cat: a: No such file or directory 
  3.  
  4. $ echo $? 

throw new Error與Promise.reject區(qū)別

以下是兩段代碼,第一段拋出一個異常,第二段 Promise.reject,兩段代碼都會如下打印出一段異常信息,那么兩者有什么區(qū)別?

 
 
 
  1. function error () { 
  2.   throw new Error('hello, error') 
  3.  
  4. error() 
  5.  
  6. // Output: 
  7.  
  8. // /Users/shanyue/Documents/note/demo.js:2 
  9. //   throw new Error('hello, world') 
  10. //   ^ 
  11. // 
  12. // Error: hello, world 
  13. //     at error (/Users/shanyue/Documents/note/demo.js:2:9) 
  14. //     at Object. (/Users/shanyue/Documents/note/demo.js:5:1) 
  15. //     at Module._compile (internal/modules/cjs/loader.js:701:30) 
 
 
 
  1. function error () { 
  2.   throw new Error('hello, error') 
  3.  
  4. error() 
  5.  
  6. // Output: 
  7.  
  8. // /Users/shanyue/Documents/note/demo.js:2 
  9. //   throw new Error('hello, world') 
  10. //   ^ 
  11. // 
  12. // Error: hello, world 
  13. //     at error (/Users/shanyue/Documents/note/demo.js:2:9) 
  14. //     at Object. (/Users/shanyue/Documents/note/demo.js:5:1) 
  15. //     at Module._compile (internal/modules/cjs/loader.js:701:30) 

在對上述兩個測試用例使用 echo $? 查看 exit code,我們會發(fā)現(xiàn) throw new Error() 的 exit code 為 1,而 Promise.reject() 的為 0。

「從操作系統(tǒng)的角度來講,exit code 為 0 代表進(jìn)程成功運行并退出,此時即使有 Promise.reject,操作系統(tǒng)也會視為它執(zhí)行成功?!?/p>

這在 Dockerfile 與 CI 中將留有安全隱患。

Dockerfile 在 node 中的注意點

當(dāng)使用 Dockerfile 構(gòu)建鏡像時,如果 RUN 的進(jìn)程返回非 0 的返回碼,構(gòu)建就會失敗。

「而在 Node 中的錯誤處理中,我們傾向于所有的異常都交由 async/await 來處理,而當(dāng)發(fā)生異常時,由于此時 exit code 為 0 并不會導(dǎo)致鏡像構(gòu)建失敗?!?/p>

這是一個淺顯易懂的含 Promise.reject() 問題的鏡像。

 
 
 
  1. FROM node:12-alpine 
  2.  
  3. RUN node -e "Promise.reject('hello, world')" 

構(gòu)建鏡像過程如下:「即使在構(gòu)建過程打印出了 unhandledPromiseRejection 信息,但是鏡像仍然構(gòu)建成功?!?/p>

 
 
 
  1. $ docker build -t demo . 
  2. Sending build context to Docker daemon  33.28kB 
  3. Step 1/2 : FROM node:12-alpine 
  4.  ---> 18f4bc975732 
  5. Step 2/2 : RUN node -e "Promise.reject('hello, world')" 
  6.  ---> Running in 79a6d53c5aa6 
  7. (node:1) UnhandledPromiseRejectionWarning: hello, world 
  8. (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) 
  9. (node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 
  10. Removing intermediate container 79a6d53c5aa6 
  11.  ---> 09f07eb993fe 
  12. Successfully built 09f07eb993fe 
  13. Successfully tagged demo:latest 

Promise.reject 腳本解決方案

能在編譯時能發(fā)現(xiàn)的問題,絕不要放在運行時。所以,構(gòu)建鏡像或 CI 中需要執(zhí)行 node 腳本時,對異常處理需要手動指定 process.exitCode = 1 來提前暴露問題

 
 
 
  1. runScript().catch(() => { 
  2.   process.exitCode = 1 
  3. }) 

在構(gòu)建鏡像時,也有關(guān)于異常解決方案的建議:

(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

根據(jù)提示,--unhandled-rejections=strict 將會把 Promise.reject 的退出碼設(shè)置為 1,并在將來的 node 版本中修正 Promise 異常退出碼。

 
 
 
  1. $ node --unhandled-rejections=strict error.js 

--unhandled-rejections=strict 的配置對 node 有版本要求:

Added in: v12.0.0, v10.17.0

By default all unhandled rejections trigger a warning plus a deprecation warning for the very first unhandled rejection in case no unhandledRejection hook is used.

總結(jié)

  • 當(dāng)進(jìn)程結(jié)束的 exit code 為非 0 時,系統(tǒng)會認(rèn)為該進(jìn)程執(zhí)行失敗
  • 通過 echo $? 可查看終端上一進(jìn)程的 exit code
  • Node 中 Promise.reject 時 exit code 為 0
  • Node 中可以通過 process.exitCode = 1 顯式設(shè)置 exit code
  • 在 Node12+ 中可以通過 node --unhandled-rejections=strict error.js執(zhí)行腳本,視 Promise.reject 的 exit code 為 1

新聞名稱:Node腳本遭遇異常時如何安全退出
當(dāng)前鏈接:http://www.dlmjj.cn/article/dhpsicj.html