Add retries to upload

This commit is contained in:
Jonathan Cremin 2016-06-06 20:57:34 +01:00
parent 8cfcef4b85
commit cb8f5f59d0

View file

@ -15,7 +15,7 @@ export function get(remotePath) {
.then(() => sftp.get(join('hostr', 'uploads', remotePath), { encoding: null }));
}
export function upload(localPath, remotePath) {
function sendFile(localPath, remotePath) {
const sftp = new Client();
return sftp.connect({
host: process.env.SFTP_HOST,
@ -24,6 +24,25 @@ export function upload(localPath, remotePath) {
password: process.env.SFTP_PASSWORD,
})
.then(() => sftp.put(localPath, remotePath, true))
.catch(() => sftp.mkdir(dirname(remotePath), true)
.then(() => sftp.put(localPath, remotePath, true)));
.catch((err) => {
if (err.message === 'No such file') {
debug('Creating directory');
return sftp.mkdir(dirname(remotePath), true)
.then(() => sftp.put(localPath, remotePath, true));
}
throw err;
});
}
export function *upload(localPath, remotePath) {
let done = false;
for (let retries = 0; retries < 5; retries++) {
try {
done = yield sendFile(localPath, remotePath);
break;
} catch (err) {
debug('retry');
}
}
return done;
}