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

我在 worker.js 里 console.log('ooxx'),在浏览器刷新页面会出现2~5条的ooxx,什么情况? #27

Closed
antife-yinyue opened this issue Dec 31, 2012 · 13 comments

Comments

@antife-yinyue
Copy link

No description provided.

@antife-yinyue
Copy link
Author

我用的是Express,worker.js里有连接MongoDB的代码,这样是不是多次连接数据库了?

@aleafs
Copy link
Owner

aleafs commented Dec 31, 2012

代码弄上来给我看看

@antife-yinyue
Copy link
Author

就你wiki上的代码

@antife-yinyue
Copy link
Author

就在http.emit('connection', socket)下一行写了console

@antife-yinyue
Copy link
Author

具体代码得4号上班才能贴上来了

@antife-yinyue
Copy link
Author

另,如果一服务器有多个独立app,master.js要写几份?独立于各项目之外,还是跟项目走?

@aleafs
Copy link
Owner

aleafs commented Jan 1, 2013

肯定是随项目走了

@aleafs
Copy link
Owner

aleafs commented Jan 1, 2013

你console.log的意思是每个HTTP请求进来都打印一个ooxx

@aleafs aleafs closed this as completed Jan 1, 2013
@antife-yinyue
Copy link
Author

master.js:

var master = require('pm').createMaster({
  pidfile: __dirname + '/tmp/jenny.pid'
})

master.register('http', __dirname + '/app.js', {
  listen: 3000
})

master.on('giveup', function(name, num, pause) {
  console.log('Master giveup to restart "%s" process after %d times. pm will try after %d ms.', name, fatals, pause)
})

master.dispatch()

app.js:

var express = require('express')
var mongoskin = require('mongoskin')
var app = express()

app.configure(function() {
  var db = mongoskin.db('xxxx', { safe: true })
  app.use(function(req, res, next) {
    req.db = db
    next()
  })
})

var http = require('http').createServer(app)
require('pm').createWorker().ready(function(socket, port) {
  http.emit('connection', socket)
  console.log(
    'Express server listening on port %d within %s environment.',
    port, app.get('env')
  )
})
$ node master.js

然后在浏览器访问localhost:3000

Snip20130104_1

@aleafs
Copy link
Owner

aleafs commented Jan 4, 2013

这个没错的,每个request打印一个console.log。
看你的意思应该是需要监听worker的listen事件,https://github.com/aleafs/pm/wiki/Worker

@antife-yinyue
Copy link
Author

app.js 调整下:

var express = require('express')
var mongoskin = require('mongoskin')
var app = express()

app.configure(function() {
  var db = mongoskin.db('xxxx', { safe: true })

  console.log('MongoDB connected')

  app.use(function(req, res, next) {
    req.db = db
    next()
  })
})

var http = require('http').createServer(app)
require('pm').createWorker().ready(function(socket, port) {
  http.emit('connection', socket)
})

console.log(
  'Express server listening on port %d within %s environment.',
  3000, app.get('env')
)
$ node master.js

Snip20130104_2

app.js被调用了 8 次?MongoDB也连了 8 次?

@aleafs
Copy link
Owner

aleafs commented Jan 4, 2013

你是8核CPU么?默认按CPU数起进程的

@antife-yinyue
Copy link
Author

4核的啊

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

2 participants