@kumori/aurora-backend-handler 1.0.135 → 1.0.137

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.
@@ -1,4 +1,4 @@
1
- import { Channel, Resource, Service, Tenant, Usage } from "@kumori/aurora-interfaces";
1
+ import { Channel, errors, Notification, Resource, Service, Tenant, Usage } from "@kumori/aurora-interfaces";
2
2
  import { Revision } from "@kumori/aurora-interfaces/interfaces/revision-interface";
3
3
  import { getTimestamp } from "../utils/utils";
4
4
 
@@ -98,6 +98,11 @@ const parseKeyPath = (key: string): { [entity: string]: string } => {
98
98
  return result;
99
99
  };
100
100
 
101
+ const firstString = (value: unknown): string => {
102
+ const first = Array.isArray(value) ? value[0] : value;
103
+ return typeof first === "string" ? first : "";
104
+ };
105
+
101
106
  const getDefaultUsage = (eventData?: any): Usage => ({
102
107
  current: {
103
108
  cpu: 0,
@@ -222,9 +227,9 @@ export const handleServiceEvent = ({
222
227
 
223
228
  const baseServiceData = {
224
229
  tenant: serviceTenantId,
225
- account: eventData.spec.metadata.targetAccount || pathParts.account,
230
+ account: firstString(eventData.spec.metadata.targetAccount) || pathParts.account,
226
231
  environment:
227
- eventData.spec.metadata.targetEnvironment || pathParts.environment,
232
+ firstString(eventData.spec.metadata.targetEnvironment) || pathParts.environment,
228
233
  name: entityId,
229
234
  revisions: [...serviceRevisions].sort(
230
235
  (a, b) => Number(b.id) - Number(a.id),
@@ -525,4 +530,56 @@ export const mapChannelsFromApiData = (
525
530
  mapChannels(apiData.duplex, duplexChannels);
526
531
 
527
532
  return { serverChannels, clientChannels, duplexChannels };
528
- };
533
+ };
534
+
535
+ const instancesNotRunningNotifications = new Map<string, Notification>();
536
+ let lastInstancesNotRunningNotificationDate = 0;
537
+
538
+ const hasInstancesNotRunning = (service: Service): boolean =>
539
+ (service.role ?? []).some((role) =>
540
+ (role.instances ?? []).some(
541
+ (instance) => instance.status?.toLowerCase() !== "running",
542
+ ),
543
+ );
544
+
545
+ export const syncInstancesNotRunningNotifications = (
546
+ services: Iterable<Service>,
547
+ eventHelper: any,
548
+ ): void => {
549
+ const pending = new Map<string, Record<string, string>>();
550
+ for (const service of services) {
551
+ if (service.status?.code === "REMOVING_SERVICE") continue;
552
+ if (!hasInstancesNotRunning(service)) continue;
553
+ const { tenant, account, environment, name } = service;
554
+ pending.set(`${tenant}/${account}/${environment}/${name}`, {
555
+ tenant,
556
+ account,
557
+ environment,
558
+ service: name,
559
+ });
560
+ }
561
+
562
+ instancesNotRunningNotifications.forEach((notification, key) => {
563
+ if (pending.has(key)) return;
564
+ instancesNotRunningNotifications.delete(key);
565
+ eventHelper.notification.publish.deletion(notification);
566
+ });
567
+
568
+ pending.forEach((data, key) => {
569
+ if (instancesNotRunningNotifications.has(key)) return;
570
+ lastInstancesNotRunningNotificationDate = Math.max(
571
+ Date.now(),
572
+ lastInstancesNotRunningNotificationDate + 1,
573
+ );
574
+ const notification: Notification = {
575
+ type: "warning",
576
+ subtype: errors.service.instancesNotRunning.subtype,
577
+ date: lastInstancesNotRunningNotificationDate.toString(),
578
+ status: "unread",
579
+ notificationType: "regular",
580
+ data,
581
+ };
582
+ instancesNotRunningNotifications.set(key, notification);
583
+ eventHelper.notification.publish.creation(notification);
584
+ });
585
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumori/aurora-backend-handler",
3
- "version": "1.0.135",
3
+ "version": "1.0.137",
4
4
  "description": "backend handler",
5
5
  "main": "backend-handler.ts",
6
6
  "scripts": {
@@ -11,7 +11,7 @@
11
11
  "glob": "^11.0.0"
12
12
  },
13
13
  "dependencies": {
14
- "@kumori/aurora-interfaces": "^1.0.40",
14
+ "@kumori/aurora-interfaces": "1.0.41",
15
15
  "@kumori/kumori-dsl-generator": "1.0.11",
16
16
  "@kumori/kumori-module-generator": "^1.1.6",
17
17
  "ts-node": "^10.9.2",
@@ -49,6 +49,7 @@ import {
49
49
  handleServiceOperationError,
50
50
  handleServiceOperationSuccess,
51
51
  mapChannelsFromApiData,
52
+ syncInstancesNotRunningNotifications,
52
53
  } from "./helpers/service-helper";
53
54
  import {
54
55
  handleAccountEvent,
@@ -685,6 +686,7 @@ const rebuildHierarchy = () => {
685
686
  accountsMap.values(),
686
687
  eventHelper,
687
688
  );
689
+ syncInstancesNotRunningNotifications(servicesMap.values(), eventHelper);
688
690
 
689
691
  tenantsMap.forEach((tenant) => {
690
692
  tenant.accounts = Array.from(accountsMap.values()).filter((account) => {