run method

Future<Process> run({
  1. required String executable,
  2. required List<String> arguments,
  3. required String host,
  4. required int tg_bot_api_port,
  5. String? workingDirectory,
  6. Map<String, String>? environment,
  7. bool includeParentEnvironment = true,
  8. bool runInShell = true,
  9. ProcessStartMode mode = ProcessStartMode.normal,
  10. bool is_print = false,
})

run executable telegram bot api

Implementation

Future<Process> run({
  required String executable,
  required List<String> arguments,
  required String host,
  required int tg_bot_api_port,
  String? workingDirectory,
  Map<String, String>? environment,
  bool includeParentEnvironment = true,
  bool runInShell = true,
  ProcessStartMode mode = ProcessStartMode.normal,
  bool is_print = false,
}) async {
  Process shell_tg_bot = await Process.start(
    executable,
    arguments,
    environment: environment,
    includeParentEnvironment: includeParentEnvironment,
    runInShell: runInShell,
    workingDirectory: workingDirectory,
    mode: mode,
  );

  if (is_print) {
    var stderr_wa = shell_tg_bot.stderr.listen((event) {
      try {
        stderr.add(event);
      } catch (e) {}
    });
    // shell_tg_bot
    var stdout_wa = shell_tg_bot.stdout.listen((event) {
      try {
        stdout.add(event);
      } catch (e) {}
    });
    Timer.periodic(Duration(seconds: 2), (timer) async {
      try {
        await get(Uri.parse("http://$host:$tg_bot_api_port"));
      } catch (e) {
        if (e is ClientException) {
          if (e.message == "Connection refused") {
            timer.cancel();
            await stderr_wa.cancel();
            await stdout_wa.cancel();
            shell_tg_bot.kill(ProcessSignal.sigkill);
            await run(
              executable: executable,
              arguments: arguments,
              host: host,
              tg_bot_api_port: tg_bot_api_port,
              workingDirectory: workingDirectory,
              environment: environment,
              includeParentEnvironment: includeParentEnvironment,
              runInShell: runInShell,
              mode: mode,
              is_print: is_print,
            );
          }
        }
      }
    });
  } else {
    Timer.periodic(Duration(seconds: 2), (timer) async {
      try {
        await get(Uri.parse("http://$host:$tg_bot_api_port"));
      } catch (e) {
        if (e is ClientException) {
          if (e.message == "Connection refused") {
            timer.cancel();
            // await stderr_wa.cancel();
            // await stdout_wa.cancel();
            shell_tg_bot.kill(ProcessSignal.sigkill);
            await run(
              executable: executable,
              arguments: arguments,
              host: host,
              tg_bot_api_port: tg_bot_api_port,
              workingDirectory: workingDirectory,
              environment: environment,
              includeParentEnvironment: includeParentEnvironment,
              runInShell: runInShell,
              mode: mode,
              is_print: is_print,
            );
          }
        }
      }
    });
  }

  return shell_tg_bot;
}