Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

entrypoint: implement spec #60

Closed
vmarchaud opened this issue Apr 30, 2018 · 3 comments
Closed

entrypoint: implement spec #60

vmarchaud opened this issue Apr 30, 2018 · 3 comments
Assignees

Comments

@vmarchaud
Copy link
Contributor

No description provided.

@Unitech
Copy link
Member

Unitech commented Apr 30, 2018

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

@wallet77
Copy link
Contributor

@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.

@wallet77
Copy link
Contributor

Done : #91

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants