velocious 1.0.726 → 1.0.728
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.
- package/README.md +2 -2
- package/bin/velocious.js +2 -19
- package/build/application.js +32 -3
- package/build/background-jobs/main.js +9 -1
- package/build/background-jobs/worker.js +69 -6
- package/build/bin/velocious.js +2 -19
- package/build/cli/process-args.js +34 -0
- package/build/configuration-types.js +1 -1
- package/build/configuration.js +43 -2
- package/build/database/advisory-lock-lock-server-backend.js +30 -24
- package/build/database/advisory-lock-lock-server-embedded-client.js +74 -0
- package/build/database/advisory-lock-lock-server-engine.js +544 -0
- package/build/database/advisory-lock-lock-server-local-client.js +260 -0
- package/build/database/advisory-lock-lock-server-socket-path.js +17 -0
- package/build/database/advisory-lock-lock-server-transport.js +30 -0
- package/build/database/drivers/base.js +34 -12
- package/build/database/drivers/mssql/index.js +17 -18
- package/build/database/drivers/mssql/table.js +28 -25
- package/build/database/drivers/mysql/index.js +15 -18
- package/build/database/drivers/mysql/table.js +31 -23
- package/build/database/drivers/pgsql/index.js +15 -18
- package/build/database/drivers/pgsql/table.js +28 -26
- package/build/database/drivers/sqlite/base.js +7 -9
- package/build/database/drivers/sqlite/table.js +32 -26
- package/build/database/pool/async-tracked-multi-connection.js +1 -0
- package/build/database/pool/base.js +14 -4
- package/build/database/pool/single-multi-use.js +1 -0
- package/build/database/record/auditing.js +89 -11
- package/build/database/record/index.js +1 -1
- package/build/environment-handlers/base.js +50 -9
- package/build/environment-handlers/node/advisory-lock-lock-server-local-server.js +206 -0
- package/build/environment-handlers/node/cli/commands/server.js +1 -0
- package/build/environment-handlers/node.js +30 -3
- package/build/http-server/server-lock.js +55 -18
- package/build/src/application.d.ts +7 -4
- package/build/src/application.d.ts.map +1 -1
- package/build/src/application.js +27 -4
- package/build/src/background-jobs/main.d.ts +2 -1
- package/build/src/background-jobs/main.d.ts.map +1 -1
- package/build/src/background-jobs/main.js +10 -2
- package/build/src/background-jobs/worker.d.ts +42 -0
- package/build/src/background-jobs/worker.d.ts.map +1 -1
- package/build/src/background-jobs/worker.js +61 -5
- package/build/src/cli/process-args.d.ts +11 -0
- package/build/src/cli/process-args.d.ts.map +1 -0
- package/build/src/cli/process-args.js +34 -0
- package/build/src/configuration-types.d.ts +3 -1
- package/build/src/configuration-types.d.ts.map +1 -1
- package/build/src/configuration-types.js +2 -2
- package/build/src/configuration.d.ts +16 -1
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +39 -3
- package/build/src/database/advisory-lock-lock-server-backend.d.ts +10 -3
- package/build/src/database/advisory-lock-lock-server-backend.d.ts.map +1 -1
- package/build/src/database/advisory-lock-lock-server-backend.js +25 -22
- package/build/src/database/advisory-lock-lock-server-embedded-client.d.ts +24 -0
- package/build/src/database/advisory-lock-lock-server-embedded-client.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-embedded-client.js +64 -0
- package/build/src/database/advisory-lock-lock-server-engine.d.ts +285 -0
- package/build/src/database/advisory-lock-lock-server-engine.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-engine.js +460 -0
- package/build/src/database/advisory-lock-lock-server-local-client.d.ts +114 -0
- package/build/src/database/advisory-lock-lock-server-local-client.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-local-client.js +227 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.d.ts +9 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.js +16 -0
- package/build/src/database/advisory-lock-lock-server-transport.d.ts +18 -0
- package/build/src/database/advisory-lock-lock-server-transport.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-transport.js +27 -0
- package/build/src/database/drivers/base.d.ts +15 -2
- package/build/src/database/drivers/base.d.ts.map +1 -1
- package/build/src/database/drivers/base.js +33 -13
- package/build/src/database/drivers/mssql/index.d.ts +2 -2
- package/build/src/database/drivers/mssql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mssql/index.js +17 -18
- package/build/src/database/drivers/mssql/table.d.ts.map +1 -1
- package/build/src/database/drivers/mssql/table.js +25 -23
- package/build/src/database/drivers/mysql/index.d.ts +2 -2
- package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/index.js +16 -19
- package/build/src/database/drivers/mysql/table.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/table.js +27 -22
- package/build/src/database/drivers/pgsql/index.d.ts +2 -2
- package/build/src/database/drivers/pgsql/index.d.ts.map +1 -1
- package/build/src/database/drivers/pgsql/index.js +16 -19
- package/build/src/database/drivers/pgsql/table.d.ts.map +1 -1
- package/build/src/database/drivers/pgsql/table.js +25 -23
- package/build/src/database/drivers/sqlite/base.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/base.js +8 -10
- package/build/src/database/drivers/sqlite/table.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/table.js +29 -25
- package/build/src/database/pool/async-tracked-multi-connection.d.ts.map +1 -1
- package/build/src/database/pool/async-tracked-multi-connection.js +2 -1
- package/build/src/database/pool/base.d.ts +6 -0
- package/build/src/database/pool/base.d.ts.map +1 -1
- package/build/src/database/pool/base.js +14 -5
- package/build/src/database/pool/single-multi-use.d.ts.map +1 -1
- package/build/src/database/pool/single-multi-use.js +2 -1
- package/build/src/database/record/auditing.d.ts +14 -4
- package/build/src/database/record/auditing.d.ts.map +1 -1
- package/build/src/database/record/auditing.js +77 -12
- package/build/src/database/record/index.d.ts +4 -2
- package/build/src/database/record/index.d.ts.map +1 -1
- package/build/src/database/record/index.js +2 -2
- package/build/src/environment-handlers/base.d.ts +86 -7
- package/build/src/environment-handlers/base.d.ts.map +1 -1
- package/build/src/environment-handlers/base.js +48 -9
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.d.ts +2 -0
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.d.ts.map +1 -0
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.js +179 -0
- package/build/src/environment-handlers/node/cli/commands/server.d.ts.map +1 -1
- package/build/src/environment-handlers/node/cli/commands/server.js +2 -1
- package/build/src/environment-handlers/node.d.ts +10 -4
- package/build/src/environment-handlers/node.d.ts.map +1 -1
- package/build/src/environment-handlers/node.js +30 -4
- package/build/src/http-server/server-lock.d.ts +43 -14
- package/build/src/http-server/server-lock.d.ts.map +1 -1
- package/build/src/http-server/server-lock.js +54 -19
- package/build/src/tenants/default-tenant-database-provisioning.d.ts.map +1 -1
- package/build/src/tenants/default-tenant-database-provisioning.js +7 -1
- package/build/tenants/default-tenant-database-provisioning.js +8 -0
- package/package.json +2 -2
- package/src/application.js +32 -3
- package/src/background-jobs/main.js +9 -1
- package/src/background-jobs/worker.js +69 -6
- package/src/cli/process-args.js +34 -0
- package/src/configuration-types.js +1 -1
- package/src/configuration.js +43 -2
- package/src/database/advisory-lock-lock-server-backend.js +30 -24
- package/src/database/advisory-lock-lock-server-embedded-client.js +74 -0
- package/src/database/advisory-lock-lock-server-engine.js +544 -0
- package/src/database/advisory-lock-lock-server-local-client.js +260 -0
- package/src/database/advisory-lock-lock-server-socket-path.js +17 -0
- package/src/database/advisory-lock-lock-server-transport.js +30 -0
- package/src/database/drivers/base.js +34 -12
- package/src/database/drivers/mssql/index.js +17 -18
- package/src/database/drivers/mssql/table.js +28 -25
- package/src/database/drivers/mysql/index.js +15 -18
- package/src/database/drivers/mysql/table.js +31 -23
- package/src/database/drivers/pgsql/index.js +15 -18
- package/src/database/drivers/pgsql/table.js +28 -26
- package/src/database/drivers/sqlite/base.js +7 -9
- package/src/database/drivers/sqlite/table.js +32 -26
- package/src/database/pool/async-tracked-multi-connection.js +1 -0
- package/src/database/pool/base.js +14 -4
- package/src/database/pool/single-multi-use.js +1 -0
- package/src/database/record/auditing.js +89 -11
- package/src/database/record/index.js +1 -1
- package/src/environment-handlers/base.js +50 -9
- package/src/environment-handlers/node/advisory-lock-lock-server-local-server.js +206 -0
- package/src/environment-handlers/node/cli/commands/server.js +1 -0
- package/src/environment-handlers/node.js +30 -3
- package/src/http-server/server-lock.js +55 -18
- package/src/tenants/default-tenant-database-provisioning.js +8 -0
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
+
import AdvisoryLockLockServerBackend from "../database/advisory-lock-lock-server-backend.js"
|
|
4
|
+
import AdvisoryLockLockServerEngine from "../database/advisory-lock-lock-server-engine.js"
|
|
3
5
|
import BackgroundJobsAdapterClient from "../background-jobs/adapter-client.js"
|
|
4
|
-
import {
|
|
6
|
+
import {createEmbeddedLockServerClientFactory} from "../database/advisory-lock-lock-server-embedded-client.js"
|
|
7
|
+
import {validateTimeZone} from "../time-zone.js"
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
10
|
* CommandFileObjectType type.
|
|
@@ -24,6 +27,20 @@ import { validateTimeZone } from "../time-zone.js"
|
|
|
24
27
|
* @property {() => ({revoked: boolean} | undefined)} getStore - Gets the inherited scope.
|
|
25
28
|
* @property {(scope: {revoked: boolean} | undefined, callback: () => ReturnType<typeof JSON.parse>) => ReturnType<typeof JSON.parse>} run - Runs in the scope.
|
|
26
29
|
*/
|
|
30
|
+
/**
|
|
31
|
+
* Lock-server advisory-lock backend options as resolved by `Configuration`
|
|
32
|
+
* (STC-715). `mode` selects the transport the handler must provide.
|
|
33
|
+
* @typedef {object} AdvisoryLockLockServerBackendOptions
|
|
34
|
+
* @property {string} [authToken] - Shared secret; the server fails closed without a matching token.
|
|
35
|
+
* @property {(args: {authToken: string, url: string}) => import("../database/advisory-lock-lock-server-backend.js").LockServerClientLike} [clientFactory] - Transport override (specs inject a fake).
|
|
36
|
+
* @property {number} [connectTimeoutMs] - Handshake timeout.
|
|
37
|
+
* @property {number} [leaseMs] - Lease duration per grant.
|
|
38
|
+
* @property {"embedded" | "external" | "local"} [mode] - Lock-server mode. Defaults to `external`.
|
|
39
|
+
* @property {(error: Error) => void} [onError] - Async error reporter.
|
|
40
|
+
* @property {number} [reprobeCooldownMs] - How long an unavailable server is not re-probed.
|
|
41
|
+
* @property {string} [socketPath] - Unix socket path for `local` mode (defaults to a short path under os.tmpdir()).
|
|
42
|
+
* @property {string} url - Lock-server endpoint (websocket URL for external mode; informational for embedded/local modes).
|
|
43
|
+
*/
|
|
27
44
|
|
|
28
45
|
export class TestDatabaseAccessRevokedError extends Error {}
|
|
29
46
|
|
|
@@ -50,14 +67,38 @@ export default class VelociousEnvironmentHandlerBase {
|
|
|
50
67
|
}
|
|
51
68
|
|
|
52
69
|
/**
|
|
53
|
-
* Creates the lock-server advisory-lock backend
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
* Creates the lock-server advisory-lock backend (STC-715 modes).
|
|
71
|
+
*
|
|
72
|
+
* `embedded` mode runs the shared lock-server engine inside this process
|
|
73
|
+
* over plain function calls — no sockets, no `ws`, no Node builtins — so
|
|
74
|
+
* browser and Expo environments can serve the tier too. Coordination stays
|
|
75
|
+
* inside this JS realm: it does not cross tabs, workers or processes unless
|
|
76
|
+
* an explicit transport (`external`/`local`) is configured.
|
|
77
|
+
*
|
|
78
|
+
* `external` and `local` modes need a transport only the Node handler can
|
|
79
|
+
* provide; other environments refuse loudly instead of half-working.
|
|
80
|
+
* @param {object} args - Backend options.
|
|
81
|
+
* @param {string} [args.authToken] - Shared secret.
|
|
82
|
+
* @param {number} [args.connectTimeoutMs] - Handshake timeout.
|
|
83
|
+
* @param {number} [args.leaseMs] - Lease duration.
|
|
84
|
+
* @param {"embedded" | "external" | "local"} [args.mode] - Lock-server mode. Defaults to `external`.
|
|
85
|
+
* @param {(error: Error) => void} [args.onError] - Async error reporter.
|
|
86
|
+
* @param {number} [args.reprobeCooldownMs] - Unavailability cache window.
|
|
87
|
+
* @param {string} [args.socketPath] - Unix socket path for `local` mode.
|
|
88
|
+
* @param {string} args.url - Lock-server endpoint.
|
|
89
|
+
* @returns {Promise<import("../database/advisory-lock-lock-server-backend.js").default>} - Resolves with the created backend in capable handlers.
|
|
90
|
+
*/
|
|
91
|
+
async createAdvisoryLockLockServerBackend(args) {
|
|
92
|
+
if (args.mode === "embedded") {
|
|
93
|
+
const engine = new AdvisoryLockLockServerEngine({leaseMs: args.leaseMs})
|
|
94
|
+
|
|
95
|
+
return new AdvisoryLockLockServerBackend({
|
|
96
|
+
...args,
|
|
97
|
+
clientFactory: createEmbeddedLockServerClientFactory(engine)
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
throw new Error(`The advisory-lock lock-server "${String(args.mode ?? "external")}" mode requires a Node environment handler`)
|
|
61
102
|
}
|
|
62
103
|
|
|
63
104
|
/**
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import AdvisoryLockLockServerEngine from "../../database/advisory-lock-lock-server-engine.js"
|
|
4
|
+
import {LOCK_SERVER_PROTOCOL_VERSION} from "../../database/advisory-lock-lock-server-backend.js"
|
|
5
|
+
import {defaultLockServerSocketPath} from "../../database/advisory-lock-lock-server-socket-path.js"
|
|
6
|
+
import fs from "node:fs"
|
|
7
|
+
import net from "node:net"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Local lock-server engine server for the `local` mode (STC-715): the shared
|
|
11
|
+
* `AdvisoryLockLockServerEngine` exposed over a Unix domain socket, one JSON
|
|
12
|
+
* line per protocol message tagged with the client's `connectionId`. No
|
|
13
|
+
* listening network port, no `ws`.
|
|
14
|
+
*
|
|
15
|
+
* Lifecycle follows the environment: `AdvisoryLockLockServerLocalClient`
|
|
16
|
+
* spawns this server as a child when no local engine is listening yet, and the
|
|
17
|
+
* server idles out (and removes its socket) once its last client connection
|
|
18
|
+
* has been gone for `VELOCIOUS_LOCK_SERVER_IDLE_SHUTDOWN_MS` — so a host never
|
|
19
|
+
* accumulates orphan engines. SIGTERM/SIGINT shut it down cleanly.
|
|
20
|
+
*
|
|
21
|
+
* Startup is race-safe: if another live engine already owns the socket path,
|
|
22
|
+
* this candidate exits quietly; a stale socket file from a crashed engine is
|
|
23
|
+
* removed before listening.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const socketPath = defaultLockServerSocketPath(process.env.VELOCIOUS_LOCK_SERVER_SOCKET_PATH)
|
|
27
|
+
const leaseMsRaw = Number(process.env.CONSENS_LOCK_LEASE_MS)
|
|
28
|
+
const idleShutdownMsRaw = Number(process.env.VELOCIOUS_LOCK_SERVER_IDLE_SHUTDOWN_MS)
|
|
29
|
+
const idleShutdownMs = Number.isFinite(idleShutdownMsRaw) && idleShutdownMsRaw >= 0 ? idleShutdownMsRaw : 30_000
|
|
30
|
+
|
|
31
|
+
const engine = new AdvisoryLockLockServerEngine({
|
|
32
|
+
leaseMs: Number.isFinite(leaseMsRaw) && leaseMsRaw > 0 ? leaseMsRaw : 30_000
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
engine.startSweeper()
|
|
36
|
+
|
|
37
|
+
let closing = false
|
|
38
|
+
let connectionCount = 0
|
|
39
|
+
/** @type {ReturnType<typeof setTimeout> | null} */
|
|
40
|
+
let idleTimer = null
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Answers a startup race: is another live engine already listening on the
|
|
44
|
+
* socket path?
|
|
45
|
+
* @returns {Promise<boolean>} - True when a live engine accepted a probe connection.
|
|
46
|
+
*/
|
|
47
|
+
function probeExistingEngine() {
|
|
48
|
+
return new Promise((resolve) => {
|
|
49
|
+
const probe = net.connect(socketPath)
|
|
50
|
+
|
|
51
|
+
probe.once("connect", () => {
|
|
52
|
+
probe.destroy()
|
|
53
|
+
resolve(true)
|
|
54
|
+
})
|
|
55
|
+
probe.once("error", () => {
|
|
56
|
+
probe.destroy()
|
|
57
|
+
resolve(false)
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Cancels a pending idle-shutdown countdown (a client is back).
|
|
64
|
+
* @returns {void}
|
|
65
|
+
*/
|
|
66
|
+
function cancelIdleShutdown() {
|
|
67
|
+
if (!idleTimer) return
|
|
68
|
+
|
|
69
|
+
clearTimeout(idleTimer)
|
|
70
|
+
idleTimer = null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Starts the idle-shutdown countdown: with zero live connections the engine
|
|
75
|
+
* stops itself and removes the socket, so spawned engines follow their
|
|
76
|
+
* environment instead of outliving it.
|
|
77
|
+
* @returns {void}
|
|
78
|
+
*/
|
|
79
|
+
function scheduleIdleShutdown() {
|
|
80
|
+
if (closing || connectionCount > 0) return
|
|
81
|
+
|
|
82
|
+
cancelIdleShutdown()
|
|
83
|
+
idleTimer = setTimeout(() => shutdown(), idleShutdownMs)
|
|
84
|
+
idleTimer.unref?.()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Clean shutdown: close the engine, stop listening, remove the socket file.
|
|
89
|
+
* @returns {void}
|
|
90
|
+
*/
|
|
91
|
+
function shutdown() {
|
|
92
|
+
if (closing) return
|
|
93
|
+
|
|
94
|
+
closing = true
|
|
95
|
+
cancelIdleShutdown()
|
|
96
|
+
engine.close()
|
|
97
|
+
server.close(() => {
|
|
98
|
+
try {
|
|
99
|
+
fs.unlinkSync(socketPath)
|
|
100
|
+
} catch {
|
|
101
|
+
// The socket file may already be gone; nothing left to clean.
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
process.exit(0)
|
|
105
|
+
})
|
|
106
|
+
setTimeout(() => process.exit(0), 2000).unref?.()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Wires one client socket into the engine's line protocol.
|
|
111
|
+
* @param {net.Socket} socket - Client connection.
|
|
112
|
+
* @returns {void}
|
|
113
|
+
*/
|
|
114
|
+
function handleClientSocket(socket) {
|
|
115
|
+
connectionCount += 1
|
|
116
|
+
cancelIdleShutdown()
|
|
117
|
+
|
|
118
|
+
socket.setEncoding("utf8")
|
|
119
|
+
|
|
120
|
+
/** @type {Set<string>} */
|
|
121
|
+
const socketConnectionIds = new Set()
|
|
122
|
+
let buffer = ""
|
|
123
|
+
|
|
124
|
+
socket.on("data", (/** @type {string} */ chunk) => {
|
|
125
|
+
buffer += chunk
|
|
126
|
+
|
|
127
|
+
let newlineIndex = buffer.indexOf("\n")
|
|
128
|
+
|
|
129
|
+
while (newlineIndex >= 0) {
|
|
130
|
+
const line = buffer.slice(0, newlineIndex)
|
|
131
|
+
|
|
132
|
+
buffer = buffer.slice(newlineIndex + 1)
|
|
133
|
+
|
|
134
|
+
if (line.trim()) {
|
|
135
|
+
try {
|
|
136
|
+
const message = /** @type {Record<string, unknown>} */ (JSON.parse(line))
|
|
137
|
+
const connectionId = typeof message.connectionId === "string" ? message.connectionId : ""
|
|
138
|
+
|
|
139
|
+
if (connectionId) {
|
|
140
|
+
socketConnectionIds.add(connectionId)
|
|
141
|
+
|
|
142
|
+
if (message.type === "hello") {
|
|
143
|
+
socket.write(`${JSON.stringify({connectionId, leaseMs: engine.leaseMs, protocolVersion: LOCK_SERVER_PROTOCOL_VERSION, type: "hello-ack"})}\n`)
|
|
144
|
+
} else {
|
|
145
|
+
engine.ensureConnection(connectionId, {
|
|
146
|
+
send: (/** @type {Record<string, unknown>} */ body) => {
|
|
147
|
+
if (!socket.destroyed) socket.write(`${JSON.stringify({...body, connectionId})}\n`)
|
|
148
|
+
}
|
|
149
|
+
})
|
|
150
|
+
engine.handleMessage(connectionId, message)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} catch {
|
|
154
|
+
// A malformed line is not a lock request: unanswered requests time
|
|
155
|
+
// out loudly on the client side.
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
newlineIndex = buffer.indexOf("\n")
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
socket.on("close", () => {
|
|
164
|
+
connectionCount -= 1
|
|
165
|
+
|
|
166
|
+
// The transport died: the engine releases every lease this socket held,
|
|
167
|
+
// exactly like the external daemon does on a websocket close.
|
|
168
|
+
for (const connectionId of socketConnectionIds) engine.connectionClose(connectionId)
|
|
169
|
+
|
|
170
|
+
scheduleIdleShutdown()
|
|
171
|
+
})
|
|
172
|
+
socket.on("error", () => socket.destroy())
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const server = net.createServer((socket) => handleClientSocket(socket))
|
|
176
|
+
|
|
177
|
+
server.on("error", async (/** @type {{code?: string}} */ error) => {
|
|
178
|
+
if (error.code === "EADDRINUSE") {
|
|
179
|
+
// Either a live engine owns the path (quiet exit — that engine serves the
|
|
180
|
+
// host) or a crashed one left a stale socket behind (remove and retry).
|
|
181
|
+
if (await probeExistingEngine()) process.exit(0)
|
|
182
|
+
|
|
183
|
+
fs.unlinkSync(socketPath)
|
|
184
|
+
server.listen(socketPath)
|
|
185
|
+
|
|
186
|
+
return
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
process.exit(1)
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
const alreadyLive = await probeExistingEngine()
|
|
193
|
+
|
|
194
|
+
if (alreadyLive) process.exit(0)
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
fs.unlinkSync(socketPath)
|
|
198
|
+
} catch {
|
|
199
|
+
// No stale socket to remove.
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
server.listen(socketPath)
|
|
203
|
+
|
|
204
|
+
for (const signal of ["SIGTERM", "SIGINT"]) {
|
|
205
|
+
process.on(signal, () => shutdown())
|
|
206
|
+
}
|
|
@@ -145,6 +145,7 @@ export default class VelociousCliCommandsServer extends BaseCommand{
|
|
|
145
145
|
const application = new Application({
|
|
146
146
|
configuration,
|
|
147
147
|
httpServer,
|
|
148
|
+
httpServerLockOptOut: parsedProcessArgs["no-server-lock"] === true,
|
|
148
149
|
type: "server"
|
|
149
150
|
})
|
|
150
151
|
const environment = configuration.getEnvironment()
|
|
@@ -4,6 +4,9 @@ import "../database/annotations-async-hooks.js"
|
|
|
4
4
|
import "../database/advisory-lock-reentrancy-node.js"
|
|
5
5
|
import AdvisoryLockFileBackend from "../database/advisory-lock-file-backend.js"
|
|
6
6
|
import AdvisoryLockLockServerBackend from "../database/advisory-lock-lock-server-backend.js"
|
|
7
|
+
import AdvisoryLockLockServerLocalClient from "../database/advisory-lock-lock-server-local-client.js"
|
|
8
|
+
import {defaultLockServerSocketPath} from "../database/advisory-lock-lock-server-socket-path.js"
|
|
9
|
+
import {createLockServerWebsocketTransport} from "../database/advisory-lock-lock-server-transport.js"
|
|
7
10
|
import Base from "./base.js"
|
|
8
11
|
import BackgroundJobsClient from "../background-jobs/client.js"
|
|
9
12
|
import SqlBackgroundJobsAdapter from "../background-jobs/sql-adapter.js"
|
|
@@ -120,12 +123,36 @@ export default class VelociousEnvironmentHandlerNode extends Base{
|
|
|
120
123
|
}
|
|
121
124
|
|
|
122
125
|
/**
|
|
123
|
-
* Creates the lock-server advisory-lock backend
|
|
124
|
-
*
|
|
126
|
+
* Creates the lock-server advisory-lock backend for one of the STC-715 modes:
|
|
127
|
+
*
|
|
128
|
+
* - `external` (default): the Node websocket transport to the shared
|
|
129
|
+
* consens-lock daemon — unchanged behavior for existing configurations.
|
|
130
|
+
* - `local`: spawns the engine as a child of this process over stdio
|
|
131
|
+
* JSON-lines — no listening network port, lifecycle follows the
|
|
132
|
+
* environment, crash detection via the adapter's unavailability cache.
|
|
133
|
+
* - `embedded`: the base handler's in-process engine (no sockets at all).
|
|
134
|
+
* @param {import("./base.js").AdvisoryLockLockServerBackendOptions} args - Backend options.
|
|
125
135
|
* @returns {Promise<import("../database/advisory-lock-lock-server-backend.js").default>} - Created backend.
|
|
126
136
|
*/
|
|
127
137
|
async createAdvisoryLockLockServerBackend(args) {
|
|
128
|
-
|
|
138
|
+
if (args.mode === "local") {
|
|
139
|
+
return new AdvisoryLockLockServerBackend({
|
|
140
|
+
...args,
|
|
141
|
+
clientFactory: ({authToken}) => new AdvisoryLockLockServerLocalClient({
|
|
142
|
+
authToken,
|
|
143
|
+
connectTimeoutMs: args.connectTimeoutMs,
|
|
144
|
+
leaseMs: args.leaseMs,
|
|
145
|
+
socketPath: args.socketPath || defaultLockServerSocketPath()
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (args.mode === "embedded") return await super.createAdvisoryLockLockServerBackend(args)
|
|
151
|
+
|
|
152
|
+
return new AdvisoryLockLockServerBackend({
|
|
153
|
+
...args,
|
|
154
|
+
clientFactory: createLockServerWebsocketTransport
|
|
155
|
+
})
|
|
129
156
|
}
|
|
130
157
|
|
|
131
158
|
/**
|
|
@@ -4,26 +4,45 @@ import AdvisoryLockFileBackend from "../database/advisory-lock-file-backend.js"
|
|
|
4
4
|
import path from "node:path"
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Directory-based lock that allows only one HTTP server process
|
|
8
|
-
* directory. Thin wrapper over the shared
|
|
9
|
-
* (`src/database/advisory-lock-file-backend.js` — the
|
|
10
|
-
* primitive generalized from this lock's original
|
|
11
|
-
* protocol), so the server lock and the advisory-lock
|
|
12
|
-
* acquisition, owner-metadata, lease-renewal, and
|
|
7
|
+
* Directory + environment-based lock that allows only one HTTP server process
|
|
8
|
+
* per (app directory, environment) pair. Thin wrapper over the shared
|
|
9
|
+
* file-lock backend (`src/database/advisory-lock-file-backend.js` — the
|
|
10
|
+
* STC-210 `file` tier primitive generalized from this lock's original
|
|
11
|
+
* hand-rolled directory protocol), so the server lock and the advisory-lock
|
|
12
|
+
* file tier share one acquisition, owner-metadata, lease-renewal, and
|
|
13
|
+
* staleness implementation.
|
|
14
|
+
*
|
|
15
|
+
* The lock name is scoped by the configuration's environment
|
|
16
|
+
* (`server.<environment>` → `tmp/server.<environment>.lock`) so a development
|
|
17
|
+
* server and a test-spawned backend in the same app directory coexist, while
|
|
18
|
+
* the single-server-per-(directory, environment) invariant still fails loudly
|
|
19
|
+
* for a duplicate within one environment (STC-697).
|
|
20
|
+
*
|
|
21
|
+
* A caller that knowingly wants a second server in the same
|
|
22
|
+
* (directory, environment) pair can opt out of this lock (STC-698). The lock
|
|
23
|
+
* itself is unchanged for every other caller: an opted-out lock never acquires
|
|
24
|
+
* and therefore never releases, so it can never remove a live owner's lock.
|
|
13
25
|
*/
|
|
14
26
|
export default class VelociousHttpServerLock {
|
|
15
27
|
/**
|
|
16
|
-
* Build a lock for the configured application directory and
|
|
28
|
+
* Build a lock for the configured application directory, environment and
|
|
29
|
+
* server endpoint.
|
|
17
30
|
* @param {object} args - Options object.
|
|
18
31
|
* @param {import("../configuration.js").default} args.configuration - Configuration that owns the application directory.
|
|
19
32
|
* @param {string} args.host - Configured HTTP host.
|
|
20
33
|
* @param {number} args.port - Configured HTTP port.
|
|
34
|
+
* @param {boolean} [args.optOut] - Skip acquisition for this caller instead of contending for the lock.
|
|
21
35
|
*/
|
|
22
|
-
constructor({configuration, host, port}) {
|
|
36
|
+
constructor({configuration, host, port, optOut = false}) {
|
|
23
37
|
this.configuration = configuration
|
|
24
38
|
this.host = host
|
|
25
39
|
this.port = port
|
|
26
|
-
this.
|
|
40
|
+
this.optOut = optOut
|
|
41
|
+
this.environment = String(configuration.getEnvironment())
|
|
42
|
+
// Scope the lock name by environment and keep the segment filesystem-safe,
|
|
43
|
+
// so odd environment names can never escape tmp/ or collide with each other.
|
|
44
|
+
this.lockName = `server.${this.environment.replace(/[^a-zA-Z0-9._-]/g, "_")}`
|
|
45
|
+
this.lockPath = path.join(configuration.getDirectory(), "tmp", `${this.lockName}.lock`)
|
|
27
46
|
this.backend = new AdvisoryLockFileBackend({
|
|
28
47
|
directory: path.join(configuration.getDirectory(), "tmp"),
|
|
29
48
|
extraOwnerFields: {host: this.host, port: this.port},
|
|
@@ -33,28 +52,36 @@ export default class VelociousHttpServerLock {
|
|
|
33
52
|
}
|
|
34
53
|
|
|
35
54
|
/**
|
|
36
|
-
* Acquires the app-directory HTTP server lock before startup
|
|
37
|
-
* run.
|
|
38
|
-
*
|
|
39
|
-
*
|
|
55
|
+
* Acquires the app-directory + environment HTTP server lock before startup
|
|
56
|
+
* side effects run. An opted-out caller never touches the lock, so the live
|
|
57
|
+
* owner keeps it. A stale lock (dead local owner or expired lease) is
|
|
58
|
+
* reclaimed by the backend; a live foreign owner in the same environment
|
|
59
|
+
* fails loudly with the owner details.
|
|
60
|
+
* @returns {Promise<boolean>} - Whether this caller holds the lock.
|
|
40
61
|
*/
|
|
41
62
|
async acquire() {
|
|
42
|
-
|
|
63
|
+
if (this.optOut) return false
|
|
64
|
+
|
|
65
|
+
const acquired = await this.backend.acquire(this.lockName, {timeoutMs: 0})
|
|
43
66
|
|
|
44
67
|
if (!acquired) throw new Error(await this.lockHeldMessage())
|
|
45
68
|
|
|
46
69
|
this.acquired = true
|
|
70
|
+
|
|
71
|
+
return true
|
|
47
72
|
}
|
|
48
73
|
|
|
49
74
|
/**
|
|
50
|
-
* Releases the held HTTP server lock directory.
|
|
75
|
+
* Releases the held environment-scoped HTTP server lock directory. A lock
|
|
76
|
+
* that was never acquired (an opted-out caller) is a no-op, so it can never
|
|
77
|
+
* remove the live owner's lock on shutdown.
|
|
51
78
|
* @returns {Promise<void>} - Resolves after best-effort lock release.
|
|
52
79
|
*/
|
|
53
80
|
async release() {
|
|
54
81
|
if (!this.acquired) return
|
|
55
82
|
|
|
56
83
|
this.acquired = false
|
|
57
|
-
await this.backend.release(
|
|
84
|
+
await this.backend.release(this.lockName)
|
|
58
85
|
}
|
|
59
86
|
|
|
60
87
|
/**
|
|
@@ -62,7 +89,7 @@ export default class VelociousHttpServerLock {
|
|
|
62
89
|
* @returns {Promise<import("../database/advisory-lock-file-backend.js").AdvisoryLockFileOwner | null>} - Parsed owner metadata, when readable.
|
|
63
90
|
*/
|
|
64
91
|
async readOwnerMetadata() {
|
|
65
|
-
return await this.backend.holderInfo(
|
|
92
|
+
return await this.backend.holderInfo(this.lockName)
|
|
66
93
|
}
|
|
67
94
|
|
|
68
95
|
/**
|
|
@@ -75,6 +102,16 @@ export default class VelociousHttpServerLock {
|
|
|
75
102
|
? `PID ${String(owner.pid)} on ${String(owner.hostname)} (${String(owner.host)}:${String(owner.port)})`
|
|
76
103
|
: `lock directory ${this.lockPath}`
|
|
77
104
|
|
|
78
|
-
return `A Velocious HTTP server is already running for this application (${details}). Remove ${this.lockPath} if the server is no longer running.`
|
|
105
|
+
return `A Velocious HTTP server is already running for this application in the ${this.environment} environment (${details}). Remove ${this.lockPath} if the server is no longer running.`
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Builds the warning emitted when a caller knowingly starts a server without
|
|
110
|
+
* the lock, so an unprotected production process is visible in the log rather
|
|
111
|
+
* than silently running without duplicate-server protection.
|
|
112
|
+
* @returns {string} - Warning message naming the application directory and port.
|
|
113
|
+
*/
|
|
114
|
+
lockSkippedMessage() {
|
|
115
|
+
return `Skipping the HTTP server lock for ${this.configuration.getDirectory()} on ${this.host}:${String(this.port)} because the caller opted out. This server runs without duplicate-server protection for the ${this.environment} environment.`
|
|
79
116
|
}
|
|
80
117
|
}
|
|
@@ -2,15 +2,16 @@ import Logger from "./logger.js";
|
|
|
2
2
|
import HttpServer from "./http-server/index.js";
|
|
3
3
|
import HttpServerLock from "./http-server/server-lock.js";
|
|
4
4
|
export type HttpServerConfiguration = import("./configuration-types.js").HttpServerConfiguration;
|
|
5
|
-
/**
|
|
6
|
-
* HttpServerConfiguration type.
|
|
7
|
-
* @typedef {import("./configuration-types.js").HttpServerConfiguration} HttpServerConfiguration */
|
|
8
5
|
export default class VelociousApplication {
|
|
9
6
|
configuration: import("./configuration.js").default;
|
|
10
7
|
/**
|
|
11
8
|
* Stores the http server configuration value.
|
|
12
9
|
* @type {HttpServerConfiguration} */
|
|
13
10
|
httpServerConfiguration: HttpServerConfiguration;
|
|
11
|
+
/**
|
|
12
|
+
* Stores the explicit HTTP server lock opt-out for this application.
|
|
13
|
+
* @type {boolean} */
|
|
14
|
+
httpServerLockOptOut: boolean;
|
|
14
15
|
logger: Logger;
|
|
15
16
|
_type: string;
|
|
16
17
|
/**
|
|
@@ -26,11 +27,13 @@ export default class VelociousApplication {
|
|
|
26
27
|
* @param {object} args - Options object.
|
|
27
28
|
* @param {import("./configuration.js").default} args.configuration - Configuration instance.
|
|
28
29
|
* @param {HttpServerConfiguration} [args.httpServer] - Http server.
|
|
30
|
+
* @param {boolean} [args.httpServerLockOptOut] - Start this server without the HTTP server lock, knowingly running unprotected against a duplicate server in the same application directory and environment.
|
|
29
31
|
* @param {string} args.type - Type identifier.
|
|
30
32
|
*/
|
|
31
|
-
constructor({ configuration, httpServer, type, ...restArgs }: {
|
|
33
|
+
constructor({ configuration, httpServer, httpServerLockOptOut, type, ...restArgs }: {
|
|
32
34
|
configuration: import("./configuration.js").default;
|
|
33
35
|
httpServer?: HttpServerConfiguration;
|
|
36
|
+
httpServerLockOptOut?: boolean;
|
|
34
37
|
type: string;
|
|
35
38
|
});
|
|
36
39
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"application.d.ts","sourceRoot":"","sources":["../../src/application.js"],"names":[],"mappings":"AAGA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,UAAU,MAAM,wBAAwB,CAAA;AAC/C,OAAO,cAAc,MAAM,8BAA8B,CAAA;AAUtD,YAAsE,uBAAuB,GAAnF,OAAO,0BAA0B,EAAE,uBAAuB,CAAyB;
|
|
1
|
+
{"version":3,"file":"application.d.ts","sourceRoot":"","sources":["../../src/application.js"],"names":[],"mappings":"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"}
|