@fedify/postgres 2.0.0-dev.395 → 2.0.0-dev.396

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.
Files changed (3) hide show
  1. package/dist/mq.cjs +20 -2
  2. package/dist/mq.js +20 -2
  3. package/package.json +4 -4
package/dist/mq.cjs CHANGED
@@ -12,6 +12,14 @@ const logger = (0, __logtape_logtape.getLogger)([
12
12
  "postgres",
13
13
  "mq"
14
14
  ]);
15
+ const INITIALIZE_MAX_ATTEMPTS = 5;
16
+ const INITIALIZE_BACKOFF_MS = 10;
17
+ function sleep(milliseconds) {
18
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
19
+ }
20
+ function isInitializationRaceError(error) {
21
+ return error instanceof postgres.default.PostgresError && (error.constraint_name === "pg_type_typname_nsp_index" || error.code === "42P07" || error.code === "42710");
22
+ }
15
23
  /**
16
24
  * A message queue that uses PostgreSQL as the underlying storage.
17
25
  *
@@ -239,7 +247,7 @@ var PostgresMessageQueue = class {
239
247
  }
240
248
  async #doInitialize() {
241
249
  logger.debug("Initializing the message queue table {tableName}...", { tableName: this.#tableName });
242
- try {
250
+ for (let attempt = 1; attempt <= INITIALIZE_MAX_ATTEMPTS; attempt++) try {
243
251
  await this.#sql`
244
252
  CREATE TABLE IF NOT EXISTS ${this.#sql(this.#tableName)} (
245
253
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
@@ -253,11 +261,21 @@ var PostgresMessageQueue = class {
253
261
  ALTER TABLE ${this.#sql(this.#tableName)}
254
262
  ADD COLUMN IF NOT EXISTS ordering_key text;
255
263
  `;
264
+ break;
256
265
  } catch (error) {
257
- if (!(error instanceof postgres.default.PostgresError && (error.constraint_name === "pg_type_typname_nsp_index" || error.code === "42P07"))) {
266
+ if (!isInitializationRaceError(error) || attempt >= INITIALIZE_MAX_ATTEMPTS) {
258
267
  logger.error("Failed to initialize the message queue table: {error}", { error });
259
268
  throw error;
260
269
  }
270
+ const backoffMs = INITIALIZE_BACKOFF_MS * 2 ** (attempt - 1);
271
+ logger.debug("Initialization raced for table {tableName}; retrying in {backoffMs}ms (attempt {attempt}/{maxAttempts}).", {
272
+ tableName: this.#tableName,
273
+ backoffMs,
274
+ attempt,
275
+ maxAttempts: INITIALIZE_MAX_ATTEMPTS,
276
+ error
277
+ });
278
+ await sleep(backoffMs);
261
279
  }
262
280
  this.#driverSerializesJson = await require_utils.driverSerializesJson(this.#sql);
263
281
  this.#initialized = true;
package/dist/mq.js CHANGED
@@ -11,6 +11,14 @@ const logger = getLogger([
11
11
  "postgres",
12
12
  "mq"
13
13
  ]);
14
+ const INITIALIZE_MAX_ATTEMPTS = 5;
15
+ const INITIALIZE_BACKOFF_MS = 10;
16
+ function sleep(milliseconds) {
17
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
18
+ }
19
+ function isInitializationRaceError(error) {
20
+ return error instanceof postgres.PostgresError && (error.constraint_name === "pg_type_typname_nsp_index" || error.code === "42P07" || error.code === "42710");
21
+ }
14
22
  /**
15
23
  * A message queue that uses PostgreSQL as the underlying storage.
16
24
  *
@@ -238,7 +246,7 @@ var PostgresMessageQueue = class {
238
246
  }
239
247
  async #doInitialize() {
240
248
  logger.debug("Initializing the message queue table {tableName}...", { tableName: this.#tableName });
241
- try {
249
+ for (let attempt = 1; attempt <= INITIALIZE_MAX_ATTEMPTS; attempt++) try {
242
250
  await this.#sql`
243
251
  CREATE TABLE IF NOT EXISTS ${this.#sql(this.#tableName)} (
244
252
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
@@ -252,11 +260,21 @@ var PostgresMessageQueue = class {
252
260
  ALTER TABLE ${this.#sql(this.#tableName)}
253
261
  ADD COLUMN IF NOT EXISTS ordering_key text;
254
262
  `;
263
+ break;
255
264
  } catch (error) {
256
- if (!(error instanceof postgres.PostgresError && (error.constraint_name === "pg_type_typname_nsp_index" || error.code === "42P07"))) {
265
+ if (!isInitializationRaceError(error) || attempt >= INITIALIZE_MAX_ATTEMPTS) {
257
266
  logger.error("Failed to initialize the message queue table: {error}", { error });
258
267
  throw error;
259
268
  }
269
+ const backoffMs = INITIALIZE_BACKOFF_MS * 2 ** (attempt - 1);
270
+ logger.debug("Initialization raced for table {tableName}; retrying in {backoffMs}ms (attempt {attempt}/{maxAttempts}).", {
271
+ tableName: this.#tableName,
272
+ backoffMs,
273
+ attempt,
274
+ maxAttempts: INITIALIZE_MAX_ATTEMPTS,
275
+ error
276
+ });
277
+ await sleep(backoffMs);
260
278
  }
261
279
  this.#driverSerializesJson = await driverSerializesJson(this.#sql);
262
280
  this.#initialized = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/postgres",
3
- "version": "2.0.0-dev.395+0d27c2dd",
3
+ "version": "2.0.0-dev.396+e21a5cbe",
4
4
  "description": "PostgreSQL drivers for Fedify",
5
5
  "keywords": [
6
6
  "fedify",
@@ -74,14 +74,14 @@
74
74
  },
75
75
  "peerDependencies": {
76
76
  "postgres": "^3.4.7",
77
- "@fedify/fedify": "^2.0.0-dev.395+0d27c2dd"
77
+ "@fedify/fedify": "^2.0.0-dev.396+e21a5cbe"
78
78
  },
79
79
  "devDependencies": {
80
80
  "@std/async": "npm:@jsr/std__async@^1.0.13",
81
81
  "tsdown": "^0.12.9",
82
82
  "typescript": "^5.9.3",
83
- "@fedify/testing": "^2.0.0-dev.395+0d27c2dd",
84
- "@fedify/fixture": "^2.0.0"
83
+ "@fedify/fixture": "^2.0.0",
84
+ "@fedify/testing": "^2.0.0-dev.396+e21a5cbe"
85
85
  },
86
86
  "scripts": {
87
87
  "build:self": "tsdown",