Skip to content

Commit

Permalink
chore: Synchronize socket PID retrieval (fossasia#2235)
Browse files Browse the repository at this point in the history
And improve error handling
  • Loading branch information
iamareebjamal authored Mar 7, 2020
1 parent e5ebd22 commit 68daf6a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ configmap.yml.secret

.tool-versions
.env
.vscode
41 changes: 36 additions & 5 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,30 @@ io.on('connection', function(socket) {
});
});

function getSocket(jobId) {
let tries = 0;
return new Promise((resolve, reject) => {
const tryGettingSocket = () => {
if (tries > 1)
console.log('Trying to get socket for job', jobId, 'Attempt: ', tries);
const socket = socketObj[jobId];
if (socket) {
resolve(socket);
} else {
if (tries > 5) {
reject(new Error('Socket not found in object after ' + tries + ' tries'));
return;
}

tries++;
setTimeout(tryGettingSocket, 2**tries);
}
}

tryGettingSocket();
});
}

queue.on('ready', function() {
queue.process(function(job, done) {
console.log('processing job ' + job.id);
Expand All @@ -132,12 +156,19 @@ queue.on('ready', function() {
resolve(queue.getJobs('waiting', {start: 0, end: 25}));
});

generator.createDistDir(job.data, socketObj[processId], done);
jobs.then(function(waitingJobs) {
waitingJobs.forEach(function(waitingJob) {
logger.addLog('Info', 'Request waiting number: ' + (waitingJob.id - job.id), socketObj[waitingJob.id]);
getSocket(processId)
.then(socket => {
generator.createDistDir(job.data, socket, done);
jobs.then(function(waitingJobs) {
waitingJobs.forEach(function(waitingJob) {
logger.addLog('Info', 'Request waiting number: ' + (waitingJob.id - job.id), socketObj[waitingJob.id]);
});
});
})
.catch(error => {
console.error(error);
done(null);
});
});
});
console.log('processing jobs...');
});
Expand Down
28 changes: 16 additions & 12 deletions src/backend/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,24 @@ const downloadJsonFromEventyay = function(appPath, endpoint, jsonFile, cb) {
return cb(err);
}

const json = JSON.parse(body);
try {
const json = JSON.parse(body);

new JSONAPIDeserializer().deserialize(json).then(data => {
fs.writeFile(fileName, JSON.stringify(data), 'utf-8', function(error) {
if (error) {
console.log(error);
return cb(error);
}
cb();
new JSONAPIDeserializer().deserialize(json).then(data => {
fs.writeFile(fileName, JSON.stringify(data), 'utf-8', function(error) {
if (error) {
console.log(error);
return cb(error);
}
cb();
});
}).catch(error => {
console.error('Error while parsing JSONAPI response', error);
cb(error);
});
}).catch(error => {
console.error('Error while parsing JSONAPI response', error);
cb(error);
});
} catch (e) {
return cb(e);
}
});
};

Expand Down

0 comments on commit 68daf6a

Please sign in to comment.