Node.js process μ’…λ£Œ 이벀트 감지

2021. 12. 23. 15:48Β·Web/Nodejs

🎯 Goals

  • Node.js ν”„λ‘œμ„ΈμŠ€ μ’…λ£Œμ‹œ 이벀트λ₯Ό λ°œμƒ, κ°μ§€ν•œλ‹€.
  • 자주 μ“°μ΄λŠ” process event 의 μš©λ‘€μ™€ μ£Όμ˜μ‚¬ν•­μ„ μ•Œμ•„λ³Έλ‹€.

 

 

process.exit

When to use?

ν”„λ‘œμ„ΈμŠ€ μ’…λ£Œ 이벀트λ₯Ό κ°μ§€κ±°λ‚˜ μΆ”κ°€ μž‘μ—…μ„ ν•˜κ³  싢을 λ•Œ

ex) λ‘œκΉ…

 

When this occur?

exit μ΄λ²€νŠΈκ°€ emit λ˜λŠ” 상황은 λ‹€μŒκ³Ό κ°™λ‹€.

  • μ½œμŠ€νƒ, 콜백큐, μž‘νκ°€ λͺ¨λ‘ λΉ„μ›Œμ Έμ„œ 더 이상 ν•  일이 없을 λ•Œ == μ •상 μ’…λ£Œ (exit code 0)
  • process.exit(N) 둜 직접 μ’…λ£Œ 이벀트 전달(emit).
  • Exception λ°œμƒμ‹œ exit code 1 (단, process.on('uncaughtException') λ―Έλ“±λ‘μ‹œ)
    λ“±λ‘ν–ˆμ„ 경우 exit code 0으둜 μ’…λ£Œ
    πŸ‘‰πŸ» λ‹Ήμ—°ν•œ 것이, ν•˜λ‚˜μ˜ ν…ŒμŠ€ν¬(이벀트 λ¦¬μŠ€λ„ˆ)κ°€ event queue 에 μΆ”κ°€λ˜κ³  μ •μƒμ μœΌλ‘œ 처리되면 μ’…λ£Œ μ½”λ“œκ°€ μžλ™μœΌλ‘œ 0으둜 λ³€ν™˜λœλ‹€.

 

⚠️ Execption 이 μ•„λ‹Œ async funciton λ‚΄μ—μ„œ throw  (reject) κ°€ λ°œμƒν•˜λ©΄ process.exit 이 λ°œλ™ν•˜μ§€ μ•ŠλŠ”λ‹€.
이 λ•ŒλŠ” process.on('unhandledRejection') 을 λ“±λ‘ν•΄μ„œ μ²˜λ¦¬ν•΄μ€˜μ•Όν•œλ‹€.

 

 

 

예제 μ½”λ“œ

// Use `beforeExit`
// When event loop has no additional work to schedule.
// Normally, Node.js process will exit when there is no work scheduled.
// But a listener registered on `beforeExit` event can make asynchronous call, thereby Node.js process to continue.

// explicit termination μ‹œ `beforeExit` 이 λ°œλ™ν•˜μ§€ μ•ŠμŒ.
// πŸ‘‰πŸ» μ΄λ•ŒλŠ” μ˜€λ‘œμ§€ 'exit' 이벀트만 λ°œμƒ λ˜λŠ” uncaughtException λ°œλ™.

// Explicit termination examples
// (1) calling process.exit()
// (2) uncaught exceptions.
// μœ„ 2κ°€μ§€ μΌ€μ΄μŠ€λŠ” Callback queue, job queue 에 일이 λ‚¨μ•„μžˆμ–΄λ„ λ°”λ‘œ μ’…λ£Œ.

process.on('beforeExit', (code)=>{
    logger.error({
        exitCode: code,
        message: 'Process exit!',
        timestamp: new Date(),
    });

});

// 0 외에도 λͺ¨λ‘ λ°œμƒ
process.on('exit', (code)=>{
    console.log(`exit code : ${code}`);

    if(code !== 0) {
        logger.error({
            exitCode: code,
            message: "I'm gone",
            timestamp: new Date(),
        });
    }

});

// [Default Behavior]
// 이게 μ—†μœΌλ©΄ 보톡 process.stderr.fd 에 찍힌 내역을 logging ν•˜κ³  μ’…λ£Œν•¨.
// exitCode 1 κ³Ό ν•¨κ»˜.

// process.on('uncaughtException') 은 이 κΈ°λ³Έ behavior λ₯Ό μ˜€λ²„λΌμ΄λ”© ν•˜λŠ”κ²ƒ.
process.on('uncaughtException', (err)=>{
    console.error('exception');
    logger.error({
        exitCode: err.name,
        message: err.message,
        timestamp: new Date(),
    });
    
    // μ•„λž˜μ™€ 같이 μΌλΆ€λŸ¬ exitCodeλ₯Ό μˆ˜μ •ν•  수 있음.
    process.exitCode = -1;
});

// 'SGITERM' means process was terminated gracefully
process.once('SIGTERM', (code)=>{
    console.log('SIGTERM exit code : ', code);
});

throw new Error('κΈ°λͺ¨λ¦¬');

 

 

 

 

πŸ€” μœ„ μ½”λ“œλŒ€λ‘œν•˜λ©΄  process.exit 이벀트 λ¦¬μŠ€λ„ˆκ°€ λ™μž‘ν•˜μ§€ μ•ŠλŠ”λ‹€. (λ‘œκΉ… X)

uncaughtException μ—μ„œ λ‘œκΉ…μ—†μ΄ λ°”λ‘œ ν”„λ‘œμ„ΈμŠ€ μ’…λ£Œ 

μ™œ λ‘œκΉ…μ΄ μ•ˆλ˜κ³  λλ‚ κΉŒ?

 

 

λͺ¨λ“  μ΄λ²€νŠΈκ°€ λ“±λ‘λ˜κΈ° 전에
λ„ˆλ¬΄ κΈ‰ν•˜κ²Œ throw ν•˜κ±°λ‚˜
process λ₯Ό μ’…λ£Œμ‹œμΌ°κΈ° λ•Œλ¬Έ.

 

// 1μ΄ˆν›„ μ§€μ—° μ‹€ν–‰
setTimeout(()=>{
    // process.exit(-1);
    throw new Error('κΈ°λͺ¨λ¦¬');
}, 1000)

μ΄λ ‡κ²Œλ§Œ 해도 λ‘œκΉ…μ΄ μ œλŒ€λ‘œ λœλ‹€.

 

 

Ctrl + C (SIGINT) 이벀트 κ°μ§€λŠ”?

$ process.on('SIGINT')

 

μœ„ μ˜ˆμ œμ—μ„œ Ctrl + C ν‚€λ₯Ό 눌러 SIGINT μ‹œκ·Έλ„μ„ 전솑할 λ•ŒλŠ” process.exit 이벀트 λ¦¬μŠ€λ„ˆκ°€ λ™μž‘ν•˜μ§€ μ•ŠλŠ”λ‹€.

 

λŒ€μ‹  λ‹€μŒκ³Ό 같은 μ½”λ“œλ‘œ μž‘μ•„λ‚Ό 수 μžˆλ‹€.


process.on('exit', (code)=>{
    console.log(`exit code : ${code}`);

// exit code κ°€ 0이 아닐 λ•Œλ§Œ μΆ”κ°€ λ‘œκΉ…
    if(code !== 0) {
        logger.error({
            exitCode: code,
            message: "I'm gone",
            timestamp: new Date(),
        });
    }
});

// log κ°€ 찍히고 
// λ³„λ„λ‘œ process.exit(0) κ³Ό 같은 μ½”λ“œλ₯Ό μΆ”κ°€ν•˜μ§€ μ•Šμ•„λ„.
// process.exitCode κ°€ 0으둜 μ’…λ£Œλœλ‹€.

process.once('SIGINT', ()=>{
    console.log("You've pressed Ctrl + C on this process.");
})

// Print:
// exit code: 0

 

 

 


πŸ”— Reference

 

Process | Node.js v17.3.0 Documentation

Process# Source Code: lib/process.js The process object provides information about, and control over, the current Node.js process. While it is available as a global, it is recommended to explicitly access it via require or import: import process from 'proc

nodejs.org

 

μ €μž‘μžν‘œμ‹œ (μƒˆμ°½μ—΄λ¦Ό)

'Web > Nodejs' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

node_modules 와 package.json  (0) 2021.12.08
[Node.js] λ‘œμ»¬μ— μ„€μΉ˜λœ νŒ¨ν‚€μ§€ λͺ…λ Ήμ–΄(CLI)둜 μ‹€ν–‰ν•˜κΈ°  (0) 2021.07.27
[Typescript] Creating Custom Types  (0) 2021.06.21
imagemagick module PDF->png κΆŒν•œ μ„€μ • Error  (0) 2020.02.06
Error: Cannot find module  (0) 2020.01.15
'Web/Nodejs' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • node_modules 와 package.json
  • [Node.js] λ‘œμ»¬μ— μ„€μΉ˜λœ νŒ¨ν‚€μ§€ λͺ…λ Ήμ–΄(CLI)둜 μ‹€ν–‰ν•˜κΈ°
  • [Typescript] Creating Custom Types
  • imagemagick module PDF->png κΆŒν•œ μ„€μ • Error
M_Falcon
M_Falcon
  • M_Falcon
    Falcon
    M_Falcon
  • 전체
    였늘
    μ–΄μ œ
    • λΆ„λ₯˜ 전체보기 (432)
      • Web (16)
        • Nodejs (14)
        • Javascript (23)
        • FrontEnd (4)
      • DataBase (39)
        • Fundamental (1)
        • Redis (4)
        • PostgreSQL (10)
        • NoSQL (4)
        • MySQL (9)
        • MSSQL (3)
        • Error (4)
      • Algorithm (79)
        • Algorithm (λ¬Έμ œν’€μ΄) (56)
        • Algorithm (이둠) (23)
      • JVM (65)
        • Spring (13)
        • JPA (5)
        • Kotlin (13)
        • Java (24)
        • Error (7)
      • 기타 (70)
        • Kafka (3)
        • Kubernetes (3)
        • Docker (13)
        • git (19)
        • μž‘λ™μ‚¬λ‹ˆ (27)
      • μž¬ν…Œν¬ (11)
        • 세무 (4)
        • 투자 (3)
        • λ³΄ν—˜ (0)
      • BlockChain (2)
        • BitCoin (0)
      • C (32)
        • C (10)
        • C++ (17)
        • Error (3)
      • Low Level (8)
        • OS (3)
        • μ‹œμŠ€ν…œ λ³΄μ•ˆ (5)
      • λ„€νŠΈμ›Œν¬ (3)
      • LINUX (30)
        • Linux (26)
        • Error (4)
      • μ €μž‘κΆŒκ³Ό 슀마트폰의 이해 (0)
      • 생각 λ­‰μΉ˜ (6)
      • ꢁ금증 (2)
      • Private (4)
        • 이직 κ²½ν—˜ (0)
        • κΏˆμ„ μ°Ύμ•„μ„œ (1)
      • Android (21)
        • OS (4)
  • λΈ”λ‘œκ·Έ 메뉴

    • ν™ˆ
    • WEB
    • μ•Œκ³ λ¦¬μ¦˜
    • DataBase
    • Linux
    • Mobile
    • C
    • λ°©λͺ…둝
  • 링크

    • github
  • 곡지사항

  • 인기 κΈ€

  • νƒœκ·Έ

    λ°±μ€€
    android
    Spring
    Bitcoin
    JPA
    Kotlin
    algorithm
    Programmers
    ubuntu
    Git
    java
    javascript
    PostgreSQL
    linux
    C++
    ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€
    kafka
    μ•Œκ³ λ¦¬μ¦˜
    database
    docker
  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.3
M_Falcon
Node.js process μ’…λ£Œ 이벀트 감지
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”