mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-11-04 19:32:17 +08:00 
			
		
		
		
	Add a retry mechanism to mongo init
This commit is contained in:
		
							parent
							
								
									973442e3d3
								
							
						
					
					
						commit
						9e50c37f14
					
				@ -11,29 +11,42 @@
 | 
				
			|||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
# See the License for the specific language governing permissions and
 | 
					# See the License for the specific language governing permissions and
 | 
				
			||||||
# limitations under the License.
 | 
					# limitations under the License.
 | 
				
			||||||
 | 
					 | 
				
			||||||
mongosh <<EOF
 | 
					mongosh <<EOF
 | 
				
			||||||
use admin
 | 
					var maxRetries = 300;
 | 
				
			||||||
 | 
					var connected = false;
 | 
				
			||||||
var rootUsername = '$MONGO_INITDB_ROOT_USERNAME';
 | 
					var rootUsername = '$MONGO_INITDB_ROOT_USERNAME';
 | 
				
			||||||
var rootPassword = '$MONGO_INITDB_ROOT_PASSWORD';
 | 
					var rootPassword = '$MONGO_INITDB_ROOT_PASSWORD';
 | 
				
			||||||
 | 
					var dbName = '$MONGO_INITDB_DATABASE';
 | 
				
			||||||
 | 
					var openimUsername = '$MONGO_OPENIM_USERNAME';
 | 
				
			||||||
 | 
					var openimPassword = '$MONGO_OPENIM_PASSWORD';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					while (!connected && maxRetries > 0) {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        db = connect('mongodb://127.0.0.1:27017/admin');
 | 
				
			||||||
        var authResult = db.auth(rootUsername, rootPassword);
 | 
					        var authResult = db.auth(rootUsername, rootPassword);
 | 
				
			||||||
        if (authResult) {
 | 
					        if (authResult) {
 | 
				
			||||||
            print('Authentication successful for root user: ' + rootUsername);
 | 
					            print('Authentication successful for root user: ' + rootUsername);
 | 
				
			||||||
 | 
					            connected = true;
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            print('Authentication failed for root user: ' + rootUsername + ' with password: ' + rootPassword);
 | 
					            print('Authentication failed for root user: ' + rootUsername + ' with password: ' + rootPassword);
 | 
				
			||||||
            quit(1);
 | 
					            quit(1);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					        maxRetries--;
 | 
				
			||||||
 | 
					        print('Connection failed, retrying... Remaining attempts: ' + maxRetries);
 | 
				
			||||||
 | 
					        sleep(1000); // Sleep for 1 second
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var dbName = '$MONGO_INITDB_DATABASE';
 | 
					if (connected) {
 | 
				
			||||||
    db = db.getSiblingDB(dbName);
 | 
					    db = db.getSiblingDB(dbName);
 | 
				
			||||||
var openimUsername = '$MONGO_OPENIM_USERNAME';
 | 
					 | 
				
			||||||
var openimPassword = '$MONGO_OPENIM_PASSWORD';
 | 
					 | 
				
			||||||
    var createUserResult = db.createUser({
 | 
					    var createUserResult = db.createUser({
 | 
				
			||||||
        user: openimUsername,
 | 
					        user: openimUsername,
 | 
				
			||||||
        pwd: openimPassword,
 | 
					        pwd: openimPassword,
 | 
				
			||||||
  roles: [
 | 
					        roles: [{
 | 
				
			||||||
    { role: 'readWrite', db: dbName }
 | 
					            role: 'readWrite',
 | 
				
			||||||
  ]
 | 
					            db: dbName
 | 
				
			||||||
 | 
					        }]
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (createUserResult.ok == 1) {
 | 
					    if (createUserResult.ok == 1) {
 | 
				
			||||||
@ -42,7 +55,12 @@ if (createUserResult.ok == 1) {
 | 
				
			|||||||
        print('User creation failed for user: ' + openimUsername + ' in database: ' + dbName);
 | 
					        print('User creation failed for user: ' + openimUsername + ' in database: ' + dbName);
 | 
				
			||||||
        quit(1);
 | 
					        quit(1);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					} else {
 | 
				
			||||||
 | 
					    print('Failed to connect to MongoDB after 300 retries.');
 | 
				
			||||||
 | 
					    quit(1);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
EOF
 | 
					EOF
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user