@kumori/aurora-backend-handler 1.1.47 → 1.1.49

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.
@@ -159,12 +159,52 @@ const processRolesAndInstances = (
159
159
  return { roles, instances, usedCpu, usedMemory };
160
160
  };
161
161
 
162
+ const getContainerError = (
163
+ revisionStatus: any,
164
+ ): { code: string; message: string; timestamp: string } | null => {
165
+ for (const roleData of Object.values<any>(revisionStatus?.runtime?.roles ?? {})) {
166
+ for (const instanceData of Object.values<any>(roleData.instances ?? {})) {
167
+ for (const container of Object.values<any>(instanceData.containers ?? {})) {
168
+ if (!container.status.ready) {
169
+ return {
170
+ code: container.status.status.waiting?.reason ?? "ContainerNotReady",
171
+ message: container.status.status.waiting?.message ?? "One or more containers are not ready for unknown reasons.",
172
+ timestamp: Date.now().toString(),
173
+ };
174
+ }
175
+ }
176
+ }
177
+ }
178
+ return null;
179
+ };
180
+
181
+ const handleRevisionStatus = (revisionStatus: any) => {
182
+ if (revisionStatus?.error) {
183
+ return {
184
+ code: revisionStatus.error.code,
185
+ message: revisionStatus.error.message,
186
+ timestamp: Date.now().toString(),
187
+ };
188
+ }
189
+
190
+ const containerError = getContainerError(revisionStatus);
191
+ if (containerError) return containerError;
192
+ if (revisionStatus?.runtime?.status) {
193
+ return revisionStatus.runtime.status;
194
+ }
195
+ return revisionStatus?.state;
196
+ };
197
+
162
198
  const createRevision = (
163
199
  entityId: string,
164
200
  eventData: any,
165
201
  usedCpu: number,
166
202
  usedMemory: number,
167
203
  ): Revision => {
204
+ const status = handleRevisionStatus(eventData.status);
205
+ const explicitError = eventData.status?.error;
206
+ const containerError = !explicitError ? getContainerError(eventData.status) : null;
207
+
168
208
  return {
169
209
  id: entityId,
170
210
  schema: {},
@@ -199,30 +239,13 @@ const createRevision = (
199
239
  },
200
240
  cost: 0,
201
241
  },
202
- status: handleRevisionStatus(eventData.status),
203
- errorCode: eventData.status?.error ? eventData.status.error.code : handleRevisionStatus(eventData.status)?.code || "",
204
- errorMsg: eventData.status?.error ? eventData.status.error.message : handleRevisionStatus(eventData.status)?.message || "",
242
+ status,
243
+ errorCode: explicitError ? explicitError.code : containerError ? containerError.code : "",
244
+ errorMsg: explicitError ? explicitError.message : containerError ? containerError.message : "",
205
245
  createdAt:
206
246
  (eventData.status && eventData.status.runtime?.status?.createdAt) || "",
207
247
  };
208
248
  };
209
- const handleRevisionStatus = (revisionStatus: any) => {
210
- for (const roleData of Object.values<any>(revisionStatus.runtime.roles)) {
211
- for (const instanceData of Object.values<any>(roleData.instances)) {
212
- for (const container of Object.values<any>(instanceData.containers ?? {})) {
213
- if (!container.status.ready) {
214
- return {
215
- code: container.status.status.waiting?.reason ?? "ContainerNotReady",
216
- message: container.status.status.waiting?.message ?? "One or more containers are not ready for unknown reasons.",
217
- timestamp: Date.now().toString(),
218
- };
219
- }
220
- }
221
- }
222
- }
223
-
224
- return revisionStatus?.state;
225
- };
226
249
  const updateServiceWithRevision = (
227
250
  existingService: Service,
228
251
  entityId: string,
@@ -123,48 +123,59 @@ const determineFinalStatusAndError = (
123
123
  eventData: any,
124
124
  pendingRevisionErrors: Array<{ service: string; revision: Revision }>,
125
125
  entityId: string,
126
+ currentRevision: Revision | undefined,
126
127
  ): {
127
128
  finalStatus: any;
128
129
  finalError: any;
129
130
  pendingErrorIndex: number;
130
131
  } => {
131
- const incomingStatus = eventData.status.state;
132
- const incomingTs = getTimestamp(incomingStatus.timestamp);
132
+ const incomingServiceStatus = eventData.status.state;
133
+ const incomingTs = getTimestamp(incomingServiceStatus.timestamp);
133
134
  const currentTs = getTimestamp(existingService?.status?.timestamp);
134
-
135
135
  const isNewer = !existingService || incomingTs > currentTs;
136
- let finalStatus = existingService?.status;
137
- let finalError = existingService?.error;
138
-
139
- if (isNewer) {
140
- finalStatus = incomingStatus;
141
- if (eventData.status.error) {
142
- finalError = eventData.status.error;
143
- } else {
144
- finalError = undefined;
145
- }
146
- }
147
136
 
148
137
  const pendingErrorIndex = pendingRevisionErrors.findIndex(
149
138
  (pending) => pending.service === entityId,
150
139
  );
140
+ const pendingError =
141
+ pendingErrorIndex !== -1 ? pendingRevisionErrors[pendingErrorIndex] : null;
142
+ const serviceError = isNewer ? eventData.status.error : undefined;
143
+ const revisionError =
144
+ currentRevision?.errorCode
145
+ ? {
146
+ code: currentRevision.errorCode,
147
+ message: currentRevision.errorMsg || "",
148
+ timestamp: currentRevision.status?.timestamp || "",
149
+ }
150
+ : null;
151
151
 
152
- if (pendingErrorIndex !== -1) {
153
- const pendingError = pendingRevisionErrors[pendingErrorIndex];
154
- const pendingTs = getTimestamp(pendingError.revision.status.timestamp);
155
- const currentDecisionTs = getTimestamp(finalStatus?.timestamp);
152
+ if (serviceError) {
153
+ return { finalStatus: incomingServiceStatus, finalError: serviceError, pendingErrorIndex };
154
+ }
155
+
156
+ if (revisionError) {
157
+ return { finalStatus: currentRevision!.status, finalError: revisionError, pendingErrorIndex };
158
+ }
156
159
 
157
- if (pendingTs > currentDecisionTs) {
158
- finalStatus = pendingError.revision.status;
159
- finalError = {
160
+ if (pendingError) {
161
+ return {
162
+ finalStatus: pendingError.revision.status,
163
+ finalError: {
160
164
  code: pendingError.revision.errorCode || "",
161
165
  message: pendingError.revision.errorMsg || "",
162
- timestamp: pendingError.revision.status.timestamp || "",
163
- };
164
- }
166
+ timestamp: pendingError.revision.status?.timestamp || "",
167
+ },
168
+ pendingErrorIndex,
169
+ };
170
+ }
171
+ if (currentRevision?.status && isNewer) {
172
+ return { finalStatus: currentRevision.status, finalError: undefined, pendingErrorIndex };
173
+ }
174
+ if (isNewer) {
175
+ return { finalStatus: incomingServiceStatus, finalError: undefined, pendingErrorIndex };
165
176
  }
166
177
 
167
- return { finalStatus, finalError, pendingErrorIndex };
178
+ return { finalStatus: existingService?.status, finalError: existingService?.error, pendingErrorIndex };
168
179
  };
169
180
 
170
181
  export const handleServiceEvent = ({
@@ -203,6 +214,7 @@ export const handleServiceEvent = ({
203
214
  eventData,
204
215
  pendingRevisionErrors,
205
216
  entityId,
217
+ currentRevision,
206
218
  );
207
219
 
208
220
  const updatedPendingRevisionErrors = [...pendingRevisionErrors];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumori/aurora-backend-handler",
3
- "version": "1.1.47",
3
+ "version": "1.1.49",
4
4
  "description": "backend handler",
5
5
  "main": "backend-handler.ts",
6
6
  "scripts": {