waitingQueueSocket.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Socket.io handler
  3. */
  4. var fs = require('fs');
  5. var waitingQueueSocket = function(socket, testQueue) {
  6. socket.on('waiting', function(testId) {
  7. console.log('User waiting for test id ' + testId);
  8. sendTestStatus(testId);
  9. testQueue.on('testComplete', function(id) {
  10. if (testId === id) {
  11. socket.emit('complete');
  12. console.log('Sending complete event to test id ' + testId);
  13. }
  14. });
  15. testQueue.on('queueMoving', function() {
  16. var positionInQueue = testQueue.indexOf(testId);
  17. if (positionInQueue >= 0) {
  18. socket.emit('position', positionInQueue);
  19. console.log('Sending position to test id ' + testId);
  20. }
  21. });
  22. });
  23. // Finds the status of a test and send it to the client
  24. function sendTestStatus(testId) {
  25. // Check task position in queue
  26. var positionInQueue = testQueue.indexOf(testId);
  27. if (positionInQueue >= 0) {
  28. socket.emit('position', positionInQueue);
  29. } else {
  30. // Find in results files
  31. var exists = fs.exists('results/' + testId + '/results.json', function(exists) {
  32. if (exists) {
  33. // TODO : find a way to make sure the file is completely written
  34. setTimeout(function() {
  35. socket.emit('complete');
  36. }, 4000);
  37. } else {
  38. socket.emit('404');
  39. }
  40. });
  41. }
  42. }
  43. };
  44. module.exports = waitingQueueSocket;