我当前正在运行节点js服务器,该服务器可通过我的apache服务器上的特定URL进行访问。
整个代码是:
// server.js
var webshot = require('./lib/webshot');
var fs = require('fs');
var http = require('http');
var bodyParser = require('body-parser');
const used = process.memoryUsage();
var express = require('express');
var app = express();
app.use( bodyParser.urlencoded() );
// your express configuration here
var httpServer = http.createServer(app);
// For http
httpServer.listen(8080);
app.post('/', function (req, res) {
console.log(req.body);
var firstLine = req.body.firstLine;
var secondLine = req.body.secondLine;
var previewID = req.body.previewId;
var productPlate = req.body.prodName;
res.header('Access-Control-Allow-Origin', 'https://citylocs.com');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
takeWebshot(firstLine, secondLine, previewID, productPlate)
res.end()
});
function takeWebshot(fLine, sLine, pID, prodPlate) {
var options = {
onLoadFinished: {
fn: function() {
document.getElementById("user_input").value=this.fLineSnapshot;
document.getElementById("second_user_input").value=this.sLineSnapshot;
document.getElementById("preview_btn").click();
},
context: {
fLineSnapshot: fLine,
sLineSnapshot: sLine,
}
},
takeShotOnCallback: true,
captureSelector: '#img_preview_fancybox',
licensePlate: 'Guzman Plate'
};
webshot('example.com/preview/productpreview/testy.html?prod=' + prodPlate, '../screenshot/' + pID +'.png', options, function(err) {
if(!err) { process.exit() }
else { console.log(err);
process.exit() }
});
};
它基本上需要一些数据,并通过
webshot()
方法使用幻影js来制作网站的屏幕快照。为了节省内存,因为该函数多次运行,所以在回调中完成process.exit()
后,我有了webshot()
。我的期望是server.js存在。然后在pm2之前停用。问题是一段时间后出现内存错误。检查后运行ps aux --sort -rss
我得到这个:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 12019 0.0 2.5 224036 105992 ? Ss 04:13 0:06 /usr/local/cpanel/3rdparty/perl/528/bin/perl -T -w /usr/local/cpanel/3rdparty/bin/spamd --max-spare=
root 12237 0.0 2.4 225184 103664 ? S 04:26 0:03 spamd child
root 12238 0.0 2.4 224036 102128 ? S 04:26 0:00 spamd child
root 12239 0.0 2.4 224036 102124 ? S 04:26 0:00 spamd child
mysql 1592 0.2 1.3 1586436 57104 ? Sl Aug29 1:56 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user
named 1056 0.0 0.9 1924092 41828 ? Ssl Aug29 0:00 /usr/sbin/named -u named
root 1380 0.0 0.8 902416 37480 ? Ssl Aug29 0:19 PM2 v3.5.1: God Daemon (/root/.pm2)
root 5032 0.0 0.8 2037540 35732 ? Sl Aug29 0:01 phantomjs /home/ogdi/public_html/preview/productpreview/node-webshot/lib/webshot.phantom.js {
root 9778 0.0 0.8 2037500 35708 ? Sl 02:57 0:01 phantomjs /home/ogdi/public_html/preview/productpreview/node-webshot/lib/webshot.phantom.js {
root 18725 0.0 0.8 2037500 35680 ? Sl 08:09 0:00 phantomjs /home/ogdi/public_html/preview/productpreview/node-webshot/lib/webshot.phantom.js {
root 7577 0.0 0.8 2037460 35676 ? Sl 01:46 0:01 phantomjs /home/ogdi/public_html/preview/productpreview/node-webshot/lib/webshot.phantom.js {
它告诉我,自昨天以来,幻影js脚本仍然处于 Activity 状态。它出现了几次。如果
process.exit()
成功,为什么会这样?编辑:
我手动杀死了一堆这些幻影js命令。我又诊断了一个。并显示STAT Sl,表示它是Sleep(多线程)。根据我的读物。
Webshot链接:https://raw.githubusercontent.com/brenden/node-webshot/master/lib/webshot.js
Webshot幻像链接:https://raw.githubusercontent.com/brenden/node-webshot/master/lib/webshot.phantom.js
最佳答案
如果发生错误,则不调用process.exit()
。如果发生错误,流程中是否有继续的地方?
webshot('example.com/preview/productpreview/testy.html?prod=' + prodPlate, '../screenshot/' + pID +'.png', options, function(err) {
if (err) { console.log(err) }
process.exit(err)
});