Code Monkey home page Code Monkey logo

Comments (3)

Unitech avatar Unitech commented on May 19, 2024
const Entrypoint = require('@pm2/pm2.io').Entrypoint

class App extends Entrypoint {

  // The .onStart is called by Entrypoint when initializing the app
  onStart(cb) {
      this.server = http.createServer((req, res) => {
        res.send('Hello')
      })
   
      // The cb send process.send('ready') to PM2 
      // if we manage to retrieve the http instance we could bind it to
      // https://github.com/hunterloftis/stoppable
      this.server.listen(8000, cb)
  }

  // The .onStop is called when the app will exit (exception, rejection, sigint, sigstop...)
  onStop(cb) {
    
  }

  // Just a method to cleanly list metrics
  metrics() {
     this.meter('stuff-metrics', () => {
       return 42
     })
  }

  // To cleanly list diagnostics
  diagnostics() {
     this.on('getEnv', (cb) => {
       cb(null, process.env)
     })
  }
}

 module.exports = new Entrypoint
class Entrypoint {
  constructor () {
     this.io = io.init(this.conf())

     this.onStart(err => () {
       this.metrics()
       this.diagnostics()
       this.io.onExit((err) => {
         this.onStop(err, () => {
           this.io.destroy()
         })
       })
       process.send('ready')
     })
  },

  onStart () {
    throw new Error('Entrypoint onStart() not specified')
  },  

  onStop () {
    throw new Error('Entrypoint onStop() not specified')
  },

  conf () {
    return { defaultConf }
  }
}

// wait_ready? listen_timeout? kill_timeout?
// this.io.waitFor('app') pour onStart demare app
// auto require de pm2 lightweight
// ipc pubsub

This is a simple draft, based on my intuition, @vmarchaud @wallet77 feedback welcome on this api design

from pm2-io-apm.

wallet77 avatar wallet77 commented on May 19, 2024

@vmarchaud @Unitech

I think onSotp and onStart should exists by default and just execute the callback :
onStart/onStop (cb) {
cb()
}

In this case user can override only onStop for example.

from pm2-io-apm.

wallet77 avatar wallet77 commented on May 19, 2024

Done : #91

from pm2-io-apm.

Related Issues (20)

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.