Code Monkey home page Code Monkey logo

pm2-notify's Introduction

How does it work?

We're listening on pm2 bus automatic events (manual exits won't be mailed). When an exit event happend we're keeping the pm2 process environnement in a queue. Those elements are then emailed through SMTP by using the famous nodemailer.

Templates can be customized, if you want to use another mailing protocol feel free to dig into the code! This is meant to be used out of the box with minimal configuration but it can't fit everyone needs.

If you need better email interactions have a look at Keymetrics!

Configuration

mail:
    from: "[email protected]"
    to: "[email protected]"
# see https://github.com/andris9/nodemailer-smtp-transport#usage
smtp:
    host: "smtp.gmail.com"
    port: 25
# Events list:
# - restart
# - delete
# - stop
# - restart overlimit
# - exit
# - start
# - online
events:
    - exit
template: './template.md'
# this is the process subject if there is only one event to be mailed
subject: '<%= process.name %> errored (<%= process.NODE_ENV %>)'
# if multiple events are going to be mailed, use a global subject:
multiple_subject: 'Error on <%= hostname %>'
#wait for 5 seconds after the last event before sending an email - avoid spam when a lot of events happened
polling: 5000 
#if events are sent continuously, an email will be sent after 5 minutes anyway
max_polling_time: 300000
#attach your process logs to the email
attach_logs: true 

Templating

This is how a standard pm2 object looks like:

{ event: 'exit',
  manually: false,
  process:
   { name: 'throw',
     vizion: true,
     autorestart: true,
     exec_mode: 'fork_mode',
     exec_interpreter: 'node',
     pm_exec_path: '/usr/share/nginx/www/pm2-notify/throw.js',
     pm_cwd: '/usr/share/nginx/www/pm2-notify',
     instances: 1,
     node_args: [],
     pm_out_log_path: '/home/abluchet/.pm2/logs/throw-out-0.log',
     pm_err_log_path: '/home/abluchet/.pm2/logs/throw-error-0.log',
     pm_pid_path: '/home/abluchet/.pm2/pids/throw-0.pid',
     NODE_APP_INSTANCE: 0,
     NODE_ENV: 'production',

     ... //your env variables here

     status: 'stopped',
     pm_uptime: 1434096604224,
     axm_actions: [],
     axm_monitor: {},
     axm_options: {},
     axm_dynamic: {},
     vizion_running: false,
     created_at: 1434096545628,
     pm_id: 0,
     restart_time: 19,
     unstable_restarts: 0,
     started_inside: false,
     command:
      { locked: false,
        metadata: {},
        started_at: null,
        finished_at: null,
        error: null },
     versioning: null,
     exit_code: 1 },
  at: 1434096607294 }

You can use all those variables in the template. We're adding the hostname and we format at in a date variable by using date = new Date(at).toString(). Templating is done through lodash and the email should be markdown-formatted.

Testing

Mail testing is hard to automate. There is a sample throwing process in the test directory. To test manually just launch pm2 start test/throw.js with node index.js. If you have an idea to unit test this feel free to open an issue.

pm2-notify's People

Contributors

holmeron avatar l-vo avatar soyuka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pm2-notify's Issues

can't catch process:restart

Hello,
I am trying to catch process:restart.
But after testing the bus system, the only events I saw emitted were stopping,stopped & online.

The config.example.yml says that the events list includes:

  • restart
  • delete
  • stop
  • restart overlimit
  • exit
  • start
  • online

Is there any way I can achieve that?

Error: ETIMEDOUT

pm2-monitor-1 { [Error: Connection timeout] code: 'ETIMEDOUT' }
pm2-monitor-1 Mail sent undefined

my config.yml:

mail:
    from: "[email protected]"
    to: "[email protected]"
# see https://github.com/andris9/nodemailer-smtp-transport#usage
smtp:
    host: "smtp.gmail.com"
    port: 25
    auth:
      user: "[email protected]"
      pass: "mypassword"

# Events list:
# - restart
# - delete
# - stop
# - restart overlimit
# - exit
# - start
# - online
events:
    - restart
    - exit
template: './template.md'
# this is the process subject if there is only one event to be mailed
subject: '<%= process.name %> errored (<%= process.NODE_ENV %>)'
# if multiple events are going to be mailed, use a global subject:
multiple_subject: 'Error on <%= hostname %>'
#wait for 5 seconds after each event before sending an email - avoid spam when a lot of events happened
polling: 2500
#attach your process logs to the email
attach_logs: true

Mail are not sent when events interval is less than polling time

For instance in the case where a task crash during its startup, exit events will be sent with a high frequency. The polling time usually used for avoiding spam will delay the mail send. As long as startup crash occurs, the notification can't be sent.

A solution can be to determine a max time after which the notification will be send anyway.

How to use this?

I have read the README but still could not get an idea on how to use it with my pm2 and my project. Any help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.