Reputation: 2443
In my Rails app, users can upload and download files. After upload, the files are processed by a Sidekiq worker. Everything is awesome until this point.
When the user wants to download, the Sidekiq needs to process the files again and make it ready for download. The way I have implemented is to have the user trigger the file processing, and send a notification saying "Please reload the page after sometime to download the file." The user then has to wait around and guess when the download link will be available. He then has to click on the download link again, to download the files.
The file has an attribute called "ready", after the processing is done, the "ready" attribute is set to "true". If he reloads now, the download link will be available. He can then click on it to initiate download.
My question is if there is someway to listen to this attribute change, and then initiate the download automatically?
Upvotes: 1
Views: 818
Reputation: 7313
Do something like @apneadiving suggested: (pseudocode)
var downloadCheck = setTimeout(checkIfDownloadReady, 3000); // 3 seconds
function checkIfDownloadReady() {
var downloadReady = sendReqToServer('document/3/download-status'); // $.get for example
if (downloadReady) {
clearTimeout(downloadCheck);
showDownloadButton();
} else {
displayMessage("Download is being prepared...");
}
}
Doesn't that choke the server?
Depends on how many of these "polling" requests are sent/received. You can always adjust the frequency of the polling. But I think for starters this is the easiest solution.
Upvotes: 1