replaces wait-on #938

Merged
jessopb merged 1 commit from waitOnBroke into master 2019-02-26 09:28:56 +01:00
3 changed files with 36 additions and 7 deletions

0
changelog.md Normal file
View file

View file

@ -1,11 +1,13 @@
const { getClaim } = require('../../../../lbrynet'); const { getClaim } = require('../../../../lbrynet');
const { createFileRecordDataAfterGet } = require('../../../../models/utils/createFileRecordData.js'); const {
createFileRecordDataAfterGet,
} = require('../../../../models/utils/createFileRecordData.js');
const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const getClaimData = require('server/utils/getClaimData'); const getClaimData = require('server/utils/getClaimData');
const chainquery = require('chainquery').default; const chainquery = require('chainquery').default;
const db = require('../../../../models'); const db = require('../../../../models');
const waitOn = require('wait-on');
const logger = require('winston'); const logger = require('winston');
const awaitFileSize = require('server/utils/awaitFileSize');
/* /*
@ -36,11 +38,11 @@ const claimGet = async ({ ip, originalUrl, params }, res) => {
if (!claimData) { if (!claimData) {
throw new Error('claim/get: getClaimData failed to get file blobs'); throw new Error('claim/get: getClaimData failed to get file blobs');
} }
await waitOn({ let fileReady = await awaitFileSize(lbrynetResult.download_path, 2000000, 10000, 250);
resources: [ lbrynetResult.download_path ],
timeout : 10000, // 10 seconds if (fileReady !== 'ready') {
window : 500, throw new Error('claim/get: failed to get file after 10 seconds');
}); }
const fileData = await createFileRecordDataAfterGet(claimData, lbrynetResult); const fileData = await createFileRecordDataAfterGet(claimData, lbrynetResult);
if (!fileData) { if (!fileData) {
throw new Error('claim/get: createFileRecordDataAfterGet failed to create file in time'); throw new Error('claim/get: createFileRecordDataAfterGet failed to create file in time');

View file

@ -0,0 +1,27 @@
const fs = require('fs');
const { promisify } = require('util');
const fsstat = promisify(fs.stat);
const awaitFileSize = (path, sizeInBytes, timeout, interval) => {
return new Promise((resolve, reject) => {
let totalTime = 0;
let timer = setInterval(() => {
totalTime = totalTime + interval;
fsstat(path)
.then(stats => {
if (stats.size > sizeInBytes) {
clearInterval(interval);
resolve('ready');
}
if (totalTime > timeout) {
const error = new Error('File did not arrive in time');
error.name = 'FILE_NOT_ARRIVED';
reject(error);
}
})
.catch();
}, interval);
});
};
module.exports = awaitFileSize;