diff --git a/api/src/lib/minio.ts b/api/src/lib/minio.ts index e6ab780473..7cd27ea4e1 100644 --- a/api/src/lib/minio.ts +++ b/api/src/lib/minio.ts @@ -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) { @@ -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;