@dbcube/core 1.0.51 → 1.0.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -599,11 +599,14 @@ var Engine = class {
599
599
  arguments;
600
600
  binary = null;
601
601
  timeout;
602
+ isDaemonConnected = false;
603
+ isConnecting = false;
602
604
  constructor(name, timeout = 3e4) {
603
605
  this.name = name;
604
606
  this.config = this.setConfig(name);
605
607
  this.arguments = this.setArguments();
606
608
  this.timeout = timeout;
609
+ this.setupProcessExitHandlers();
607
610
  }
608
611
  async initializeBinary() {
609
612
  if (!this.binary) {
@@ -675,6 +678,12 @@ var Engine = class {
675
678
  if (!this.binary) {
676
679
  throw new Error("Binary not initialized");
677
680
  }
681
+ const isConnectAction = args.includes("connect");
682
+ const isDisconnectAction = args.includes("disconnect");
683
+ const isExecuteAction = args.includes("execute");
684
+ if (isExecuteAction) {
685
+ await this.ensureDaemonConnected();
686
+ }
678
687
  return new Promise((resolve5, reject) => {
679
688
  const child = (0, import_child_process.spawn)(this.binary[binary], [...this.arguments, ...args]);
680
689
  let stdoutBuffer = "";
@@ -692,10 +701,17 @@ var Engine = class {
692
701
  isResolved = true;
693
702
  clearTimeout(timeoutId);
694
703
  resolve5(response);
704
+ if (isConnectAction && response.status === 200) {
705
+ this.isDaemonConnected = true;
706
+ this.isConnecting = false;
707
+ } else if (isDisconnectAction) {
708
+ this.isDaemonConnected = false;
709
+ }
695
710
  }
696
711
  };
697
712
  child.stdout.on("data", (data) => {
698
713
  stdoutBuffer += data.toString();
714
+ console.log(stdoutBuffer);
699
715
  const match = stdoutBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
700
716
  if (match) {
701
717
  try {
@@ -716,6 +732,7 @@ var Engine = class {
716
732
  });
717
733
  child.stderr.on("data", (data) => {
718
734
  stderrBuffer += data.toString();
735
+ console.log(stderrBuffer);
719
736
  const match = stderrBuffer.match(/PROCESS_RESPONSE:(\{.*\})/);
720
737
  if (match) {
721
738
  try {
@@ -757,6 +774,67 @@ var Engine = class {
757
774
  child.unref();
758
775
  });
759
776
  }
777
+ async ensureDaemonConnected() {
778
+ if (this.isDaemonConnected) {
779
+ return;
780
+ }
781
+ if (this.isConnecting) {
782
+ while (this.isConnecting) {
783
+ await new Promise((resolve5) => setTimeout(resolve5, 50));
784
+ }
785
+ return;
786
+ }
787
+ this.isConnecting = true;
788
+ try {
789
+ const response = await this.run("query_engine", ["--action", "connect"]);
790
+ if (response.status !== 200) {
791
+ throw new Error(`Failed to connect daemon: ${response.message}`);
792
+ }
793
+ } catch (error) {
794
+ this.isConnecting = false;
795
+ throw error;
796
+ }
797
+ }
798
+ async disconnect() {
799
+ if (this.isDaemonConnected) {
800
+ await this.run("query_engine", ["--action", "disconnect"]);
801
+ }
802
+ }
803
+ setupProcessExitHandlers() {
804
+ const gracefulShutdown = async () => {
805
+ if (this.isDaemonConnected) {
806
+ console.log("\u{1F504} Disconnecting daemon before exit...");
807
+ try {
808
+ await this.disconnect();
809
+ console.log("\u2705 Daemon disconnected successfully");
810
+ } catch (error) {
811
+ console.error("\u274C Error disconnecting daemon:", error);
812
+ }
813
+ }
814
+ };
815
+ process.on("exit", () => {
816
+ });
817
+ process.on("SIGINT", async () => {
818
+ console.log("\n\u{1F6D1} Received SIGINT (Ctrl+C)");
819
+ await gracefulShutdown();
820
+ process.exit(0);
821
+ });
822
+ process.on("SIGTERM", async () => {
823
+ console.log("\u{1F6D1} Received SIGTERM");
824
+ await gracefulShutdown();
825
+ process.exit(0);
826
+ });
827
+ process.on("uncaughtException", async (error) => {
828
+ console.error("\u{1F4A5} Uncaught Exception:", error);
829
+ await gracefulShutdown();
830
+ process.exit(1);
831
+ });
832
+ process.on("unhandledRejection", async (reason, promise) => {
833
+ console.error("\u{1F4A5} Unhandled Rejection at:", promise, "reason:", reason);
834
+ await gracefulShutdown();
835
+ process.exit(1);
836
+ });
837
+ }
760
838
  };
761
839
 
762
840
  // src/lib/SqliteExecutor.ts