2016-03-28 73 views
1

我正在製作一個縮略圖生成器,並且我使用Gearman在上傳照片後立即在後臺完成作業。我有一個用Node.js編寫的工人,它基本上做了縮略圖處理(使用imagemagick作爲節點)並將其插入到數據庫中。客戶端是用PHP編寫的,它基本上發送Photo_id和原始圖像的base64編碼字符串。 Node.js工作人員獲取輸入的base64編碼圖像並對其進行處理。一切工作正常。我發現的問題是,當我通過調用Node.js工作。 PHP第一次生成所有3個縮略圖(我爲每個圖像生成3個縮略圖),第二次和第三次生成所有3個縮略圖,但第四次只生成一個縮略圖,並且在不管我多少次打電話給工人,都不會生成縮略圖。是否需要在Node.js中清除Buffer?如果是,如何?如果不是,這裏有什麼問題?

我認爲問題可能是緩衝區被填充,因爲我使用Node.js Buffer將base64字符串轉換爲Binary,反之亦然。

我搜索了清除緩衝區的方法,如指向緩衝區變量爲空,在緩衝區變量上使用js delete函數。但是,這似乎沒有任何幫助。緩衝區沒有被清除,或者問題根本不在緩衝區中。

請仔細閱讀代碼,並告訴我可能是什麼問題。

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 

注意在最後幾次嘗試中,它實際上並沒有創建任何縮略圖。顯示所花費的時間基本上意味着縮略圖創建過程完成。

我不確定這個問題可能是什麼。尋找一些幫助。

回答

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(); 
    }); 
}); 
+0

如果我沒有錯,worker.end()只是讓PHP Gearman客戶端停止等待響應。我一開始就需要它,因爲我不希望PHP方面等待響應。此外,我一直在嘗試,我發現,即使Gearman不被考慮,我在Node.js腳本中多次調用'initializePic'函數,或者一個接一個地調用'initializePic'函數,它只處理4個到5次。我無法查明確切的問題。 –

1

經過大量的故障排除,發現該問題是與node-mysql包。使用node.js的mysql包解決了這個問題。查看我的其他question and answer瞭解更多詳情。

相關問題