@cadenza.io/service 2.17.4 → 2.17.5

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.
@@ -1844,12 +1844,14 @@ var ServiceRegistry = class _ServiceRegistry {
1844
1844
  this.useSocket ? "socket" : "rest"
1845
1845
  ) : void 0;
1846
1846
  const message = error instanceof Error ? error.message : String(error);
1847
+ const diagnostics = error && typeof error === "object" && "runtimeStatusFallback" in error && error.runtimeStatusFallback && typeof error.runtimeStatusFallback === "object" ? error.runtimeStatusFallback : void 0;
1847
1848
  CadenzaService.log(
1848
1849
  "Runtime status fallback inquiry failed.",
1849
1850
  {
1850
1851
  serviceName,
1851
1852
  serviceInstanceId,
1852
- error: message
1853
+ error: message,
1854
+ diagnostics
1853
1855
  },
1854
1856
  "warning"
1855
1857
  );
@@ -2339,6 +2341,66 @@ var ServiceRegistry = class _ServiceRegistry {
2339
2341
  }
2340
2342
  return this.getInstance(this.serviceName, this.serviceInstanceId);
2341
2343
  }
2344
+ summarizeTransportForDebug(transport) {
2345
+ if (!transport) {
2346
+ return void 0;
2347
+ }
2348
+ return {
2349
+ uuid: transport.uuid,
2350
+ role: transport.role,
2351
+ origin: transport.origin,
2352
+ protocols: transport.protocols,
2353
+ clientCreated: transport.clientCreated
2354
+ };
2355
+ }
2356
+ summarizeInstanceForRuntimeStatusFallback(instance) {
2357
+ return {
2358
+ exists: Boolean(instance),
2359
+ runtimeState: instance?.runtimeState,
2360
+ acceptingWork: instance?.acceptingWork,
2361
+ reportedAt: instance?.reportedAt ?? null,
2362
+ isDatabase: instance?.isDatabase,
2363
+ isFrontend: instance?.isFrontend,
2364
+ transports: (instance?.transports ?? []).map((transport) => ({
2365
+ uuid: transport.uuid,
2366
+ role: transport.role,
2367
+ origin: transport.origin,
2368
+ protocols: transport.protocols,
2369
+ clientCreated: transport.clientCreated
2370
+ }))
2371
+ };
2372
+ }
2373
+ summarizeRuntimeStatusInquiryReports(inquiryResult) {
2374
+ const reports = Array.isArray(inquiryResult.runtimeStatusReports) ? inquiryResult.runtimeStatusReports : [];
2375
+ return reports.map((candidate) => {
2376
+ const normalized = this.normalizeRuntimeStatusReport(candidate);
2377
+ if (normalized) {
2378
+ return {
2379
+ serviceName: normalized.serviceName,
2380
+ serviceInstanceId: normalized.serviceInstanceId,
2381
+ transportId: normalized.transportId,
2382
+ state: normalized.state,
2383
+ acceptingWork: normalized.acceptingWork,
2384
+ reportedAt: normalized.reportedAt
2385
+ };
2386
+ }
2387
+ const raw = candidate && typeof candidate === "object" ? candidate : {};
2388
+ const rawState = raw.state === "healthy" || raw.state === "degraded" || raw.state === "overloaded" || raw.state === "unavailable" ? raw.state : void 0;
2389
+ return {
2390
+ serviceName: typeof raw.serviceName === "string" ? raw.serviceName : typeof raw.__serviceName === "string" ? raw.__serviceName : void 0,
2391
+ serviceInstanceId: typeof raw.serviceInstanceId === "string" ? raw.serviceInstanceId : typeof raw.__serviceInstanceId === "string" ? raw.__serviceInstanceId : void 0,
2392
+ transportId: typeof raw.transportId === "string" ? raw.transportId : typeof raw.serviceTransportId === "string" ? raw.serviceTransportId : void 0,
2393
+ state: rawState,
2394
+ acceptingWork: typeof raw.acceptingWork === "boolean" ? raw.acceptingWork : void 0,
2395
+ reportedAt: typeof raw.reportedAt === "string" ? raw.reportedAt : void 0
2396
+ };
2397
+ });
2398
+ }
2399
+ createRuntimeStatusFallbackError(message, diagnostics) {
2400
+ return Object.assign(new Error(message), {
2401
+ runtimeStatusFallback: diagnostics
2402
+ });
2403
+ }
2342
2404
  resolveLocalStatusCheck(ctx = {}) {
2343
2405
  if (!this.serviceName) {
2344
2406
  return {
@@ -2658,32 +2720,48 @@ var ServiceRegistry = class _ServiceRegistry {
2658
2720
  }
2659
2721
  async resolveRuntimeStatusFallbackInquiry(serviceName, serviceInstanceId, options = {}) {
2660
2722
  const instance = this.getInstance(serviceName, serviceInstanceId);
2661
- if (instance) {
2662
- const directReport = await this.requestRuntimeStatusViaRest(
2663
- instance,
2664
- serviceName,
2665
- serviceInstanceId
2666
- );
2667
- if (directReport) {
2668
- if (!this.applyRuntimeStatusReport(directReport)) {
2669
- throw new Error(
2670
- `No tracked instance for runtime fallback ${serviceName}/${serviceInstanceId}`
2671
- );
2672
- }
2673
- this.lastHeartbeatAtByInstance.set(serviceInstanceId, Date.now());
2674
- this.missedHeartbeatsByInstance.set(serviceInstanceId, 0);
2675
- return {
2676
- report: directReport,
2677
- inquiryMeta: {
2678
- inquiry: META_RUNTIME_STATUS_INTENT,
2679
- responded: 1,
2680
- failed: 0,
2681
- timedOut: 0,
2682
- pending: 0,
2683
- directStatusCheck: true
2723
+ const directStatusCheck = instance ? await this.requestRuntimeStatusViaRest(
2724
+ instance,
2725
+ serviceName,
2726
+ serviceInstanceId
2727
+ ) : {
2728
+ report: null,
2729
+ diagnostic: {
2730
+ attempted: false,
2731
+ outcome: "instance_missing"
2732
+ }
2733
+ };
2734
+ if (directStatusCheck.report) {
2735
+ if (!this.applyRuntimeStatusReport(directStatusCheck.report)) {
2736
+ throw this.createRuntimeStatusFallbackError(
2737
+ `No tracked instance for runtime fallback ${serviceName}/${serviceInstanceId}`,
2738
+ {
2739
+ target: {
2740
+ serviceName,
2741
+ serviceInstanceId
2742
+ },
2743
+ instance: this.summarizeInstanceForRuntimeStatusFallback(instance),
2744
+ directStatusCheck: directStatusCheck.diagnostic,
2745
+ inquiry: {
2746
+ meta: {},
2747
+ reportTargets: []
2748
+ }
2684
2749
  }
2685
- };
2750
+ );
2686
2751
  }
2752
+ this.lastHeartbeatAtByInstance.set(serviceInstanceId, Date.now());
2753
+ this.missedHeartbeatsByInstance.set(serviceInstanceId, 0);
2754
+ return {
2755
+ report: directStatusCheck.report,
2756
+ inquiryMeta: {
2757
+ inquiry: META_RUNTIME_STATUS_INTENT,
2758
+ responded: 1,
2759
+ failed: 0,
2760
+ timedOut: 0,
2761
+ pending: 0,
2762
+ directStatusCheck: true
2763
+ }
2764
+ };
2687
2765
  }
2688
2766
  const inquiryResult = await CadenzaService.inquire(
2689
2767
  META_RUNTIME_STATUS_INTENT,
@@ -2705,13 +2783,37 @@ var ServiceRegistry = class _ServiceRegistry {
2705
2783
  serviceInstanceId
2706
2784
  );
2707
2785
  if (!report) {
2708
- throw new Error(
2709
- `No runtime status report for ${serviceName}/${serviceInstanceId}`
2786
+ throw this.createRuntimeStatusFallbackError(
2787
+ `No runtime status report for ${serviceName}/${serviceInstanceId}`,
2788
+ {
2789
+ target: {
2790
+ serviceName,
2791
+ serviceInstanceId
2792
+ },
2793
+ instance: this.summarizeInstanceForRuntimeStatusFallback(instance),
2794
+ directStatusCheck: directStatusCheck.diagnostic,
2795
+ inquiry: {
2796
+ meta: inquiryResult.__inquiryMeta && typeof inquiryResult.__inquiryMeta === "object" ? inquiryResult.__inquiryMeta : {},
2797
+ reportTargets: this.summarizeRuntimeStatusInquiryReports(inquiryResult)
2798
+ }
2799
+ }
2710
2800
  );
2711
2801
  }
2712
2802
  if (!this.applyRuntimeStatusReport(report)) {
2713
- throw new Error(
2714
- `No tracked instance for runtime fallback ${serviceName}/${serviceInstanceId}`
2803
+ throw this.createRuntimeStatusFallbackError(
2804
+ `No tracked instance for runtime fallback ${serviceName}/${serviceInstanceId}`,
2805
+ {
2806
+ target: {
2807
+ serviceName,
2808
+ serviceInstanceId
2809
+ },
2810
+ instance: this.summarizeInstanceForRuntimeStatusFallback(instance),
2811
+ directStatusCheck: directStatusCheck.diagnostic,
2812
+ inquiry: {
2813
+ meta: inquiryResult.__inquiryMeta && typeof inquiryResult.__inquiryMeta === "object" ? inquiryResult.__inquiryMeta : {},
2814
+ reportTargets: this.summarizeRuntimeStatusInquiryReports(inquiryResult)
2815
+ }
2816
+ }
2715
2817
  );
2716
2818
  }
2717
2819
  this.lastHeartbeatAtByInstance.set(serviceInstanceId, Date.now());
@@ -2723,11 +2825,23 @@ var ServiceRegistry = class _ServiceRegistry {
2723
2825
  }
2724
2826
  async requestRuntimeStatusViaRest(instance, serviceName, serviceInstanceId) {
2725
2827
  if (typeof globalThis.fetch !== "function") {
2726
- return null;
2828
+ return {
2829
+ report: null,
2830
+ diagnostic: {
2831
+ attempted: false,
2832
+ outcome: "fetch_unavailable"
2833
+ }
2834
+ };
2727
2835
  }
2728
2836
  const transport = this.getRouteableTransport(instance, "rest");
2729
2837
  if (!transport) {
2730
- return null;
2838
+ return {
2839
+ report: null,
2840
+ diagnostic: {
2841
+ attempted: false,
2842
+ outcome: "no_rest_transport"
2843
+ }
2844
+ };
2731
2845
  }
2732
2846
  const controller = typeof AbortController === "function" ? new AbortController() : null;
2733
2847
  const timeoutId = controller ? setTimeout(() => controller.abort(), this.runtimeStatusFallbackTimeoutMs) : null;
@@ -2737,7 +2851,16 @@ var ServiceRegistry = class _ServiceRegistry {
2737
2851
  signal: controller?.signal
2738
2852
  });
2739
2853
  if ("ok" in response && response.ok === false) {
2740
- return null;
2854
+ return {
2855
+ report: null,
2856
+ diagnostic: {
2857
+ attempted: true,
2858
+ outcome: "http_error",
2859
+ transport: this.summarizeTransportForDebug(transport),
2860
+ responseStatus: typeof response.status === "number" ? response.status : void 0,
2861
+ responseStatusText: typeof response.statusText === "string" ? response.statusText : void 0
2862
+ }
2863
+ };
2741
2864
  }
2742
2865
  const payload = typeof response.json === "function" ? await response.json() : response;
2743
2866
  const report = this.normalizeRuntimeStatusReport({
@@ -2747,14 +2870,46 @@ var ServiceRegistry = class _ServiceRegistry {
2747
2870
  transportProtocols: payload?.transportProtocols ?? transport.protocols
2748
2871
  });
2749
2872
  if (!report) {
2750
- return null;
2873
+ return {
2874
+ report: null,
2875
+ diagnostic: {
2876
+ attempted: true,
2877
+ outcome: "invalid_report",
2878
+ transport: this.summarizeTransportForDebug(transport),
2879
+ payloadKeys: payload && typeof payload === "object" ? Object.keys(payload).sort() : []
2880
+ }
2881
+ };
2751
2882
  }
2752
2883
  if (report.serviceName !== serviceName || report.serviceInstanceId !== serviceInstanceId) {
2753
- return null;
2884
+ return {
2885
+ report: null,
2886
+ diagnostic: {
2887
+ attempted: true,
2888
+ outcome: "identity_mismatch",
2889
+ transport: this.summarizeTransportForDebug(transport),
2890
+ payloadServiceName: report.serviceName,
2891
+ payloadServiceInstanceId: report.serviceInstanceId
2892
+ }
2893
+ };
2754
2894
  }
2755
- return report;
2756
- } catch {
2757
- return null;
2895
+ return {
2896
+ report,
2897
+ diagnostic: {
2898
+ attempted: true,
2899
+ outcome: "matched",
2900
+ transport: this.summarizeTransportForDebug(transport)
2901
+ }
2902
+ };
2903
+ } catch (error) {
2904
+ return {
2905
+ report: null,
2906
+ diagnostic: {
2907
+ attempted: true,
2908
+ outcome: "fetch_error",
2909
+ transport: this.summarizeTransportForDebug(transport),
2910
+ error: error instanceof Error ? error.message : String(error)
2911
+ }
2912
+ };
2758
2913
  } finally {
2759
2914
  if (timeoutId) {
2760
2915
  clearTimeout(timeoutId);