| ... | ... | @@ -12,10 +12,19 @@ import { relayDeliveryAttempts } from "../persistence/schema.ts"; |
| 12 | 12 | |
| 13 | 13 | export class DeliveryAttemptRecordPersistenceError extends Schema.TaggedErrorClass<DeliveryAttemptRecordPersistenceError>()( |
| 14 | 14 | "DeliveryAttemptRecordPersistenceError", |
| 15 | | { cause: Schema.Defect() }, |
| 15 | { |
| 16 | operation: Schema.Literals(["record", "claim-source-job", "complete-source-job"]), |
| 17 | sourceJobId: Schema.NullOr(Schema.String), |
| 18 | userId: Schema.NullOr(Schema.String), |
| 19 | environmentId: Schema.NullOr(Schema.String), |
| 20 | threadId: Schema.NullOr(Schema.String), |
| 21 | deviceId: Schema.NullOr(Schema.String), |
| 22 | kind: Schema.NullOr(Schema.String), |
| 23 | cause: Schema.Defect(), |
| 24 | }, |
| 16 | 25 | ) { |
| 17 | 26 | override get message(): string { |
| 18 | | return "Failed to persist APNs delivery attempt"; |
| 27 | return `Failed to persist APNs delivery attempt during ${this.operation}.`; |
| 19 | 28 | } |
| 20 | 29 | } |
| 21 | 30 | |
| ... | ... | @@ -99,30 +108,43 @@ export const make = Effect.gen(function* () { |
| 99 | 108 | }; |
| 100 | 109 | |
| 101 | 110 | return DeliveryAttempts.of({ |
| 102 | | record: Effect.fn("relay.delivery_attempts.record")( |
| 103 | | function* (input) { |
| 104 | | yield* Effect.annotateCurrentSpan({ |
| 105 | | "relay.delivery.kind": input.kind, |
| 106 | | ...(input.sourceJobId ? { "relay.delivery.job_id": input.sourceJobId } : {}), |
| 107 | | ...(input.deviceId ? { "relay.mobile.device_id": input.deviceId } : {}), |
| 108 | | ...(input.environmentId ? { "relay.environment_id": input.environmentId } : {}), |
| 109 | | ...(input.threadId ? { "relay.thread_id": input.threadId } : {}), |
| 110 | | }); |
| 111 | record: Effect.fn("relay.delivery_attempts.record")(function* (input) { |
| 112 | yield* Effect.annotateCurrentSpan({ |
| 113 | "relay.delivery.kind": input.kind, |
| 114 | ...(input.sourceJobId ? { "relay.delivery.job_id": input.sourceJobId } : {}), |
| 115 | ...(input.deviceId ? { "relay.mobile.device_id": input.deviceId } : {}), |
| 116 | ...(input.environmentId ? { "relay.environment_id": input.environmentId } : {}), |
| 117 | ...(input.threadId ? { "relay.thread_id": input.threadId } : {}), |
| 118 | }); |
| 119 | yield* Effect.gen(function* () { |
| 111 | 120 | const id = yield* crypto.randomUUIDv4; |
| 112 | 121 | const createdAt = DateTime.formatIso(yield* DateTime.now); |
| 113 | 122 | yield* db.insert(relayDeliveryAttempts).values(insertValues(input, id, createdAt)); |
| 114 | | }, |
| 115 | | Effect.mapError((cause) => new DeliveryAttemptRecordPersistenceError({ cause })), |
| 116 | | ), |
| 117 | | claimSourceJob: Effect.fn("relay.delivery_attempts.claim_source_job")( |
| 118 | | function* (input) { |
| 119 | | yield* Effect.annotateCurrentSpan({ |
| 120 | | "relay.delivery.kind": input.kind, |
| 121 | | "relay.delivery.job_id": input.sourceJobId, |
| 122 | | ...(input.deviceId ? { "relay.mobile.device_id": input.deviceId } : {}), |
| 123 | | ...(input.environmentId ? { "relay.environment_id": input.environmentId } : {}), |
| 124 | | ...(input.threadId ? { "relay.thread_id": input.threadId } : {}), |
| 125 | | }); |
| 123 | }).pipe( |
| 124 | Effect.mapError( |
| 125 | (cause) => |
| 126 | new DeliveryAttemptRecordPersistenceError({ |
| 127 | operation: "record", |
| 128 | sourceJobId: input.sourceJobId ?? null, |
| 129 | userId: input.userId, |
| 130 | environmentId: input.environmentId, |
| 131 | threadId: input.threadId, |
| 132 | deviceId: input.deviceId, |
| 133 | kind: input.kind, |
| 134 | cause, |
| 135 | }), |
| 136 | ), |
| 137 | ); |
| 138 | }), |
| 139 | claimSourceJob: Effect.fn("relay.delivery_attempts.claim_source_job")(function* (input) { |
| 140 | yield* Effect.annotateCurrentSpan({ |
| 141 | "relay.delivery.kind": input.kind, |
| 142 | "relay.delivery.job_id": input.sourceJobId, |
| 143 | ...(input.deviceId ? { "relay.mobile.device_id": input.deviceId } : {}), |
| 144 | ...(input.environmentId ? { "relay.environment_id": input.environmentId } : {}), |
| 145 | ...(input.threadId ? { "relay.thread_id": input.threadId } : {}), |
| 146 | }); |
| 147 | return yield* Effect.gen(function* () { |
| 126 | 148 | const id = yield* crypto.randomUUIDv4; |
| 127 | 149 | const now = yield* DateTime.now; |
| 128 | 150 | const createdAt = DateTime.formatIso(now); |
| ... | ... | @@ -179,26 +201,51 @@ export const make = Effect.gen(function* () { |
| 179 | 201 | ) |
| 180 | 202 | .returning({ id: relayDeliveryAttempts.id }); |
| 181 | 203 | return reclaimed.length > 0 ? "claimed" : "in_flight"; |
| 182 | | }, |
| 183 | | Effect.mapError((cause) => new DeliveryAttemptRecordPersistenceError({ cause })), |
| 184 | | ), |
| 185 | | completeSourceJob: Effect.fn("relay.delivery_attempts.complete_source_job")( |
| 186 | | function* (input) { |
| 187 | | yield* Effect.annotateCurrentSpan({ "relay.delivery.job_id": input.sourceJobId }); |
| 188 | | const completedAt = DateTime.formatIso(yield* DateTime.now); |
| 189 | | yield* db |
| 190 | | .update(relayDeliveryAttempts) |
| 191 | | .set({ |
| 192 | | createdAt: completedAt, |
| 193 | | apnsStatus: input.apnsStatus ?? null, |
| 194 | | apnsReason: input.apnsReason ?? null, |
| 195 | | apnsId: input.apnsId ?? null, |
| 196 | | transportError: input.transportError ?? null, |
| 197 | | }) |
| 198 | | .where(eq(relayDeliveryAttempts.sourceJobId, input.sourceJobId)); |
| 199 | | }, |
| 200 | | Effect.mapError((cause) => new DeliveryAttemptRecordPersistenceError({ cause })), |
| 201 | | ), |
| 204 | }).pipe( |
| 205 | Effect.mapError( |
| 206 | (cause) => |
| 207 | new DeliveryAttemptRecordPersistenceError({ |
| 208 | operation: "claim-source-job", |
| 209 | sourceJobId: input.sourceJobId, |
| 210 | userId: input.userId, |
| 211 | environmentId: input.environmentId, |
| 212 | threadId: input.threadId, |
| 213 | deviceId: input.deviceId, |
| 214 | kind: input.kind, |
| 215 | cause, |
| 216 | }), |
| 217 | ), |
| 218 | ); |
| 219 | }), |
| 220 | completeSourceJob: Effect.fn("relay.delivery_attempts.complete_source_job")(function* (input) { |
| 221 | yield* Effect.annotateCurrentSpan({ "relay.delivery.job_id": input.sourceJobId }); |
| 222 | const completedAt = DateTime.formatIso(yield* DateTime.now); |
| 223 | yield* db |
| 224 | .update(relayDeliveryAttempts) |
| 225 | .set({ |
| 226 | createdAt: completedAt, |
| 227 | apnsStatus: input.apnsStatus ?? null, |
| 228 | apnsReason: input.apnsReason ?? null, |
| 229 | apnsId: input.apnsId ?? null, |
| 230 | transportError: input.transportError ?? null, |
| 231 | }) |
| 232 | .where(eq(relayDeliveryAttempts.sourceJobId, input.sourceJobId)) |
| 233 | .pipe( |
| 234 | Effect.mapError( |
| 235 | (cause) => |
| 236 | new DeliveryAttemptRecordPersistenceError({ |
| 237 | operation: "complete-source-job", |
| 238 | sourceJobId: input.sourceJobId, |
| 239 | userId: null, |
| 240 | environmentId: null, |
| 241 | threadId: null, |
| 242 | deviceId: null, |
| 243 | kind: null, |
| 244 | cause, |
| 245 | }), |
| 246 | ), |
| 247 | ); |
| 248 | }), |
| 202 | 249 | }); |
| 203 | 250 | }); |
| 204 | 251 | |