Add retries to upload
This commit is contained in:
parent
8cfcef4b85
commit
cb8f5f59d0
1 changed files with 22 additions and 3 deletions
25
lib/sftp.js
25
lib/sftp.js
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue