velocious 1.0.729 → 1.0.731

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 (47) hide show
  1. package/README.md +1 -1
  2. package/build/background-jobs/adapter.js +13 -1
  3. package/build/background-jobs/local-store.js +10 -16
  4. package/build/background-jobs/main.js +65 -2
  5. package/build/background-jobs/queue-priority-order.js +96 -0
  6. package/build/background-jobs/store.js +138 -29
  7. package/build/background-jobs/types.js +10 -0
  8. package/build/configuration-types.js +9 -0
  9. package/build/configuration.js +7 -1
  10. package/build/database/drivers/mysql/index.js +116 -15
  11. package/build/src/background-jobs/adapter.d.ts +17 -1
  12. package/build/src/background-jobs/adapter.d.ts.map +1 -1
  13. package/build/src/background-jobs/adapter.js +13 -2
  14. package/build/src/background-jobs/local-store.d.ts +5 -2
  15. package/build/src/background-jobs/local-store.d.ts.map +1 -1
  16. package/build/src/background-jobs/local-store.js +11 -15
  17. package/build/src/background-jobs/main.d.ts +16 -0
  18. package/build/src/background-jobs/main.d.ts.map +1 -1
  19. package/build/src/background-jobs/main.js +58 -3
  20. package/build/src/background-jobs/queue-priority-order.d.ts +55 -0
  21. package/build/src/background-jobs/queue-priority-order.d.ts.map +1 -0
  22. package/build/src/background-jobs/queue-priority-order.js +89 -0
  23. package/build/src/background-jobs/store.d.ts +71 -14
  24. package/build/src/background-jobs/store.d.ts.map +1 -1
  25. package/build/src/background-jobs/store.js +129 -34
  26. package/build/src/background-jobs/types.d.ts +32 -0
  27. package/build/src/background-jobs/types.d.ts.map +1 -1
  28. package/build/src/background-jobs/types.js +11 -1
  29. package/build/src/configuration-types.d.ts +21 -0
  30. package/build/src/configuration-types.d.ts.map +1 -1
  31. package/build/src/configuration-types.js +10 -1
  32. package/build/src/configuration.d.ts.map +1 -1
  33. package/build/src/configuration.js +8 -2
  34. package/build/src/database/drivers/mysql/index.d.ts +18 -0
  35. package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
  36. package/build/src/database/drivers/mysql/index.js +102 -14
  37. package/package.json +3 -2
  38. package/scripts/verify-dependency-refs.js +14 -0
  39. package/src/background-jobs/adapter.js +13 -1
  40. package/src/background-jobs/local-store.js +10 -16
  41. package/src/background-jobs/main.js +65 -2
  42. package/src/background-jobs/queue-priority-order.js +96 -0
  43. package/src/background-jobs/store.js +138 -29
  44. package/src/background-jobs/types.js +10 -0
  45. package/src/configuration-types.js +9 -0
  46. package/src/configuration.js +7 -1
  47. package/src/database/drivers/mysql/index.js +116 -15
@@ -65,6 +65,9 @@ export default class VelociousDatabaseDriversMysql extends Base{
65
65
  /** @type {import("mysql").Pool | undefined} */
66
66
  pool = undefined
67
67
 
68
+ /** @type {Map<string, import("../base.js").default>} */
69
+ _advisoryLockConnections = new Map()
70
+
68
71
  /** @type {string | null} */
69
72
  _desiredSessionTimeZone = "+00:00"
70
73
 
@@ -94,6 +97,20 @@ export default class VelociousDatabaseDriversMysql extends Base{
94
97
  * @returns {Promise<void>} - Resolves when complete.
95
98
  */
96
99
  async _close() {
100
+ for (const [lockName, connection] of this._advisoryLockConnections) {
101
+ this._advisoryLockConnections.delete(lockName)
102
+
103
+ if (connection === this) continue
104
+
105
+ try {
106
+ this.configuration.unregisterAdvisoryLockConnection(connection)
107
+ } catch {
108
+ // The configuration may already be closing its registered connections.
109
+ }
110
+
111
+ await connection.close()
112
+ }
113
+
97
114
  const pool = this.pool
98
115
 
99
116
  if (!pool) return
@@ -931,14 +948,25 @@ export default class VelociousDatabaseDriversMysql extends Base{
931
948
  const timeoutSeconds = typeof timeoutMs === "number" && timeoutMs >= 0
932
949
  ? Math.ceil(timeoutMs / 1000)
933
950
  : MYSQL_INDEFINITE_LOCK_TIMEOUT_SECONDS
934
- const rows = await this.query(`SELECT GET_LOCK(${this.quote(name)}, ${timeoutSeconds}) AS velocious_advisory_lock_result`)
935
- const result = rows?.[0]?.velocious_advisory_lock_result
951
+ const connection = await this.advisoryLockConnection(name)
936
952
 
937
- if (result === null || result === undefined) {
938
- throw new Error(`GET_LOCK returned NULL for advisory lock ${JSON.stringify(name)} (typically an out-of-memory or thread-killed condition)`)
939
- }
953
+ try {
954
+ const rows = await connection.query(`SELECT GET_LOCK(${this.quote(name)}, ${timeoutSeconds}) AS velocious_advisory_lock_result`)
955
+ const result = rows?.[0]?.velocious_advisory_lock_result
956
+
957
+ if (result === null || result === undefined) {
958
+ throw new Error(`GET_LOCK returned NULL for advisory lock ${JSON.stringify(name)} (typically an out-of-memory or thread-killed condition)`)
959
+ }
960
+
961
+ const acquired = Number(result) === 1
962
+
963
+ if (!acquired) await this.closeAdvisoryLockConnection(name, connection)
940
964
 
941
- return Number(result) === 1
965
+ return acquired
966
+ } catch (error) {
967
+ await this.closeAdvisoryLockConnection(name, connection)
968
+ throw error
969
+ }
942
970
  }
943
971
 
944
972
  /**
@@ -947,14 +975,25 @@ export default class VelociousDatabaseDriversMysql extends Base{
947
975
  * @returns {Promise<boolean>} - True if the lock was acquired, false if it was already held.
948
976
  */
949
977
  async _tryAcquireAdvisoryLock(name) {
950
- const rows = await this.query(`SELECT GET_LOCK(${this.quote(name)}, 0) AS velocious_advisory_lock_result`)
951
- const result = rows?.[0]?.velocious_advisory_lock_result
978
+ const connection = await this.advisoryLockConnection(name)
952
979
 
953
- if (result === null || result === undefined) {
954
- throw new Error(`GET_LOCK returned NULL for advisory lock ${JSON.stringify(name)} (typically an out-of-memory or thread-killed condition)`)
955
- }
980
+ try {
981
+ const rows = await connection.query(`SELECT GET_LOCK(${this.quote(name)}, 0) AS velocious_advisory_lock_result`)
982
+ const result = rows?.[0]?.velocious_advisory_lock_result
983
+
984
+ if (result === null || result === undefined) {
985
+ throw new Error(`GET_LOCK returned NULL for advisory lock ${JSON.stringify(name)} (typically an out-of-memory or thread-killed condition)`)
986
+ }
956
987
 
957
- return Number(result) === 1
988
+ const acquired = Number(result) === 1
989
+
990
+ if (!acquired) await this.closeAdvisoryLockConnection(name, connection)
991
+
992
+ return acquired
993
+ } catch (error) {
994
+ await this.closeAdvisoryLockConnection(name, connection)
995
+ throw error
996
+ }
958
997
  }
959
998
 
960
999
  /**
@@ -963,10 +1002,72 @@ export default class VelociousDatabaseDriversMysql extends Base{
963
1002
  * @returns {Promise<boolean>} - True if the lock was held by this session and has now been released.
964
1003
  */
965
1004
  async _releaseAdvisoryLock(name) {
966
- const rows = await this.query(`SELECT RELEASE_LOCK(${this.quote(name)}) AS velocious_advisory_lock_result`, {retry: false})
967
- const result = rows?.[0]?.velocious_advisory_lock_result
1005
+ const connection = this._advisoryLockConnections.get(name)
1006
+
1007
+ if (!connection) return false
1008
+
1009
+ try {
1010
+ const rows = await connection.query(`SELECT RELEASE_LOCK(${this.quote(name)}) AS velocious_advisory_lock_result`, {retry: false})
1011
+ const result = rows?.[0]?.velocious_advisory_lock_result
1012
+
1013
+ // Base#releaseAdvisoryLock removes one re-entrant count after this hook.
1014
+ // Keep the session for another counted release, but close it after the
1015
+ // final release so an idle pool connection cannot retain the lock.
1016
+ if ((this._heldAdvisoryLocks.get(name) || 0) <= 1) await this.closeAdvisoryLockConnection(name, connection)
1017
+
1018
+ return Number(result) === 1
1019
+ } catch (error) {
1020
+ await this.closeAdvisoryLockConnection(name, connection)
1021
+ throw error
1022
+ }
1023
+ }
1024
+
1025
+ /**
1026
+ * Opens one physical session for the named advisory lock and keeps it pinned
1027
+ * until the matching release. The model driver's ordinary queries continue
1028
+ * using its own pool, while GET_LOCK and RELEASE_LOCK are guaranteed to run
1029
+ * on the same MariaDB session.
1030
+ * @param {string} name - Lock name.
1031
+ * @returns {Promise<import("../base.js").default>} - Pinned lock connection.
1032
+ */
1033
+ async advisoryLockConnection(name) {
1034
+ const existing = this._advisoryLockConnections.get(name)
1035
+
1036
+ if (existing) return existing
1037
+
1038
+ // Lightweight driver doubles used by SQL-generation specs do not have a
1039
+ // configuration-owned pool. They already provide a stable query session.
1040
+ if (typeof this.configuration?.getDatabasePool !== "function") {
1041
+ this._advisoryLockConnections.set(name, this)
1042
+
1043
+ return this
1044
+ }
1045
+
1046
+ const connection = await this.configuration.getDatabasePool(this._databaseIdentifier || "default").spawnConnection()
1047
+
1048
+ this._advisoryLockConnections.set(name, connection)
1049
+ this.configuration.registerAdvisoryLockConnection(connection)
1050
+
1051
+ return connection
1052
+ }
1053
+
1054
+ /**
1055
+ * Closes the physical session owned by one advisory lock.
1056
+ * @param {string} name - Lock name.
1057
+ * @param {import("../base.js").default} connection - Pinned lock connection.
1058
+ * @returns {Promise<void>} - Resolves after unregistering and closing it.
1059
+ */
1060
+ async closeAdvisoryLockConnection(name, connection) {
1061
+ if (connection === this) {
1062
+ this._advisoryLockConnections.delete(name)
1063
+
1064
+ return
1065
+ }
1066
+ if (this._advisoryLockConnections.get(name) !== connection) return
968
1067
 
969
- return Number(result) === 1
1068
+ this._advisoryLockConnections.delete(name)
1069
+ this.configuration.unregisterAdvisoryLockConnection(connection)
1070
+ await connection.close()
970
1071
  }
971
1072
 
972
1073
  /**