是否需要在Node.js中清除Buffer?如果是,怎么样?如果不是,这里有什么问题?

时间:2016-03-28 08:28:02

标签: javascript php node.js imagemagick gearman

我正在制作缩略图生成器,我正在使用Gearman在上传照片后立即在后台完成工作。我有一个用Node.js编写的工作程序,它基本上进行缩略图处理(对节点使用imagemagick)并将其插入数据库。客户端是用PHP编写的,它基本上发送原始图像的Photo_id和base64编码的字符串。 Node.js工作者获取输入base64编码的图像,并对其进行处理。一切都很好。我发现的问题是,当我通过Node调用Node.js工作时。 PHP第一次,所有3个缩略图(我为每个图像生成3个缩略图)生成,第二次和第三次以及所有3个都生成,但第四次只生成一个缩略图,并且之后无论我打电话给工人多少次,都没有生成缩略图。

我认为问题可能是缓冲区被填满,因为我使用Node.js Buffer将base64字符串转换为Binary,反之亦然。

我在缓冲区变量上使用js delete函数搜索了清除缓冲区的方法,比如将缓冲区变量指向null 。但是,这似乎都没有帮助。缓冲区未被清除或问题根本没有缓冲区。

请仔细阅读代码,并告诉我可能出现的问题。

PHP Gearman客户端

<?php
$client= new GearmanClient();
$client->addServer('127.0.0.1',4731);
print $client->do("infinite", "[\"12246\", \"Base 64 image string in here\"]");
?>

Node.js Gearman客户端

var fs = require('fs');
var db = require('./class.photo.js');
var im = require('imagemagick');
var Gearman = require('node-gearman/lib/gearman.js');
var gearman = new Gearman("127.0.0.1", 4731);
var db = new db();

function initializePic(id,base, req_type, callback) {
    console.log("Initializing thumbnail resize on " + id);

        var rawbase = base.split(",")[1];
        rawbase = rawbase.replace("\n", "");
        var buff = new Buffer(rawbase, 'base64');
        console.log(buff.length);
        var bin = buff.toString('binary');
        buff = null;

        console.time("image_" + req_type);
        imageResize(id, bin, properties[req_type].img_dest_width, properties[req_type].img_dest_height, imageRequestEnum[req_type], function (res) {
            if (res) {
                console.timeEnd(req_type);
                console.timeEnd("image_" + req_type);
            }
        });
}

function imageResize(id, bin, w, h, s, callback) {
    var fileName = id + '_' + w + '_' + h + '_thumb.jpg'
    console.log("Initializing resize process with filename " + fileName);
    im.resize({
        srcData: bin,
//        dstPath: fileName,
        width: w,
        height: h + '\!',
        quality: 0.7,
        strip: false,
        progressive: true,
    }, function (err, stdout, stderr) {
        if (err)
            throw err;
        if (stderr)
            throw stderr;

        var buff = new Buffer(stdout, "binary");
        console.log(buff.length);
        var b64 = buff.toString('base64');
        buff = null;

        if (b64) {
            b64 = "data:image/jpeg;base64," + b64;
            db.insertThumb(id, b64, s, function (res) {
                if (res) {
                    callback(true);
                }
            });
        }
    });
}


gearman.connect();

gearman.registerWorker("infinite", function (payload, worker) {
    var payload = payload.toString();
    var json = JSON.parse(payload);
    var id = json[0]; //getting the photo_id from the payload
    var base = json[1]; //Getting the base64 image data from the payload

    worker.end();
    console.time("user_image_small");
    initializePic(id,base, "user_image_small");
    console.time("user_image_medium");
    initializePic(id,base, "user_image_medium");
    console.time("profile_photo");
    initializePic(id,base, "profile_photo");
});

Node.js控制台响应

Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
profile_photo: 151ms
image_profile_photo: 150ms
user_image_medium: 211ms
image_user_image_medium: 210ms
user_image_small: 218ms
image_user_image_small: 217ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 98ms
image_user_image_small: 96ms
profile_photo: 142ms
image_profile_photo: 141ms
user_image_medium: 147ms
image_user_image_medium: 145ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 90ms
image_user_image_small: 89ms
user_image_medium: 141ms
image_user_image_medium: 140ms
profile_photo: 138ms
image_profile_photo: 137ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
user_image_small: 94ms
image_user_image_small: 93ms
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_245_245_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_50_50_thumb.jpg
Initializing thumbnail resize on 12246
Initializing resize process with filename 12246_90_90_thumb.jpg
Initializing thumbnail resize on 12246

请注意,在最近的几次尝试中,它实际上并没有创建任何缩略图。显示所花费的时间基本上意味着缩略图创建过程已完成。

我不确定问题是什么。寻求帮助。

2 个答案:

答案 0 :(得分:1)

经过多次故障排除后,发现问题出在node-mysql包中。使用node.js的mysql包解决了这个问题。查看我的其他question and answer了解更多详情。

答案 1 :(得分:0)

在我看来,问题是你在实际完成工作之前关闭工人。

这样的东西
gearman.registerWorker("infinite", function (payload, worker) {
    var payload = payload.toString();
    var json = JSON.parse(payload);
    var id = json[0]; //getting the photo_id from the payload
    var base = json[1]; //Getting the base64 image data from the payload

    // not actually work with your code right now, but you got the idea
    Promise.all([        
        initializePic(id, base, "user_image_small"),
        initializePic(id, base, "user_image_medium"),
        initializePic(id, base, "profile_photo")
    ]).then(() => {
        // should be called after all processing is done
        worker.end();
    });
});