Advertisement
4tolexx

job status poller

May 7th, 2025
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // function initJobStatusPoller(options) {
  2. //   $(document).ready(function() {
  3. //     var jobId = options.jobId;
  4. //     var statusContainer = $('#job_status_container_' + jobId);
  5. //     var currentStatus = options.currentStatus;
  6.  
  7. //     if (!statusContainer.length ||
  8. //         !['working', 'queued', 'retrying'].includes(currentStatus) ||
  9. //         statusContainer.data('polling') === true) {
  10. //       return;
  11. //     }
  12.  
  13. //     statusContainer.data('polling', true);
  14.  
  15. //     console.log('Job status poller initialized for job', jobId, 'with status', currentStatus);
  16.  
  17. //     var intervalId = setInterval(function() {
  18. //       if (!$.contains(document.documentElement, statusContainer[0])) {
  19. //         clearInterval(intervalId);
  20. //         return;
  21. //       }
  22.  
  23. //       $.ajax({
  24. //         url: '/admin/background_jobs/' + jobId + '/job_status',
  25. //         type: 'GET',
  26. //         success: function(data) {
  27. //           statusContainer.html(data);
  28.  
  29. //           var newStatus;
  30. //           var tempDiv = $('<div>').html(data);
  31. //           var statusTag = tempDiv.find('.job-status .status_tag');
  32.  
  33. //           if (statusTag.length) {
  34. //             var statusText = statusTag.text().trim().toLowerCase();
  35.  
  36. //             if (statusText === 'successful') newStatus = 'complete';
  37. //             else if (statusText === 'failed') newStatus = 'failed';
  38. //             else if (statusText === 'cancelled') newStatus = 'stopped';
  39. //             else if (statusText === 'running') newStatus = 'working';
  40. //             else if (statusText === 'retrying') newStatus = 'retrying';
  41. //             else newStatus = statusText;
  42.  
  43. //             console.log('Detected status:', newStatus);
  44. //           }
  45.  
  46. //           if (['complete', 'failed', 'stopped', 'successful'].includes(newStatus)) {
  47. //             clearInterval(intervalId);
  48. //             console.log('Job ' + jobId + ' is now ' + newStatus + ', stopping polling and refreshing page');
  49.  
  50. //             setTimeout(function() {
  51. //               window.location.href = window.location.href;
  52. //             }, 1000);
  53. //           }
  54. //         },
  55. //         error: function(xhr, status, error) {
  56. //           console.error('Error fetching job status for job ' + jobId + ':', error);
  57. //           clearInterval(intervalId);
  58. //         }
  59. //       });
  60. //     }, 5000);
  61.  
  62. //     statusContainer.data('intervalId', intervalId);
  63. //   });
  64. // }
  65.  
  66. // window.initJobStatusPoller = initJobStatusPoller;
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement