@cadenza.io/service 2.0.1 → 2.0.2

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.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _cadenza_io_core from '@cadenza.io/core';
2
- import { Task, ThrottleTagGetter, SchemaDefinition as SchemaDefinition$1, GraphContext, AnyObject, TaskResult, SignalBroker, GraphRunner, GraphRegistry, CadenzaMode, GraphRoutine, TaskOptions, TaskFunction, DebounceOptions, DebounceTask, EphemeralTaskOptions, EphemeralTask } from '@cadenza.io/core';
2
+ import { Task, ThrottleTagGetter, SchemaDefinition as SchemaDefinition$1, GraphContext, AnyObject, TaskResult, GraphRoutine, SignalBroker, GraphRunner, GraphRegistry, CadenzaMode, TaskOptions, TaskFunction, DebounceOptions, DebounceTask, EphemeralTaskOptions, EphemeralTask } from '@cadenza.io/core';
3
3
  export { AnyObject, DebounceOptions, DebounceTask, EphemeralTask, EphemeralTaskOptions, GraphRoutine, Task, TaskFunction, TaskOptions, ThrottleTagGetter } from '@cadenza.io/core';
4
4
 
5
5
  /**
@@ -190,7 +190,7 @@ declare class ServiceRegistry {
190
190
  handleGlobalSignalRegistrationTask: Task;
191
191
  getRemoteSignalsTask: Task;
192
192
  handleSocketStatusUpdateTask: Task;
193
- fullSyncTask: Task;
193
+ fullSyncTask: GraphRoutine;
194
194
  getAllInstances: Task;
195
195
  doForEachInstance: Task;
196
196
  deleteInstance: Task;
@@ -22087,7 +22087,7 @@ declare class CadenzaService {
22087
22087
  * @param {TaskOptions} [options={}] - A set of optional parameters to further configure the task.
22088
22088
  * @return {SignalTransmissionTask} A new instance of SignalTransmissionTask configured with the given parameters.
22089
22089
  */
22090
- static createSignalTransmissionTask(signalName: string, serviceName: string, options?: TaskOptions): SignalTransmissionTask;
22090
+ static createSignalTransmissionTask(signalName: string, serviceName: string, options?: TaskOptions): SignalTransmissionTask | undefined;
22091
22091
  /**
22092
22092
  * Creates and configures a database task that performs an operation on a specified table.
22093
22093
  *
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _cadenza_io_core from '@cadenza.io/core';
2
- import { Task, ThrottleTagGetter, SchemaDefinition as SchemaDefinition$1, GraphContext, AnyObject, TaskResult, SignalBroker, GraphRunner, GraphRegistry, CadenzaMode, GraphRoutine, TaskOptions, TaskFunction, DebounceOptions, DebounceTask, EphemeralTaskOptions, EphemeralTask } from '@cadenza.io/core';
2
+ import { Task, ThrottleTagGetter, SchemaDefinition as SchemaDefinition$1, GraphContext, AnyObject, TaskResult, GraphRoutine, SignalBroker, GraphRunner, GraphRegistry, CadenzaMode, TaskOptions, TaskFunction, DebounceOptions, DebounceTask, EphemeralTaskOptions, EphemeralTask } from '@cadenza.io/core';
3
3
  export { AnyObject, DebounceOptions, DebounceTask, EphemeralTask, EphemeralTaskOptions, GraphRoutine, Task, TaskFunction, TaskOptions, ThrottleTagGetter } from '@cadenza.io/core';
4
4
 
5
5
  /**
@@ -190,7 +190,7 @@ declare class ServiceRegistry {
190
190
  handleGlobalSignalRegistrationTask: Task;
191
191
  getRemoteSignalsTask: Task;
192
192
  handleSocketStatusUpdateTask: Task;
193
- fullSyncTask: Task;
193
+ fullSyncTask: GraphRoutine;
194
194
  getAllInstances: Task;
195
195
  doForEachInstance: Task;
196
196
  deleteInstance: Task;
@@ -22087,7 +22087,7 @@ declare class CadenzaService {
22087
22087
  * @param {TaskOptions} [options={}] - A set of optional parameters to further configure the task.
22088
22088
  * @return {SignalTransmissionTask} A new instance of SignalTransmissionTask configured with the given parameters.
22089
22089
  */
22090
- static createSignalTransmissionTask(signalName: string, serviceName: string, options?: TaskOptions): SignalTransmissionTask;
22090
+ static createSignalTransmissionTask(signalName: string, serviceName: string, options?: TaskOptions): SignalTransmissionTask | undefined;
22091
22091
  /**
22092
22092
  * Creates and configures a database task that performs an operation on a specified table.
22093
22093
  *
package/dist/index.js CHANGED
@@ -382,35 +382,38 @@ var ServiceRegistry = class _ServiceRegistry {
382
382
  this.handleGlobalSignalRegistrationTask = CadenzaService.createMetaTask(
383
383
  "Handle global Signal Registration",
384
384
  (ctx) => {
385
- const { signalToTaskMap } = ctx;
386
- const sortedSignalToTaskMap = signalToTaskMap.sort((a, b) => {
387
- if (a.deleted && !b.deleted) return -1;
388
- if (!a.deleted && b.deleted) return 1;
389
- return 0;
390
- });
385
+ const { signalToTaskMaps } = ctx;
386
+ const sortedSignalToTaskMap = signalToTaskMaps.sort(
387
+ (a, b) => {
388
+ if (a.deleted && !b.deleted) return -1;
389
+ if (!a.deleted && b.deleted) return 1;
390
+ return 0;
391
+ }
392
+ );
393
+ console.log("signalToTaskMap", sortedSignalToTaskMap);
391
394
  const locallyEmittedSignals = CadenzaService.broker.listEmittedSignals().filter((s) => s.startsWith("global."));
392
395
  for (const map of sortedSignalToTaskMap) {
393
396
  if (map.deleted) {
394
- this.remoteSignals.get(map.taskServiceName)?.delete(map.signal);
395
- if (!this.remoteSignals.get(map.taskServiceName)?.size) {
396
- this.remoteSignals.delete(map.taskServiceName);
397
+ this.remoteSignals.get(map.serviceName)?.delete(map.signalName);
398
+ if (!this.remoteSignals.get(map.serviceName)?.size) {
399
+ this.remoteSignals.delete(map.serviceName);
397
400
  }
398
401
  CadenzaService.get(
399
- `Transmit signal: ${map.signal} to ${map.taskServiceName}`
402
+ `Transmit signal: ${map.signalName} to ${map.serviceName}`
400
403
  )?.destroy();
401
404
  continue;
402
405
  }
403
- if (locallyEmittedSignals.includes(map.signal)) {
404
- if (!this.remoteSignals.get(map.taskServiceName)) {
405
- this.remoteSignals.set(map.taskServiceName, /* @__PURE__ */ new Set());
406
+ if (locallyEmittedSignals.includes(map.signalName)) {
407
+ if (!this.remoteSignals.get(map.serviceName)) {
408
+ this.remoteSignals.set(map.serviceName, /* @__PURE__ */ new Set());
406
409
  }
407
- if (!this.remoteSignals.get(map.taskServiceName)?.has(map.signal)) {
410
+ if (!this.remoteSignals.get(map.serviceName)?.has(map.signalName)) {
408
411
  CadenzaService.createSignalTransmissionTask(
409
- map.signal,
410
- map.taskServiceName
412
+ map.signalName,
413
+ map.serviceName
411
414
  );
412
415
  }
413
- this.remoteSignals.get(map.taskServiceName)?.add(map.signal);
416
+ this.remoteSignals.get(map.serviceName)?.add(map.signalName);
414
417
  }
415
418
  }
416
419
  return true;
@@ -507,45 +510,43 @@ var ServiceRegistry = class _ServiceRegistry {
507
510
  },
508
511
  "Handles status update from socket broadcast"
509
512
  ).doOn("meta.socket_client.status_received");
510
- this.fullSyncTask = CadenzaService.createCadenzaDBQueryTask("service_instance", {
511
- filter: {
512
- deleted: false,
513
- is_active: true,
514
- is_non_responsive: false,
515
- is_blocked: false
516
- },
517
- fields: [
518
- "uuid",
519
- "address",
520
- "port",
521
- "service_name",
522
- "is_active",
523
- "is_non_responsive",
524
- "is_blocked",
525
- "health",
526
- "exposed",
527
- "created",
528
- "is_frontend"
529
- ]
530
- }).doOn("meta.sync_requested").emits("meta.service_registry.synced_instances").then(
531
- CadenzaService.createMetaTask(
532
- "Split service instances",
533
- function* (ctx) {
534
- const { serviceInstances } = ctx;
535
- if (!serviceInstances) {
536
- CadenzaService.log(
537
- "SyncFailed: No service instances found",
538
- ctx,
539
- "error"
540
- );
541
- return;
542
- }
543
- for (const serviceInstance of serviceInstances) {
544
- yield { serviceInstance };
545
- }
546
- }
547
- ).then(this.handleInstanceUpdateTask).doOn("meta.signal_controller.signal_map_added")
548
- );
513
+ const mergeSyncDataTask = CadenzaService.createUniqueMetaTask(
514
+ "Merge sync data",
515
+ (ctx) => {
516
+ let joinedContext = {};
517
+ ctx.joinedContexts.forEach((ctx2) => {
518
+ joinedContext = { ...joinedContext, ...ctx2 };
519
+ });
520
+ console.log("Full sync joinedContext", joinedContext);
521
+ return joinedContext;
522
+ }
523
+ ).then(this.handleGlobalSignalRegistrationTask);
524
+ this.fullSyncTask = CadenzaService.createMetaRoutine("Full sync", [
525
+ CadenzaService.createCadenzaDBQueryTask("signal_to_task_map", {
526
+ fields: ["signal_name", "service_name", "deleted"]
527
+ }).then(mergeSyncDataTask),
528
+ CadenzaService.createCadenzaDBQueryTask("service_instance", {
529
+ filter: {
530
+ deleted: false,
531
+ is_active: true,
532
+ is_non_responsive: false,
533
+ is_blocked: false
534
+ },
535
+ fields: [
536
+ "uuid",
537
+ "address",
538
+ "port",
539
+ "service_name",
540
+ "is_active",
541
+ "is_non_responsive",
542
+ "is_blocked",
543
+ "health",
544
+ "exposed",
545
+ "created",
546
+ "is_frontend"
547
+ ]
548
+ }).then(mergeSyncDataTask)
549
+ ]).doOn("meta.sync_requested");
549
550
  this.getInstanceById = CadenzaService.createMetaTask(
550
551
  "Get instance by id",
551
552
  (context) => {
@@ -850,7 +851,7 @@ var ServiceRegistry = class _ServiceRegistry {
850
851
  if (isBrowser) {
851
852
  CadenzaService.createMetaTask("Prepare for signal sync", () => {
852
853
  return {};
853
- }).doAfter(this.fullSyncTask).then(
854
+ }).then(
854
855
  CadenzaService.createCadenzaDBQueryTask("signal_registry", {
855
856
  fields: ["name"],
856
857
  filter: {
@@ -3347,6 +3348,7 @@ var DatabaseController = class _DatabaseController {
3347
3348
  if (!data || Array.isArray(data) && data.length === 0) {
3348
3349
  return { errored: true, __error: "No data provided for insert" };
3349
3350
  }
3351
+ let resultContext = {};
3350
3352
  const client = transaction ? await this.getClient() : this.dbClient;
3351
3353
  try {
3352
3354
  if (transaction) await client.query("BEGIN");
@@ -3399,24 +3401,32 @@ var DatabaseController = class _DatabaseController {
3399
3401
  );
3400
3402
  if (transaction) await client.query("COMMIT");
3401
3403
  const resultRows = this.toCamelCase(result.rows);
3402
- return {
3404
+ resultContext = {
3403
3405
  [`${(0, import_lodash_es.camelCase)(tableName)}${isBatch ? "s" : ""}`]: isBatch ? resultRows : resultRows[0],
3404
3406
  rowCount: result.rowCount,
3405
3407
  __success: true
3406
3408
  };
3407
3409
  } catch (error) {
3408
- if (transaction) await client.query("ROLLBACK");
3409
- return {
3410
- ...context,
3411
- errored: true,
3412
- __error: `Insert failed: ${error.message}`,
3413
- __success: false
3414
- };
3410
+ if (error.message.includes("violates unique constraint")) {
3411
+ resultContext = {
3412
+ [`${(0, import_lodash_es.camelCase)(tableName)}`]: null,
3413
+ __success: false
3414
+ };
3415
+ } else {
3416
+ if (transaction) await client.query("ROLLBACK");
3417
+ resultContext = {
3418
+ ...context,
3419
+ errored: true,
3420
+ __error: `Insert failed: ${error.message}`,
3421
+ __success: false
3422
+ };
3423
+ }
3415
3424
  } finally {
3416
3425
  if (transaction && client) {
3417
3426
  client.release();
3418
3427
  }
3419
3428
  }
3429
+ return resultContext;
3420
3430
  }
3421
3431
  /**
3422
3432
  * Updates a database table with the provided data and filter conditions.
@@ -3438,6 +3448,7 @@ var DatabaseController = class _DatabaseController {
3438
3448
  if (!data || Object.keys(data).length === 0) {
3439
3449
  return { errored: true, __error: "No data provided for update" };
3440
3450
  }
3451
+ let resultContext = {};
3441
3452
  const client = transaction ? await this.getClient() : this.dbClient;
3442
3453
  try {
3443
3454
  if (transaction) await client.query("BEGIN");
@@ -3470,19 +3481,20 @@ var DatabaseController = class _DatabaseController {
3470
3481
  if (transaction) await client.query("COMMIT");
3471
3482
  const rows = this.toCamelCase(result.rows);
3472
3483
  if (rows.length === 0) {
3473
- return {
3484
+ resultContext = {
3474
3485
  sql,
3475
3486
  params,
3476
3487
  __success: false
3477
3488
  };
3489
+ } else {
3490
+ resultContext = {
3491
+ [`${(0, import_lodash_es.camelCase)(tableName)}`]: rows[0],
3492
+ __success: true
3493
+ };
3478
3494
  }
3479
- return {
3480
- [`${(0, import_lodash_es.camelCase)(tableName)}`]: rows[0],
3481
- __success: true
3482
- };
3483
3495
  } catch (error) {
3484
3496
  if (transaction) await client.query("ROLLBACK");
3485
- return {
3497
+ resultContext = {
3486
3498
  ...context,
3487
3499
  errored: true,
3488
3500
  __error: `Update failed: ${error.message}`,
@@ -3493,6 +3505,7 @@ var DatabaseController = class _DatabaseController {
3493
3505
  client.release();
3494
3506
  }
3495
3507
  }
3508
+ return resultContext;
3496
3509
  }
3497
3510
  /**
3498
3511
  * Deletes a record from the specified database table based on the given filter criteria.
@@ -3509,6 +3522,7 @@ var DatabaseController = class _DatabaseController {
3509
3522
  if (Object.keys(filter).length === 0) {
3510
3523
  return { errored: true, __error: "No filter provided for delete" };
3511
3524
  }
3525
+ let resultContext = {};
3512
3526
  const client = transaction ? await this.getClient() : this.dbClient;
3513
3527
  try {
3514
3528
  if (transaction) await client.query("BEGIN");
@@ -3518,13 +3532,13 @@ var DatabaseController = class _DatabaseController {
3518
3532
  const result = await client.query(sql, params);
3519
3533
  if (transaction) await client.query("COMMIT");
3520
3534
  const rows = this.toCamelCase(result.rows);
3521
- return {
3535
+ resultContext = {
3522
3536
  [`${(0, import_lodash_es.camelCase)(tableName)}`]: rows[0],
3523
3537
  __success: true
3524
3538
  };
3525
3539
  } catch (error) {
3526
3540
  if (transaction) await client.query("ROLLBACK");
3527
- return {
3541
+ resultContext = {
3528
3542
  errored: true,
3529
3543
  __error: `Delete failed: ${error.message}`,
3530
3544
  __errors: { delete: error.message },
@@ -3535,6 +3549,7 @@ var DatabaseController = class _DatabaseController {
3535
3549
  client.release();
3536
3550
  }
3537
3551
  }
3552
+ return resultContext;
3538
3553
  }
3539
3554
  /**
3540
3555
  * Constructs a SQL WHERE clause based on the provided filter object.
@@ -3794,6 +3809,7 @@ var GraphSyncController = class _GraphSyncController {
3794
3809
  this.splitRoutinesTask = CadenzaService.createMetaTask(
3795
3810
  "Split routines for registration",
3796
3811
  (ctx, emit) => {
3812
+ console.log("SPLITTING ROUTINES FOR REGISTRATION");
3797
3813
  const { routines } = ctx;
3798
3814
  if (!routines) return;
3799
3815
  for (const routine of routines) {
@@ -3835,6 +3851,7 @@ var GraphSyncController = class _GraphSyncController {
3835
3851
  uuid: CadenzaService.serviceRegistry.serviceInstanceId
3836
3852
  }
3837
3853
  });
3854
+ CadenzaService.log("Synced resources...");
3838
3855
  }
3839
3856
  ).attachSignal(
3840
3857
  "global.meta.sync_controller.routine_added",
@@ -3883,6 +3900,7 @@ var GraphSyncController = class _GraphSyncController {
3883
3900
  this.splitTasksForRegistration = CadenzaService.createMetaTask(
3884
3901
  "Split tasks for registration",
3885
3902
  function* (ctx) {
3903
+ console.log("SPLITTING TASKS FOR REGISTRATION");
3886
3904
  const tasks = ctx.tasks;
3887
3905
  for (const task of tasks) {
3888
3906
  if (task.registered) continue;
@@ -4149,7 +4167,9 @@ var GraphSyncController = class _GraphSyncController {
4149
4167
  )
4150
4168
  );
4151
4169
  CadenzaService.throttle("sync_controller.sync_tick", { __syncing: true }, 12e4);
4152
- CadenzaService.schedule("meta.sync_requested", { __syncing: true }, 2e3);
4170
+ if (this.isCadenzaDBReady) {
4171
+ CadenzaService.schedule("meta.sync_requested", { __syncing: true }, 2e3);
4172
+ }
4153
4173
  }
4154
4174
  };
4155
4175
 
@@ -4501,7 +4521,8 @@ var CadenzaService = class {
4501
4521
  options.isMeta = true;
4502
4522
  const name = `Transmit signal: ${signalName} to ${serviceName}`;
4503
4523
  if (this.get(name)) {
4504
- throw new Error(`Task '${name}' already exists`);
4524
+ console.log("Signal transmission task already exists", name);
4525
+ return;
4505
4526
  }
4506
4527
  return new SignalTransmissionTask(
4507
4528
  name,
@@ -4767,11 +4788,17 @@ var CadenzaService = class {
4767
4788
  __isDatabase: options.isDatabase
4768
4789
  };
4769
4790
  if (options.cadenzaDB?.connect) {
4770
- this.createMetaTask("Create service", async (_, emit) => {
4791
+ this.createEphemeralMetaTask("Create service", async (context, emit) => {
4771
4792
  emit("meta.create_service_requested", initContext);
4772
4793
  }).doOn("meta.fetch.handshake_complete");
4773
4794
  } else {
4774
4795
  this.emit("meta.create_service_requested", initContext);
4796
+ this.createMetaTask("Create signal transmission for sync", (ctx) => {
4797
+ this.createSignalTransmissionTask(
4798
+ "global.meta.cadenza_db.gathered_sync_data",
4799
+ ctx.serviceName
4800
+ );
4801
+ }).doOn("meta.rest.handshake");
4775
4802
  }
4776
4803
  this.createMetaTask("Handle service setup completion", () => {
4777
4804
  GraphMetadataController.instance;