Skip to content

Commit

Permalink
update to use max attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
allouis committed Apr 8, 2014
1 parent 8339d5b commit 7c40e8d
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,39 @@ var options;

var queue = {};

function addToDropboxQueue (message) {
var token = message.token;
var fileContent = message.file;
var fileName = message.fileName;
var saveObject = {
timeout: setTimeout(function(){
saveToDropBox(fileContent, fileName, token);
}, options.delayTime)
};
if (queue[fileName]) {
clearTimeout(queue[fileName].timeout);
function addToDropboxQueue(message) {
var id = message.fileName;
if (!message.attempts) {
message.attempts = 0;
}
queue[fileName] = saveObject;

if (message.attempts > options.maxAttempts) {
console.error('Max attempts reached for', id);
return;
}

var timeout = setTimeout(function saveToDropBoxWrapper() {
saveToDropBox(message);
delete queue[id];
}, options.delayTime);

if (queue[id]) {
clearTimeout(queue[id]);
}
queue[id] = timeout;
}

function saveToDropBox (file, name, token) {
function saveToDropBox(message) {
var client = new Dropbox.Client({
key: options.id,
secret: options.secret,
token: token // jshint ignore:line
token: message.token
});

client.writeFile(name, file, function (err) {
client.writeFile(message.fileName, message.file, function (err) {
if (err) {
message.attempts++;
addToDropboxQueue(message);
console.error(err);
}
});
Expand Down

0 comments on commit 7c40e8d

Please sign in to comment.