How to use @pm2/io - 10 common examples

To help you get started, we’ve selected a few @pm2/io examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github keymetrics / app-playground / app.js View on Github external
* Measure things that increment or decrement
 */
const counter = io.counter({
  name: 'Downloads'
})

/**
 * Now let's create some remote action
 * And act on the Counter probe we just created
 */
io.action('decrement', { comment: 'Increment downloads' }, (cb) => {
  counter.dec()
  cb({ success: true })
})

io.action('increment', { comment : 'Decrement downloads' }, (cb) => {
  // Increment the previous counter
  counter.inc()
  cb({ success: true })
})

io.action('throw error', { comment: 'Throw a random error ' }, (cb) => {
  // Increment the previous counter
  throw new Error('This error will be caught!')
})

io.action('send event', { comment: 'Sends an event' }, (cb) => {
  io.emit('event:sent', {
    msg: 'You sent a custom event!'
  })
  cb('Sent event!')
})
github maxindelicato / makerads / server / rest / ads.js View on Github external
if (err) {
        console.error('failed to remove from cache');
        return rej(err);
      }
      res(quantity);
    });
  });
}
function clearCache(key) {
  return removeFromCache('*');
}

let cacheSize = 0;
// action to manually clear the ad cache
if (process.env.NODE_ENV !== 'development') {
  io.action('cache:clear', async cb => {
    console.log('clearing cache');
    try {
      await clearCache();
      cb({ success: true });
    } catch (err) {
      console.error(err);
      cb({ success: false, err: err.message });
    }
  });

  // setInterval(() => {
  //   cache.size((err, size) => {
  //     if (err) {
  //       console.error(err);
  //       return
  //     }
github keymetrics / app-playground / app.js View on Github external
// Increment the previous counter
  throw new Error('This error will be caught!')
})

io.action('send event', { comment: 'Sends an event' }, (cb) => {
  io.emit('event:sent', {
    msg: 'You sent a custom event!'
  })
  cb('Sent event!')
})

io.action('get env', (cb) => {
  cb(process.env)
})

io.action('modules version', { comment: 'Get modules version' }, (cb) => {
  cb(process.versions)
})

io.action('Action with params', { comment: 'Returns sent params' }, (data, cb) => {
  // Replies the received data
  cb(`Data received: ${JSON.stringify(data)}`)
})

/**
 * Create an action that hit the HTTP server we just created
 * So we can see how the meter probe behaves
 */
io.action('do:http:query', (cb) => {
  const options = {
    hostname: '127.0.0.1',
    port: 5005,
github keymetrics / app-playground / app.js View on Github external
})

io.action('modules version', { comment: 'Get modules version' }, (cb) => {
  cb(process.versions)
})

io.action('Action with params', { comment: 'Returns sent params' }, (data, cb) => {
  // Replies the received data
  cb(`Data received: ${JSON.stringify(data)}`)
})

/**
 * Create an action that hit the HTTP server we just created
 * So we can see how the meter probe behaves
 */
io.action('do:http:query', (cb) => {
  const options = {
    hostname: '127.0.0.1',
    port: 5005,
    path: '/users',
    method: 'GET',
    headers: { 'Content-Type': 'application/json' }
  }

  const req = http.request(options, (res) => {
    res.setEncoding('utf8')
    res.on('data', (data) => {
      console.log(data)
    })
  })

  req.on('error', (e) => {
github maxindelicato / makerads / server / rest / ads.js View on Github external
cb({ success: false, err: err.message });
    }
  });

  // setInterval(() => {
  //   cache.size((err, size) => {
  //     if (err) {
  //       console.error(err);
  //       return
  //     }
  //     cacheSize = size;
  //   });
  // }, 1000 * 10);
}

io.metric({
  type: 'metric',
  name: 'Cache size (bytes)',
  value: () => {
    return cacheSize;
  }
});
github keymetrics / pm2-server-monit / src / index.js View on Github external
}, function (err, conf) {
  if (err) {
    io.notifyError(err)
    return process.exit(1)
  }

  const cpu = new CPUMetrics(io, conf) // eslint-disable-line
  const network = new NetworkMetrics(io, conf)  // eslint-disable-line
  const disk = new DiskMetrics(io, conf)  // eslint-disable-line
  const memory = new MemoryMetrics(io, conf) // eslint-disable-line
  const fd = new FSMetrics(io, conf) // eslint-disable-line
  const tty = new TTYMetrics(io, conf) // eslint-disable-line
  const processes = new ProcessesMetrics(io, conf) // eslint-disable-line
  const actions = new MonitoringActions()
  actions.expose(io)
})
github keymetrics / pm2-elasticsearch / lib / stats.js View on Github external
], (err, data) => {
    if (err) {
      console.error(err)
      return io.notifyError(err)
    }
    // lets update metrics
    for (let i = 0, max = metrics.length; i < max; i++) {
      var metric = metrics[i]
      let stats = {}

      // depending on the stats source
      switch (metric.from) {
        case 'cluster': {
          stats = data[0][0]
          break
        }
        case 'cluster_health': {
          stats = data[2][0]
          break
        }
github keymetrics / app-playground / app.js View on Github external
randomVariable++
}, 400)

io.metric({
  name: 'Var count',
  value: () => {
    return randomVariable
  }
})

/**
 * Probe system #3 - Meter
 *
 * Probe things that are measured as events / interval.
 */
const meter = io.meter({
  name: 'req/min',
  timeframe: 60
})

/**
 * Use case for Meter Probe
 *
 * Create a mock http server
 */

http.createServer((req, res) => {
  // Then mark it at every connections
  meter.mark()
  res.end('Thanks')
}).listen(5005)
github keymetrics / pm2-elasticsearch / lib / stats.js View on Github external
metrics.forEach(function (metric) {
    // instanciate each probe
    metric.probe = io.metric({
      name: metric.name,
      unit: metric.unit || ''
    })
  })
}
github keymetrics / app-playground / app.js View on Github external
* Create a mock http server
 */

http.createServer((req, res) => {
  // Then mark it at every connections
  meter.mark()
  res.end('Thanks')
}).listen(5005)


/**
 * Probe system #4 - Counter
 *
 * Measure things that increment or decrement
 */
const counter = io.counter({
  name: 'Downloads'
})

/**
 * Now let's create some remote action
 * And act on the Counter probe we just created
 */
io.action('decrement', { comment: 'Increment downloads' }, (cb) => {
  counter.dec()
  cb({ success: true })
})

io.action('increment', { comment : 'Decrement downloads' }, (cb) => {
  // Increment the previous counter
  counter.inc()
  cb({ success: true })

@pm2/io

PM2.io NodeJS APM

Apache-2.0
Latest version published 2 years ago

Package Health Score

74 / 100
Full package analysis

Similar packages