ensureRunning static method

Future<void> ensureRunning()

Ensure MySQL is running, attempt to start it if not.

Implementation

static Future<void> ensureRunning() async {
  if (await Shell.isPortListening(_port)) {
    Logger.info(_tag, 'MySQL is ready (port $_port listening).');
    return;
  }

  Logger.info(_tag, 'MySQL not ready, attempting to start...');

  if (PlatformInfo.isWindows) {
    await _startWindows();
  } else if (PlatformInfo.isMacOS) {
    await Shell.run('brew', ['services', 'start', 'mysql']);
  } else {
    await Shell.run('sudo', ['systemctl', 'start', 'mysql']);
  }

  // Wait for MySQL to become available.
  for (int i = 0; i < 10; i++) {
    if (await Shell.isPortListening(_port)) {
      Logger.info(_tag, 'MySQL is ready.');
      return;
    }
    await Future<void>.delayed(const Duration(seconds: 1));
  }
  Logger.warn('MySQL may not have started. Please check manually.');
}