Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab0cb58196 |
@@ -25,10 +25,8 @@ const StandardsController = new Elysia({ prefix: "/standards" })
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
.put("/", async ({ params: { id }, body: { name, datasetId, params: parameters } }) => {
|
||||
// Active config is not allowed to be edited
|
||||
const activeConfig = await s.Calculator.Standards.Config.get();
|
||||
if (activeConfig.id === id) throw new ConflictError("Cannot edit the active standards config");
|
||||
|
||||
const updated = await s.Standards.Configs.update(id, name, datasetId, parameters);
|
||||
if (!updated) throw new NotFoundError("Standards config not found");
|
||||
}, {
|
||||
|
||||
BIN
Binary file not shown.
Binary file not shown.
@@ -56,9 +56,6 @@ export const app = new Elysia()
|
||||
switch (code) {
|
||||
case "HttpError":
|
||||
break;
|
||||
case "CalculatorUnavailableError":
|
||||
log.error({ err: error.cause }, error.message);
|
||||
break;
|
||||
case "PrintfulError":
|
||||
case "WebflowError":
|
||||
log.error({ upstreamStatus: error.upstreamStatus, payload: error.payload }, error.message);
|
||||
|
||||
@@ -65,7 +65,7 @@ class ApparelOrdersService {
|
||||
shippingAddress: t.Object({
|
||||
addressee: t.String(),
|
||||
line1: t.String(),
|
||||
line2: t.Optional(t.Union([t.String(), t.Null()])),
|
||||
line2: t.String(),
|
||||
city: t.String(),
|
||||
country: t.String(),
|
||||
postalCode: t.String(),
|
||||
@@ -82,12 +82,6 @@ class ApparelOrdersService {
|
||||
processor: async (payload) => {
|
||||
const wOrder = payload.wOrder;
|
||||
|
||||
// a previous attempt may have created the order before failing
|
||||
if (await this.Printful.Orders.get(`@${wOrder.orderId}`)) {
|
||||
log.info({ wOrderId: wOrder.orderId }, "printful order already exists, skipping creation");
|
||||
return;
|
||||
}
|
||||
|
||||
const stateCode = zipcodesUs.find(
|
||||
wOrder.shippingAddress.postalCode.split("-")[0] ?? "",
|
||||
).stateCode;
|
||||
@@ -101,7 +95,7 @@ class ApparelOrdersService {
|
||||
recipient: {
|
||||
name: wOrder.shippingAddress.addressee,
|
||||
address1: wOrder.shippingAddress.line1,
|
||||
address2: wOrder.shippingAddress.line2 ?? "",
|
||||
address2: wOrder.shippingAddress.line2,
|
||||
city: wOrder.shippingAddress.city,
|
||||
state_code: stateCode,
|
||||
country_code: wOrder.shippingAddress.country,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { sql, type Selectable, type Transaction } from "kysely";
|
||||
import { type Selectable, type Transaction } from "kysely";
|
||||
import type { DB } from "../database/out/db";
|
||||
import { log } from "../util/log";
|
||||
import { sleep } from "bun";
|
||||
@@ -29,6 +29,7 @@ type EventQueueCfg = {
|
||||
},
|
||||
};
|
||||
|
||||
class AlreadyClaimedEventError extends Error { }
|
||||
class ConcurrencyLimitReachedError extends Error { }
|
||||
|
||||
export function defineEventType<P extends TSchema, S extends TSchema>(opt: EventTypeOptions<P, S>): EventTypeOptions<P, S> {
|
||||
@@ -45,6 +46,7 @@ function exponentialDelay(initialDelay: number, tries: number, cap: number = Inf
|
||||
}
|
||||
|
||||
export class EventQueue<G extends string, T extends Record<string, EventTypeOptions<any, any>>> {
|
||||
private static readonly DEQUEUE_ALREADY_CLAIMED_SKIP_DELAY_MS = 250;
|
||||
private static readonly DEQUEUE_RETRY_DELAY_MS = secToMs(10);
|
||||
private static readonly DEQUEUE_RETRY_LIMIT = 3;
|
||||
private static readonly RATE_LIMIT_RETRY_LIMIT = 15;
|
||||
@@ -75,8 +77,15 @@ export class EventQueue<G extends string, T extends Record<string, EventTypeOpti
|
||||
consecutiveFailures = 0;
|
||||
}
|
||||
catch (err) {
|
||||
// Return to the manager loop so it can clean timed out events
|
||||
if (err instanceof ConcurrencyLimitReachedError) break;
|
||||
if (err instanceof AlreadyClaimedEventError) {
|
||||
log.warn("event processing already claimed by another worker, skipping");
|
||||
await sleep(EventQueue.DEQUEUE_ALREADY_CLAIMED_SKIP_DELAY_MS);
|
||||
continue;
|
||||
}
|
||||
if (err instanceof ConcurrencyLimitReachedError) {
|
||||
await sleep(EventQueue.DEQUEUE_RETRY_DELAY_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (++consecutiveFailures <= EventQueue.DEQUEUE_RETRY_LIMIT) {
|
||||
log.warn({ err, consecutiveFailures }, "dequeue failed, retrying")
|
||||
@@ -93,30 +102,38 @@ export class EventQueue<G extends string, T extends Record<string, EventTypeOpti
|
||||
async dequeue() {
|
||||
log.trace("dequeueing");
|
||||
|
||||
// Serialize claims per group so concurrent workers see each other's claims when checking the concurrency limit
|
||||
const front = await db.transaction().execute(async (trx) => {
|
||||
await sql`select pg_advisory_xact_lock(hashtext(${this.group}))`.execute(trx);
|
||||
|
||||
const { count } = await trx.selectFrom("events")
|
||||
.select(trx.fn.countAll().as("count"))
|
||||
.where("group", "=", this.group)
|
||||
.where("started_at", "is not", null)
|
||||
.where("ended_at", "is", null)
|
||||
.executeTakeFirstOrThrow();
|
||||
if (Number(count) >= this.cfg.concurrency.limit) throw new ConcurrencyLimitReachedError();
|
||||
|
||||
const front = await this.front(trx);
|
||||
if (!front) return;
|
||||
|
||||
await trx.updateTable("events")
|
||||
.set({ started_at: new Date(), heartbeat_at: new Date() })
|
||||
.where("id", "=", front.id)
|
||||
.execute();
|
||||
|
||||
return front;
|
||||
});
|
||||
const front = await this.front();
|
||||
if (!front) return;
|
||||
|
||||
const result = await db.updateTable("events")
|
||||
.set({ started_at: new Date(), heartbeat_at: new Date() })
|
||||
.where("id", "=", front.id)
|
||||
.where("started_at", "is", null)
|
||||
.where((eb) =>
|
||||
eb(
|
||||
eb.selectFrom("events")
|
||||
.select(eb.fn.countAll().as("count"))
|
||||
.where("group", "=", this.group)
|
||||
.where("started_at", "is not", null)
|
||||
.where("ended_at", "is", null),
|
||||
"<",
|
||||
this.cfg.concurrency.limit
|
||||
)
|
||||
)
|
||||
.executeTakeFirstOrThrow();
|
||||
|
||||
// Guard against another worker/machine having already claimed this event and guard against max concurrency
|
||||
if (result.numUpdatedRows === 0n) {
|
||||
const frontFresh = await db.selectFrom("events")
|
||||
.select("started_at")
|
||||
.where("id", "=", front.id)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (!frontFresh) throw new AlreadyClaimedEventError();
|
||||
if (frontFresh?.started_at) throw new AlreadyClaimedEventError();
|
||||
throw new ConcurrencyLimitReachedError();
|
||||
};
|
||||
|
||||
log.trace({ name: front.id }, "dequeuing event");
|
||||
await this.process(front);
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ export class EventsService {
|
||||
|
||||
async resolve(id: string, trx?: Transaction<DB>): Promise<void> {
|
||||
await (trx ?? db).updateTable("events")
|
||||
.set({ state: null, ended_at: new Date(), has_failed: false, last_error: null })
|
||||
.set({ state: null, ended_at: new Date(), last_error: null })
|
||||
.where("id", "=", id)
|
||||
.execute();
|
||||
}
|
||||
@@ -132,8 +132,6 @@ export class EventsService {
|
||||
started_at: null,
|
||||
ended_at: null,
|
||||
has_failed: false,
|
||||
errors: 0,
|
||||
rate_limits: 0,
|
||||
available_at: new Date(),
|
||||
})
|
||||
.where("id", "=", id)
|
||||
|
||||
Reference in New Issue
Block a user