Skip to content

Commit

Permalink
api:retry loop for establishing connection to min.io
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Baus committed Feb 10, 2021
1 parent fcf735d commit 4a5d1e8
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions api/src/lib/minio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const bucketName: string = "trubudget";
const makeBucket = (bucket: string, cb: Function) => {
minioClient.bucketExists(bucket, (err, exists) => {
if (err) {
return console.error("Error during searching for bucket", err);
console.error("Error during searching for bucket", err);
return cb(err);
}

if (!exists) {
Expand Down Expand Up @@ -131,6 +132,34 @@ export const getMetadataAsPromised = (fileHash: string) => {
});
};

makeBucketAsPromised(bucketName);
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};

const establishConnection = async () => {
const retries = 20;
for (let i = 0; i <= retries; i++) {
try {
await sleep(20000);

await makeBucketAsPromised(bucketName);

console.log("Connection with min.io established.");
break;
} catch (e) {
console.error("Problem with establishing connection to min.io and creating bucket.");

if (i === retries) {
console.error("Unable to connect with min.io. EXITING!");
process.exit(1);
}
}

}
};

establishConnection();

export default minioClient;

0 comments on commit 4a5d1e8

Please sign in to comment.