ntt360 / pmon2

golang进程管理工具(golang process manager)
Apache License 2.0
988 stars 133 forks source link

What is the point of the system daemon? #20

Closed joe-at-startupmedia closed 11 months ago

joe-at-startupmedia commented 11 months ago
systemctl start pmon2

What is the point of starting this daemon? I am able to add processes without starting it using the following:

pmon2 run /home/api_user/go/bin/helloworld --user api_user
joe-at-startupmedia commented 11 months ago

My guess is that the deamon process spawned by systemctl start pmon2 monitors all processes in the database and restarts them when they are unhealthy? pm2 automatically starts the god deamon when you run pm2 start [service]. Besides this, Is there anything else I'm missing?

joe-at-startupmedia commented 11 months ago

According to the code. This file explains how pmond works: https://github.com/ntt360/pmon2/blob/483fb1f80656fa6b489d8f7ebd47f16f7b86bb97/app/god/monitor.go

In summary it does the following in an infinite loop that executes every 500 ms.

  1. finds all processes in the database that have a status of running or failed
  2. loadOrStores the pid/process name into a syncMap and returns early if loaded
  3. If stored, it proceeds to execute a closure that checks if the process is still: a. in the database b. has a status of running or failed c. hasn't been updated since 5 seconds ago
  4. Deletes the pid/process from the syncMap when the closure exits.
  5. Checks if the process is running on the OS
  6. Mark the process as failed if the process doesn't have autostart enabled OR
  7. Attempts to start the process.