@dbos-inc/dbos-sdk 5.5.8-preview → 5.6.0

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.
@@ -763,12 +763,35 @@ class SystemDatabase {
763
763
  this.customPool = true;
764
764
  }
765
765
  else {
766
+ const timeoutMs = this.idleTransactionTimeoutMs;
766
767
  const systemPoolConfig = {
767
768
  ...(0, utils_2.getClientConfig)(systemDatabaseUrl),
768
769
  // This sets the application_name column in pg_stat_activity
769
770
  application_name: `dbos_transact_${utils_1.globalParams.executorID}_${utils_1.globalParams.appVersion}`,
770
771
  max: sysDbPoolSize,
771
772
  };
773
+ if (timeoutMs !== undefined) {
774
+ // A SET, not a startup parameter, which session-mode PgBouncer rejects; awaited before the borrower gets the client.
775
+ systemPoolConfig.verify = (client, done) => {
776
+ // pg-pool has detached its listener and the borrower's comes after done, so a socket death here needs a guard.
777
+ let socketError;
778
+ const onError = (err) => {
779
+ socketError = err;
780
+ };
781
+ client.on('error', onError);
782
+ client.query(`SET idle_in_transaction_session_timeout = ${timeoutMs}`).then(() => {
783
+ client.removeListener('error', onError);
784
+ done();
785
+ }, (err) => {
786
+ client.removeListener('error', onError);
787
+ // A dead connection fails the checkout with a retryable error; pg-pool discards it.
788
+ if (socketError)
789
+ return done(socketError);
790
+ this.logger.warn(`Could not set idle_in_transaction_session_timeout on a system database connection: ${err}`);
791
+ done();
792
+ });
793
+ };
794
+ }
772
795
  this.pool = new pg_1.Pool(systemPoolConfig);
773
796
  }
774
797
  // Default the polling limit to half the pool (minimum 1), reserving the rest
@@ -782,15 +805,6 @@ class SystemDatabase {
782
805
  this.pool.on('error', (err) => {
783
806
  this.logger.warn(`Unexpected error in pool: ${err}`);
784
807
  });
785
- const timeoutMs = this.idleTransactionTimeoutMs;
786
- if (timeoutMs !== undefined) {
787
- // Queued ahead of the borrower's first query; outside any transaction, so it persists for the session.
788
- this.pool.on('connect', (client) => {
789
- client.query(`SET idle_in_transaction_session_timeout = ${timeoutMs}`).catch((err) => {
790
- this.logger.warn(`Could not set idle_in_transaction_session_timeout on a system database connection: ${err}`);
791
- });
792
- });
793
- }
794
808
  }
795
809
  }
796
810
  #onClientError = (err) => {
@@ -1282,7 +1296,7 @@ class SystemDatabase {
1282
1296
  serialization: resRow.serialization,
1283
1297
  };
1284
1298
  }
1285
- /** Move claimed workflows that exhausted their attempts off the queue, leaving rows others have moved on alone. */
1299
+ /** Dead-letter claimed workflows that exhausted their attempts, keeping their queue; rows others have moved on are left alone. */
1286
1300
  async deadLetterWorkflows(workflowIDs, minRecoveryAttempts) {
1287
1301
  if (workflowIDs.length === 0)
1288
1302
  return;
@@ -1292,7 +1306,6 @@ class SystemDatabase {
1292
1306
  owner_xid = NULL,
1293
1307
  deduplication_id = NULL,
1294
1308
  started_at_epoch_ms = NULL,
1295
- queue_name = NULL,
1296
1309
  updated_at = (EXTRACT(EPOCH FROM now()) * 1000)::bigint,
1297
1310
  completed_at = (EXTRACT(EPOCH FROM now()) * 1000)::bigint
1298
1311
  WHERE ${this.wfIn(undefined, workflowIDs, params)} AND status = $2 AND recovery_attempts >= $3`, params);
@@ -1622,7 +1635,8 @@ class SystemDatabase {
1622
1635
  }
1623
1636
  // Recovery re-enqueues rather than executing directly so the queue's atomic dequeue admits exactly one runner, and the executor ID predicate rejects sweeps for rows a live executor has already claimed.
1624
1637
  async reenqueueWorkflowsForRecovery(executorID, appVersion, recoveryQueueName) {
1625
- const params = [workflow_1.StatusString.ENQUEUED, recoveryQueueName, workflow_1.StatusString.PENDING, executorID, appVersion];
1638
+ const params = [workflow_1.StatusString.ENQUEUED, recoveryQueueName, workflow_1.StatusString.PENDING, executorID];
1639
+ const versionFilter = appVersion === undefined ? '' : `AND application_version = $${params.push(appVersion)}`;
1626
1640
  // executor_id defaults to "local", so it collides across applications.
1627
1641
  const scope = this.appNameFilter('application_name', this.appName, params);
1628
1642
  const keyColumn = this.keyColumn(undefined);
@@ -1634,7 +1648,7 @@ class SystemDatabase {
1634
1648
  queue_name = COALESCE(queue_name, $2)
1635
1649
  WHERE status = $3
1636
1650
  AND executor_id = $4
1637
- AND application_version = $5
1651
+ ${versionFilter}
1638
1652
  AND ${scope}
1639
1653
  RETURNING workflow_uuid${keyColumn !== undefined ? `, ${keyColumn} AS shard_key` : ''}`, params);
1640
1654
  return result.rows.map((row) => ({ workflowID: row.workflow_uuid, shardKey: row.shard_key ?? undefined }));
@@ -1883,7 +1897,8 @@ class SystemDatabase {
1883
1897
  async #cancelWorkflows(workflowIDs) {
1884
1898
  const params = [workflow_1.StatusString.CANCELLED, workflow_1.StatusString.SUCCESS, workflow_1.StatusString.ERROR];
1885
1899
  await this.pool.query(`UPDATE "${this.schemaName}".workflow_status
1886
- SET status = $1, owner_xid = NULL, queue_name = NULL, deduplication_id = NULL, started_at_epoch_ms = NULL,
1900
+ SET status = $1, owner_xid = NULL, deduplication_id = NULL, started_at_epoch_ms = NULL,
1901
+ suspend_key = NULL,
1887
1902
  updated_at = (EXTRACT(EPOCH FROM now()) * 1000)::bigint,
1888
1903
  completed_at = (EXTRACT(EPOCH FROM now()) * 1000)::bigint
1889
1904
  WHERE ${this.wfIn(undefined, workflowIDs, params)}
@@ -1893,7 +1908,7 @@ class SystemDatabase {
1893
1908
  // The caller's own workflow, even where a same-ID workflow is bound as a target.
1894
1909
  await this.ownKey(workflowID, undefined, () => this.#checkIfCanceled(this.pool, workflowID));
1895
1910
  }
1896
- async resumeWorkflows(workflowIDs, queueName) {
1911
+ async resumeWorkflows(workflowIDs, queueName, queuePartitionKey) {
1897
1912
  const client = await this.connect();
1898
1913
  try {
1899
1914
  await client.query('BEGIN ISOLATION LEVEL READ COMMITTED');
@@ -1905,16 +1920,20 @@ class SystemDatabase {
1905
1920
  if (missing.length > 0) {
1906
1921
  throw new error_1.DBOSNonExistentWorkflowError(`Workflow ${missing.join(', ')} does not exist`);
1907
1922
  }
1923
+ // Only what is passed changes: the row keeps its queue (the internal one if it has none) and key.
1908
1924
  const updateParams = [
1909
1925
  workflow_1.StatusString.ENQUEUED,
1910
- queueName ?? utils_1.INTERNAL_QUEUE_NAME,
1926
+ queueName ?? null,
1911
1927
  workflow_1.StatusString.SUCCESS,
1912
1928
  workflow_1.StatusString.ERROR,
1929
+ utils_1.INTERNAL_QUEUE_NAME,
1930
+ queuePartitionKey ?? null,
1913
1931
  ];
1914
1932
  await client.query(`UPDATE "${this.schemaName}".workflow_status
1915
- SET status = $1, owner_xid = NULL, queue_name = $2, recovery_attempts = 0,
1933
+ SET status = $1, owner_xid = NULL, queue_name = COALESCE($2::text, queue_name, $5::text),
1934
+ queue_partition_key = COALESCE($6::text, queue_partition_key), recovery_attempts = 0,
1916
1935
  workflow_deadline_epoch_ms = NULL, deduplication_id = NULL,
1917
- started_at_epoch_ms = NULL,
1936
+ started_at_epoch_ms = NULL, suspend_key = NULL,
1918
1937
  updated_at = (EXTRACT(EPOCH FROM now()) * 1000)::bigint,
1919
1938
  completed_at = NULL
1920
1939
  WHERE ${this.wfIn(undefined, workflowIDs, updateParams)}
@@ -2175,21 +2194,23 @@ class SystemDatabase {
2175
2194
  AND (consumed_by_function_id >= $1 OR consumed = false)`, messageParams);
2176
2195
  const outputParams = [];
2177
2196
  await client.query(`DELETE FROM "${schema}".workflow_output WHERE ${this.wf(undefined, workflowID, outputParams)}`, outputParams);
2178
- // Re-enqueue the workflow. Re-asserting the status we read keeps a workflow that
2179
- // moved on underneath us from being resurrected.
2197
+ // Re-enqueue the workflow, keeping its queue and key unless given. Re-asserting the status
2198
+ // we read keeps a workflow that moved on underneath us from being resurrected.
2180
2199
  const params = [
2181
2200
  workflow_1.StatusString.ENQUEUED,
2182
- options.queueName ?? utils_1.INTERNAL_QUEUE_NAME,
2201
+ options.queueName ?? null,
2183
2202
  options.queuePartitionKey ?? null,
2184
2203
  status,
2204
+ utils_1.INTERNAL_QUEUE_NAME,
2185
2205
  ];
2186
2206
  const setVersion = options.applicationVersion !== undefined
2187
2207
  ? `, application_version = $${params.push(options.applicationVersion)}`
2188
2208
  : '';
2189
2209
  const { rowCount } = await client.query(`UPDATE "${schema}".workflow_status
2190
- SET status = $1, owner_xid = NULL, queue_name = $2, queue_partition_key = $3, recovery_attempts = 0,
2210
+ SET status = $1, owner_xid = NULL, queue_name = COALESCE($2::text, queue_name, $5::text),
2211
+ queue_partition_key = COALESCE($3::text, queue_partition_key), recovery_attempts = 0,
2191
2212
  workflow_deadline_epoch_ms = NULL, deduplication_id = NULL, started_at_epoch_ms = NULL,
2192
- completed_at = NULL, output = NULL, error = NULL,
2213
+ suspend_key = NULL, completed_at = NULL, output = NULL, error = NULL,
2193
2214
  updated_at = (EXTRACT(EPOCH FROM now()) * 1000)::bigint${setVersion}
2194
2215
  WHERE ${this.wf(undefined, workflowID, params)} AND status = $4`, params);
2195
2216
  if (rowCount !== 1) {
@@ -2225,7 +2246,8 @@ class SystemDatabase {
2225
2246
  const { rows: statusRows } = await client.query(`SELECT ws.workflow_uuid, ws.name, ws.class_name, ws.config_name, ws.application_id,
2226
2247
  ws.authenticated_user, ws.authenticated_roles, ws.assumed_role,
2227
2248
  COALESCE(wi.inputs, ws.inputs) AS inputs, ws.serialization,
2228
- ws.application_version, ws.attributes, ws.application_name
2249
+ ws.application_version, ws.attributes, ws.application_name,
2250
+ ws.queue_name, ws.queue_partition_key, ws.priority
2229
2251
  FROM "${this.schemaName}".workflow_status ws
2230
2252
  LEFT JOIN "${this.schemaName}".workflow_input wi ON ${this.wfJoin('ws', 'wi')}
2231
2253
  WHERE ${this.wfIn('ws', originalWorkflowIDs, statusParams)}`, statusParams);
@@ -2235,7 +2257,6 @@ class SystemDatabase {
2235
2257
  throw new error_1.DBOSNonExistentWorkflowError(`Workflow ${wid} does not exist`);
2236
2258
  }
2237
2259
  }
2238
- const queueName = options.queueName ?? utils_1.INTERNAL_QUEUE_NAME;
2239
2260
  // Bulk insert all forked workflow status rows.
2240
2261
  const insertCols = [
2241
2262
  'workflow_uuid',
@@ -2255,6 +2276,7 @@ class SystemDatabase {
2255
2276
  'serialization',
2256
2277
  'attributes',
2257
2278
  'application_name',
2279
+ 'priority',
2258
2280
  ];
2259
2281
  if (options.timeoutMS !== undefined) {
2260
2282
  insertCols.push('workflow_timeout_ms');
@@ -2275,7 +2297,8 @@ class SystemDatabase {
2275
2297
  ws.name,
2276
2298
  ws.class_name ?? null,
2277
2299
  ws.config_name ?? null,
2278
- queueName,
2300
+ // A fork inherits its original's queue, key, and priority, except what is passed.
2301
+ options.queueName ?? ws.queue_name ?? utils_1.INTERNAL_QUEUE_NAME,
2279
2302
  ws.authenticated_user,
2280
2303
  ws.assumed_role,
2281
2304
  ws.authenticated_roles,
@@ -2283,11 +2306,12 @@ class SystemDatabase {
2283
2306
  ws.application_id,
2284
2307
  // Legacy column: the payload lives in workflow_input.
2285
2308
  null,
2286
- options.queuePartitionKey ?? null,
2309
+ options.queuePartitionKey ?? ws.queue_partition_key ?? null,
2287
2310
  origID,
2288
2311
  ws.serialization,
2289
2312
  ws.attributes ? JSON.stringify(ws.attributes) : null,
2290
2313
  forkOwners.get(forkID) ?? null,
2314
+ ws.priority ?? 0,
2291
2315
  ...(options.timeoutMS !== undefined ? [options.timeoutMS] : []),
2292
2316
  ];
2293
2317
  }));
@@ -2839,9 +2863,11 @@ class SystemDatabase {
2839
2863
  */
2840
2864
  async wakeReceivers(db, destinations, topics) {
2841
2865
  const params = [topics];
2842
- await db.query(`UPDATE "${this.schemaName}".workflow_status
2843
- SET delay_until_epoch_ms = 0
2844
- WHERE ${this.wfIn(undefined, destinations, params)} AND status = '${workflow_1.StatusString.SUSPENDED}' AND suspend_key = ANY($1::text[])`, params);
2866
+ await db.query(
2867
+ // Status is tested in SET, not WHERE, so the planner cannot scan idx_workflow_status_suspended.
2868
+ `UPDATE "${this.schemaName}".workflow_status
2869
+ SET delay_until_epoch_ms = CASE WHEN status = '${workflow_1.StatusString.SUSPENDED}' THEN 0 ELSE delay_until_epoch_ms END
2870
+ WHERE ${this.wfIn(undefined, destinations, params)} AND suspend_key = ANY($1::text[])`, params);
2845
2871
  }
2846
2872
  async recv(workflowID, functionID, timeoutFunctionID, topic, timeoutSeconds = dbos_executor_1.DBOSExecutor.defaultNotificationTimeoutSec, pollingIntervalMs) {
2847
2873
  topic = topic ?? this.nullTopic;
@@ -3281,7 +3307,8 @@ class SystemDatabase {
3281
3307
  // Literal statuses let the planner match idx_workflow_status_deadline_v2 under any plan mode.
3282
3308
  // SKIP LOCKED leaves a row a dequeue or a peer's sweep holds for the next sweep.
3283
3309
  const { rows } = await this.pool.query(`UPDATE "${this.schemaName}".workflow_status
3284
- SET status = $1, owner_xid = NULL, queue_name = NULL, deduplication_id = NULL, started_at_epoch_ms = NULL,
3310
+ SET status = $1, owner_xid = NULL, deduplication_id = NULL, started_at_epoch_ms = NULL,
3311
+ suspend_key = NULL,
3285
3312
  updated_at = (EXTRACT(EPOCH FROM now()) * 1000)::bigint,
3286
3313
  completed_at = (EXTRACT(EPOCH FROM now()) * 1000)::bigint
3287
3314
  WHERE workflow_uuid IN (
@@ -4197,11 +4224,13 @@ class SystemDatabase {
4197
4224
  * Promote a version to latest. Promoting a peer's is a collision, not a retiming;
4198
4225
  * promotion claims an unclaimed row, which would otherwise be every peer's latest.
4199
4226
  */
4200
- async updateApplicationVersionTimestamp(versionName, newTimestamp, applicationName) {
4227
+ async updateApplicationVersionTimestamp(versionName, newTimestamp, applicationName, txnClient) {
4201
4228
  const owner = applicationName ?? this.appName;
4202
- const client = await this.connect();
4229
+ // A caller-supplied client is already in a transaction, which owns BEGIN and COMMIT.
4230
+ const client = txnClient ?? (await this.connect());
4203
4231
  try {
4204
- await client.query('BEGIN');
4232
+ if (!txnClient)
4233
+ await client.query('BEGIN');
4205
4234
  const resolved = await this.#resolveRowOwner(client, 'application_versions', 'version_name', versionName, owner, 'Application version');
4206
4235
  // Scoped to the row this writer resolved to: once version_name is no longer globally unique, a bare name match would retime every peer's version.
4207
4236
  const scope = resolved === undefined ? 'application_name IS NULL' : '(application_name = $3 OR application_name IS NULL)';
@@ -4211,14 +4240,17 @@ class SystemDatabase {
4211
4240
  await client.query(`UPDATE "${this.schemaName}".application_versions
4212
4241
  SET version_timestamp = $1, application_name = ${resolved === undefined ? 'application_name' : '$3'}
4213
4242
  WHERE ${this.global(undefined, params)} AND version_name = $2 AND ${scope}`, params);
4214
- await client.query('COMMIT');
4243
+ if (!txnClient)
4244
+ await client.query('COMMIT');
4215
4245
  }
4216
4246
  catch (e) {
4217
- await client.query('ROLLBACK');
4247
+ if (!txnClient)
4248
+ await client.query('ROLLBACK');
4218
4249
  throw e;
4219
4250
  }
4220
4251
  finally {
4221
- client.release();
4252
+ if (!txnClient)
4253
+ client.release();
4222
4254
  }
4223
4255
  }
4224
4256
  async listApplicationVersions() {
@@ -4982,7 +5014,7 @@ __decorate([
4982
5014
  __decorate([
4983
5015
  dbRetry(),
4984
5016
  __metadata("design:type", Function),
4985
- __metadata("design:paramtypes", [Array, String]),
5017
+ __metadata("design:paramtypes", [Array, String, String]),
4986
5018
  __metadata("design:returntype", Promise)
4987
5019
  ], SystemDatabase.prototype, "resumeWorkflows", null);
4988
5020
  __decorate([