【发布时间】:2012-11-02 20:42:43
【问题描述】:
我有一个 node-apn nodejs 脚本作为 AmazonWS 上的守护程序运行。守护程序运行良好,脚本保持运行并在它出现故障时返回,但我相信我在 node.js 上遇到了同步执行和退出问题。当我使用 process.exit() 释放进程时;即使所有 console.logs 输出都说他们已经发送了我的消息,但电话上永远不会收到它们。我决定删除出口,让进程在执行后“挂起”,所有消息都成功发送。这导致我使用 ASYNC 函数执行以下实现,但似乎正在发生相同的结果。任何人都可以对此提供见解吗? APN 或其他任何地方都不会引发错误。
function closeDB()
{
connection.end(function(err) {
if (err) {
console.log("ERROR: " + util.inspect(err, false, 5));
process.exit(1);
}
console.log("APNS-PUSH: COMPLETED.");
});
setTimeout(function(){process.exit();}, 50);
} // End of closeDB()
function apnsError(err, notification)
{
console.log(err);
console.log(notification);
closeDB();
}
function async(arg, callback)
{
apnsConnection.sendNotification(arg);
console.log(arg);
setTimeout(function() { callback(1); }, 100);
}
/**
* Our MySQL query callback.
*/
function queryCB(err, results)
{
//error in our all, report and exit
if (err) {
console.log("ERROR: " + util.inspect(err, false, 5));
closeDB();
}
if(results.length == 0)
{
closeDB();
}
var notes = [];
var count = 0;
try {
for( var i = 0; i < results.length; i++ ) {
var myDevice = new apns.Device(results[i]['udid']);
var note = new apns.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = results[i]["notification_count"];
note.sound = "ping.aiff";
note.alert = results[i]["message"];
note.device = myDevice;
connection.query('UPDATE `tbl_notifications` SET `sent`=1 WHERE `id`=' + results[i]["id"] , function(err, results) {
if(err)
{
console.log("ERROR: " + util.inspect(err, false, 5));
}
});
notes.push(note);
}
} catch( err ) {
console.log('error: ' + err)
}
console.log(notes.length);
notes.forEach(function(nNode) {
async(nNode, function(result) {
count++;
if(count == notes.length) {
closeDB();
}
})
});
} // End of queryCB()
【问题讨论】:
标签: iphone ios node.js push-notification apple-push-notifications