restart abiword on crash and give the user feedback about bad import files

This commit is contained in:
Peter 'Pita' Martischka 2012-02-27 00:22:53 +01:00
parent 00c3281a02
commit ed8aff22d4
3 changed files with 75 additions and 95 deletions

View file

@ -115,7 +115,15 @@ exports.doImport = function(req, res, padId)
{
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
destFile = tempDirectory + "eplite_import_" + randNum + ".txt";
abiword.convertFile(srcFile, destFile, "txt", callback);
abiword.convertFile(srcFile, destFile, "txt", function(err){
//catch convert errors
if(err){
console.warn("Converting Error:", err);
return callback("convertFailed");
} else {
callback();
}
});
},
//get the pad object
@ -176,16 +184,18 @@ exports.doImport = function(req, res, padId)
}
], function(err)
{
//the upload failed, there is nothing we can do, send a 500
if(err == "uploadFailed")
var status = "ok";
//check for known errors and replace the status
if(err == "uploadFailed" || err == "convertFailed")
{
res.send(500);
return;
status = err;
err = null;
}
ERR(err);
//close the connection
res.send("ok");
res.send("<script>document.domain = document.domain; var impexp = window.top.require('/pad_impexp').padimpexp.handleFrameCall('" + status + "'); </script>", 200);
});
}

View file

@ -53,7 +53,7 @@ if(os.type().indexOf("Windows") > -1)
abiword.on('exit', function (code)
{
if(code != 0) {
throw "Abiword died with exit code " + code;
return callback("Abiword died with exit code " + code);
}
if(stdoutBuffer != "")
@ -75,52 +75,54 @@ if(os.type().indexOf("Windows") > -1)
else
{
//spawn the abiword process
var abiword = spawn(settings.abiword, ["--plugin", "AbiCommand"]);
//append error messages to the buffer
abiword.stderr.on('data', function (data)
{
stdoutBuffer += data.toString();
});
//throw exceptions if abiword is dieing
abiword.on('exit', function (code)
{
throw "Abiword died with exit code " + code;
});
//delegate the processing of stdout to a other function
abiword.stdout.on('data',onAbiwordStdout);
var abiword;
var stdoutCallback = null;
var stdoutBuffer = "";
var firstPrompt = true;
var spawnAbiword = function (){
abiword = spawn(settings.abiword, ["--plugin", "AbiCommand"]);
var stdoutBuffer = "";
var firstPrompt = true;
function onAbiwordStdout(data)
{
//add data to buffer
stdoutBuffer+=data.toString();
//we're searching for the prompt, cause this means everything we need is in the buffer
if(stdoutBuffer.search("AbiWord:>") != -1)
//append error messages to the buffer
abiword.stderr.on('data', function (data)
{
//filter the feedback message
var err = stdoutBuffer.search("OK") != -1 ? null : stdoutBuffer;
stdoutBuffer += data.toString();
});
//abiword died, let's restart abiword and return an error with the callback
abiword.on('exit', function (code)
{
spawnAbiword();
stdoutCallback("Abiword died with exit code " + code);
});
//delegate the processing of stdout to a other function
abiword.stdout.on('data',function (data)
{
//add data to buffer
stdoutBuffer+=data.toString();
//reset the buffer
stdoutBuffer = "";
//call the callback with the error message
//skip the first prompt
if(stdoutCallback != null && !firstPrompt)
//we're searching for the prompt, cause this means everything we need is in the buffer
if(stdoutBuffer.search("AbiWord:>") != -1)
{
stdoutCallback(err);
stdoutCallback = null;
//filter the feedback message
var err = stdoutBuffer.search("OK") != -1 ? null : stdoutBuffer;
//reset the buffer
stdoutBuffer = "";
//call the callback with the error message
//skip the first prompt
if(stdoutCallback != null && !firstPrompt)
{
stdoutCallback(err);
stdoutCallback = null;
}
firstPrompt = false;
}
firstPrompt = false;
}
});
}
spawnAbiword();
doConvertTask = function(task, callback)
{
@ -130,6 +132,7 @@ else
stdoutCallback = function (err)
{
callback();
console.log("queue continue");
task.callback(err);
};
}