@molecule/api-instance-leadership 0.0.0-stage → 1.1.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.
- package/LICENSE +202 -0
- package/README.md +293 -2
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +19 -0
- package/dist/browser-guard.js.map +1 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +58 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +79 -0
- package/dist/provider.js.map +1 -0
- package/dist/types.d.ts +128 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +18 -0
- package/dist/types.js.map +1 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,294 @@
|
|
|
1
|
-
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-10-19T22:02:10.338Z
|
|
7
|
+
-->
|
|
2
8
|
|
|
3
|
-
|
|
9
|
+
# @molecule/api-instance-leadership
|
|
10
|
+
|
|
11
|
+
Instance-leadership core interface for molecule.dev: elect ONE API instance
|
|
12
|
+
to run platform-wide singleton work, and hand leadership over within seconds
|
|
13
|
+
when the holder's connection is lost.
|
|
14
|
+
|
|
15
|
+
Bond a provider — `@molecule/api-instance-leadership-postgresql` — at
|
|
16
|
+
startup, then start leader-only work from `acquireLeadership()`:
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { acquireLeadership, isLeader } from '@molecule/api-instance-leadership'
|
|
20
|
+
import { provider } from '@molecule/api-instance-leadership-postgresql'
|
|
21
|
+
import { setProvider } from '@molecule/api-instance-leadership'
|
|
22
|
+
|
|
23
|
+
setProvider(provider)
|
|
24
|
+
|
|
25
|
+
acquireLeadership({
|
|
26
|
+
onAcquired: () => startPoolManager(),
|
|
27
|
+
onLost: () => stopPoolManager(),
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**Only the code you run inside `onAcquired` is leader-only.** Every instance
|
|
32
|
+
still serves HTTP traffic, processes queue messages, and answers readiness
|
|
33
|
+
probes. Reach for this package when a job must run once across the whole
|
|
34
|
+
platform — pre-warm pool managers, retention sweeps, status recorders,
|
|
35
|
+
periodic billing — not for anything request-driven.
|
|
36
|
+
|
|
37
|
+
Use `isLeader()` for facts that must be decided at call time (e.g. skipping a
|
|
38
|
+
duplicate periodic tick); use `onAcquired`/`onLost` to start and stop the
|
|
39
|
+
loops themselves.
|
|
40
|
+
|
|
41
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
42
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
43
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
44
|
+
|
|
45
|
+
## Type
|
|
46
|
+
|
|
47
|
+
`core`
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install @molecule/api-instance-leadership @molecule/api-bond
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
### Interfaces
|
|
58
|
+
|
|
59
|
+
#### `LeaderLockSession`
|
|
60
|
+
|
|
61
|
+
One dedicated lock session, as a provider bond sees it.
|
|
62
|
+
|
|
63
|
+
A session must be backed by a connection the provider owns exclusively: the
|
|
64
|
+
lock lives and dies with that connection, so an operator never has to tune a
|
|
65
|
+
TTL and a crashed instance never leaves a stale lock row behind.
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
interface LeaderLockSession {
|
|
69
|
+
/**
|
|
70
|
+
* Ask the shared store for the lock WITHOUT blocking.
|
|
71
|
+
*
|
|
72
|
+
* @returns `true` when this session now holds it.
|
|
73
|
+
*/
|
|
74
|
+
tryLock(): Promise<boolean>
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Release the lock (a no-op when this session does not hold it) and close
|
|
78
|
+
* the connection.
|
|
79
|
+
*/
|
|
80
|
+
release(): Promise<void>
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Register the "this connection is gone" handler. Called at most once, when
|
|
84
|
+
* the connection errors or the store ends the session.
|
|
85
|
+
*
|
|
86
|
+
* @param handler - Invoked on connection loss.
|
|
87
|
+
*/
|
|
88
|
+
onLost(handler: () => void): void
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### `AcquireLeadershipOptions`
|
|
93
|
+
|
|
94
|
+
What {@link InstanceLeadershipProvider.acquire} needs from the application.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
interface AcquireLeadershipOptions {
|
|
98
|
+
/**
|
|
99
|
+
* Start the singleton subsystems. Invoked once per acquisition — including a
|
|
100
|
+
* RE-acquisition after a connection loss, so it must be safe to call again
|
|
101
|
+
* after {@link AcquireLeadershipOptions.onLost} stopped everything.
|
|
102
|
+
*/
|
|
103
|
+
onAcquired: () => void | Promise<void>
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Stop the singleton subsystems. Invoked when a leader loses its lock
|
|
107
|
+
* connection, BEFORE the provider tries to re-acquire: the store has (or is
|
|
108
|
+
* about to) release the lock, another instance may already be taking over,
|
|
109
|
+
* and two live leaders is exactly what leadership exists to prevent.
|
|
110
|
+
*/
|
|
111
|
+
onLost?: () => void | Promise<void>
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### `InstanceLeadershipProvider`
|
|
116
|
+
|
|
117
|
+
The contract every instance-leadership provider implements.
|
|
118
|
+
|
|
119
|
+
Application code reaches the bonded provider through the accessors in
|
|
120
|
+
`@molecule/api-instance-leadership` (`setProvider`, `getProvider`,
|
|
121
|
+
`isLeader`, `acquireLeadership`, `releaseLeadership`) rather than importing
|
|
122
|
+
a bond.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
interface InstanceLeadershipProvider {
|
|
126
|
+
/**
|
|
127
|
+
* Whether THIS instance currently holds the leadership lock.
|
|
128
|
+
*
|
|
129
|
+
* @returns `true` from the moment the lock is acquired until it is lost or
|
|
130
|
+
* released.
|
|
131
|
+
*/
|
|
132
|
+
isLeader(): boolean
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The current leadership phase.
|
|
136
|
+
*
|
|
137
|
+
* @returns The {@link LeadershipState}.
|
|
138
|
+
*/
|
|
139
|
+
state(): LeadershipState
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Start contending for the lock, retrying forever in the background. Never
|
|
143
|
+
* rejects and never blocks — the caller starts its singleton subsystems from
|
|
144
|
+
* {@link AcquireLeadershipOptions.onAcquired}.
|
|
145
|
+
*
|
|
146
|
+
* Calling `acquire()` again while already contending or leading is a no-op.
|
|
147
|
+
*
|
|
148
|
+
* @param options - What to start on acquisition and stop on loss.
|
|
149
|
+
*/
|
|
150
|
+
acquire(options: AcquireLeadershipOptions): void
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Stop contending and release the lock, so another instance can take over
|
|
154
|
+
* immediately instead of waiting for this process to exit.
|
|
155
|
+
*
|
|
156
|
+
* @returns Resolves once the lock release and the connection close settle.
|
|
157
|
+
*/
|
|
158
|
+
release(): Promise<void>
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Types
|
|
163
|
+
|
|
164
|
+
#### `LeadershipState`
|
|
165
|
+
|
|
166
|
+
The leadership phase of one instance.
|
|
167
|
+
|
|
168
|
+
- `follower` — the instance is contending for the lock but does not hold it.
|
|
169
|
+
- `leader` — the instance holds the lock and runs the singleton work.
|
|
170
|
+
- `stopped` — `release()` was called; the instance no longer contends.
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
type LeadershipState = 'follower' | 'leader' | 'stopped'
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### `LeaderLockFactory`
|
|
177
|
+
|
|
178
|
+
Creates a fresh lock session.
|
|
179
|
+
|
|
180
|
+
Injected rather than imported so a host application can reuse its own
|
|
181
|
+
database connection settings, and so tests can drive several simulated
|
|
182
|
+
instances through one fake store.
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
type LeaderLockFactory = () => Promise<LeaderLockSession>
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Functions
|
|
189
|
+
|
|
190
|
+
#### `setProvider(provider)`
|
|
191
|
+
|
|
192
|
+
Registers an instance-leadership provider as the active singleton. Called by
|
|
193
|
+
a bond package during application startup.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
function setProvider(provider: InstanceLeadershipProvider): void
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
- `provider` — The provider implementation to bond.
|
|
200
|
+
|
|
201
|
+
#### `getProvider()`
|
|
202
|
+
|
|
203
|
+
Retrieves the bonded instance-leadership provider, throwing if none is wired.
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
function getProvider(): InstanceLeadershipProvider
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
**Returns:** The bonded instance-leadership provider.
|
|
210
|
+
|
|
211
|
+
#### `hasProvider()`
|
|
212
|
+
|
|
213
|
+
Checks whether an instance-leadership provider is currently bonded.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
function hasProvider(): boolean
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**Returns:** `true` if a provider is bonded.
|
|
220
|
+
|
|
221
|
+
#### `isLeader()`
|
|
222
|
+
|
|
223
|
+
Whether THIS instance currently holds platform leadership.
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
function isLeader(): boolean
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
**Returns:** `false` when no provider is bonded or this instance is a follower.
|
|
230
|
+
|
|
231
|
+
#### `leadershipState()`
|
|
232
|
+
|
|
233
|
+
The current leadership phase of this instance.
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
function leadershipState(): LeadershipState
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
**Returns:** The bonded provider's state, or `'follower'` when none is bonded.
|
|
240
|
+
|
|
241
|
+
#### `acquireLeadership(options)`
|
|
242
|
+
|
|
243
|
+
Starts contending for platform leadership through the bonded provider.
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
function acquireLeadership(options: AcquireLeadershipOptions): void
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
- `options` — What to start on acquisition and stop on loss.
|
|
250
|
+
|
|
251
|
+
#### `releaseLeadership()`
|
|
252
|
+
|
|
253
|
+
Releases platform leadership through the bonded provider so another
|
|
254
|
+
instance can take over immediately.
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
function releaseLeadership(): Promise<void>
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
**Returns:** Resolves once the release settles; a no-op when no provider is bonded.
|
|
261
|
+
|
|
262
|
+
## Available Providers
|
|
263
|
+
|
|
264
|
+
| Provider | Package |
|
|
265
|
+
| ---------- | ---------------------------------------------- |
|
|
266
|
+
| PostgreSQL | `@molecule/api-instance-leadership-postgresql` |
|
|
267
|
+
|
|
268
|
+
## Injection Notes
|
|
269
|
+
|
|
270
|
+
### Requirements
|
|
271
|
+
|
|
272
|
+
Peer dependencies:
|
|
273
|
+
|
|
274
|
+
- `@molecule/api-bond` ^1.0.1
|
|
275
|
+
|
|
276
|
+
### Runtime Dependencies
|
|
277
|
+
|
|
278
|
+
- `@molecule/api-bond`
|
|
279
|
+
|
|
280
|
+
- **The lock must live in a store every instance shares.** A host-local lock
|
|
281
|
+
(a loopback port, a file) elects one leader PER HOST, so every singleton
|
|
282
|
+
runs twice — the failure this package exists to prevent.
|
|
283
|
+
- **`onLost` is not optional in practice for a real leader.** A provider may
|
|
284
|
+
lose the lock while the process keeps running (a database connection that
|
|
285
|
+
errors); the instance demotes itself and another may already be leading.
|
|
286
|
+
Stop the loops in `onLost`, or two copies run.
|
|
287
|
+
- **Re-acquisition is possible.** `onAcquired` runs again after a loss, so
|
|
288
|
+
it must be safe to call after `onLost` stopped everything.
|
|
289
|
+
- **Followers retry forever in the background.** `acquireLeadership()` never
|
|
290
|
+
rejects and never blocks; a database that is down at boot just means this
|
|
291
|
+
instance stays a follower until it is reachable.
|
|
292
|
+
- **No provider bonded ⇒ `isLeader()` is `false`.** A single-instance app can
|
|
293
|
+
use this package unchanged (it never leads until a bond exists) or omit it
|
|
294
|
+
and run its singleton work directly.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-instance-leadership` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-instance-leadership is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=browser-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.js","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,GAAG,UAIT,CAAA;AACD,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IACrF,MAAM,IAAI,KAAK,CACb,4GAA4G;QAC1G,sFAAsF;QACtF,iFAAiF;QACjF,0CAA0C,CAC7C,CAAA;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instance-leadership core interface for molecule.dev: elect ONE API instance
|
|
3
|
+
* to run platform-wide singleton work, and hand leadership over within seconds
|
|
4
|
+
* when the holder's connection is lost.
|
|
5
|
+
*
|
|
6
|
+
* Bond a provider — `@molecule/api-instance-leadership-postgresql` — at
|
|
7
|
+
* startup, then start leader-only work from `acquireLeadership()`:
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { acquireLeadership, isLeader } from '@molecule/api-instance-leadership'
|
|
11
|
+
* import { provider } from '@molecule/api-instance-leadership-postgresql'
|
|
12
|
+
* import { setProvider } from '@molecule/api-instance-leadership'
|
|
13
|
+
*
|
|
14
|
+
* setProvider(provider)
|
|
15
|
+
*
|
|
16
|
+
* acquireLeadership({
|
|
17
|
+
* onAcquired: () => startPoolManager(),
|
|
18
|
+
* onLost: () => stopPoolManager(),
|
|
19
|
+
* })
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* **Only the code you run inside `onAcquired` is leader-only.** Every instance
|
|
23
|
+
* still serves HTTP traffic, processes queue messages, and answers readiness
|
|
24
|
+
* probes. Reach for this package when a job must run once across the whole
|
|
25
|
+
* platform — pre-warm pool managers, retention sweeps, status recorders,
|
|
26
|
+
* periodic billing — not for anything request-driven.
|
|
27
|
+
*
|
|
28
|
+
* Use `isLeader()` for facts that must be decided at call time (e.g. skipping a
|
|
29
|
+
* duplicate periodic tick); use `onAcquired`/`onLost` to start and stop the
|
|
30
|
+
* loops themselves.
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* - **The lock must live in a store every instance shares.** A host-local lock
|
|
34
|
+
* (a loopback port, a file) elects one leader PER HOST, so every singleton
|
|
35
|
+
* runs twice — the failure this package exists to prevent.
|
|
36
|
+
* - **`onLost` is not optional in practice for a real leader.** A provider may
|
|
37
|
+
* lose the lock while the process keeps running (a database connection that
|
|
38
|
+
* errors); the instance demotes itself and another may already be leading.
|
|
39
|
+
* Stop the loops in `onLost`, or two copies run.
|
|
40
|
+
* - **Re-acquisition is possible.** `onAcquired` runs again after a loss, so
|
|
41
|
+
* it must be safe to call after `onLost` stopped everything.
|
|
42
|
+
* - **Followers retry forever in the background.** `acquireLeadership()` never
|
|
43
|
+
* rejects and never blocks; a database that is down at boot just means this
|
|
44
|
+
* instance stays a follower until it is reachable.
|
|
45
|
+
* - **No provider bonded ⇒ `isLeader()` is `false`.** A single-instance app can
|
|
46
|
+
* use this package unchanged (it never leads until a bond exists) or omit it
|
|
47
|
+
* and run its singleton work directly.
|
|
48
|
+
*
|
|
49
|
+
* @module
|
|
50
|
+
*/
|
|
51
|
+
export * from './browser-guard.js';
|
|
52
|
+
export * from './provider.js';
|
|
53
|
+
export * from './types.js';
|
|
54
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instance-leadership core interface for molecule.dev: elect ONE API instance
|
|
3
|
+
* to run platform-wide singleton work, and hand leadership over within seconds
|
|
4
|
+
* when the holder's connection is lost.
|
|
5
|
+
*
|
|
6
|
+
* Bond a provider — `@molecule/api-instance-leadership-postgresql` — at
|
|
7
|
+
* startup, then start leader-only work from `acquireLeadership()`:
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { acquireLeadership, isLeader } from '@molecule/api-instance-leadership'
|
|
11
|
+
* import { provider } from '@molecule/api-instance-leadership-postgresql'
|
|
12
|
+
* import { setProvider } from '@molecule/api-instance-leadership'
|
|
13
|
+
*
|
|
14
|
+
* setProvider(provider)
|
|
15
|
+
*
|
|
16
|
+
* acquireLeadership({
|
|
17
|
+
* onAcquired: () => startPoolManager(),
|
|
18
|
+
* onLost: () => stopPoolManager(),
|
|
19
|
+
* })
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* **Only the code you run inside `onAcquired` is leader-only.** Every instance
|
|
23
|
+
* still serves HTTP traffic, processes queue messages, and answers readiness
|
|
24
|
+
* probes. Reach for this package when a job must run once across the whole
|
|
25
|
+
* platform — pre-warm pool managers, retention sweeps, status recorders,
|
|
26
|
+
* periodic billing — not for anything request-driven.
|
|
27
|
+
*
|
|
28
|
+
* Use `isLeader()` for facts that must be decided at call time (e.g. skipping a
|
|
29
|
+
* duplicate periodic tick); use `onAcquired`/`onLost` to start and stop the
|
|
30
|
+
* loops themselves.
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* - **The lock must live in a store every instance shares.** A host-local lock
|
|
34
|
+
* (a loopback port, a file) elects one leader PER HOST, so every singleton
|
|
35
|
+
* runs twice — the failure this package exists to prevent.
|
|
36
|
+
* - **`onLost` is not optional in practice for a real leader.** A provider may
|
|
37
|
+
* lose the lock while the process keeps running (a database connection that
|
|
38
|
+
* errors); the instance demotes itself and another may already be leading.
|
|
39
|
+
* Stop the loops in `onLost`, or two copies run.
|
|
40
|
+
* - **Re-acquisition is possible.** `onAcquired` runs again after a loss, so
|
|
41
|
+
* it must be safe to call after `onLost` stopped everything.
|
|
42
|
+
* - **Followers retry forever in the background.** `acquireLeadership()` never
|
|
43
|
+
* rejects and never blocks; a database that is down at boot just means this
|
|
44
|
+
* instance stays a follower until it is reachable.
|
|
45
|
+
* - **No provider bonded ⇒ `isLeader()` is `false`.** A single-instance app can
|
|
46
|
+
* use this package unchanged (it never leads until a bond exists) or omit it
|
|
47
|
+
* and run its singleton work directly.
|
|
48
|
+
*
|
|
49
|
+
* @module
|
|
50
|
+
*/
|
|
51
|
+
export * from './browser-guard.js';
|
|
52
|
+
export * from './provider.js';
|
|
53
|
+
export * from './types.js';
|
|
54
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accessors for the bonded instance-leadership provider.
|
|
3
|
+
*
|
|
4
|
+
* A provider bond (e.g. `@molecule/api-instance-leadership-postgresql`) calls
|
|
5
|
+
* `setProvider()` during application startup. Application code uses the
|
|
6
|
+
* convenience accessors (`acquireLeadership`, `isLeader`, …), which delegate to
|
|
7
|
+
* the bonded provider.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { AcquireLeadershipOptions, InstanceLeadershipProvider, LeadershipState } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Registers an instance-leadership provider as the active singleton. Called by
|
|
14
|
+
* a bond package during application startup.
|
|
15
|
+
*
|
|
16
|
+
* @param provider - The provider implementation to bond.
|
|
17
|
+
*/
|
|
18
|
+
export declare const setProvider: (provider: InstanceLeadershipProvider) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves the bonded instance-leadership provider, throwing if none is wired.
|
|
21
|
+
*
|
|
22
|
+
* @returns The bonded instance-leadership provider.
|
|
23
|
+
* @throws {Error} If no instance-leadership provider has been bonded.
|
|
24
|
+
*/
|
|
25
|
+
export declare const getProvider: () => InstanceLeadershipProvider;
|
|
26
|
+
/**
|
|
27
|
+
* Checks whether an instance-leadership provider is currently bonded.
|
|
28
|
+
*
|
|
29
|
+
* @returns `true` if a provider is bonded.
|
|
30
|
+
*/
|
|
31
|
+
export declare const hasProvider: () => boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether THIS instance currently holds platform leadership.
|
|
34
|
+
*
|
|
35
|
+
* @returns `false` when no provider is bonded or this instance is a follower.
|
|
36
|
+
*/
|
|
37
|
+
export declare const isLeader: () => boolean;
|
|
38
|
+
/**
|
|
39
|
+
* The current leadership phase of this instance.
|
|
40
|
+
*
|
|
41
|
+
* @returns The bonded provider's state, or `'follower'` when none is bonded.
|
|
42
|
+
*/
|
|
43
|
+
export declare const leadershipState: () => LeadershipState;
|
|
44
|
+
/**
|
|
45
|
+
* Starts contending for platform leadership through the bonded provider.
|
|
46
|
+
*
|
|
47
|
+
* @param options - What to start on acquisition and stop on loss.
|
|
48
|
+
* @throws {Error} If no instance-leadership provider has been bonded.
|
|
49
|
+
*/
|
|
50
|
+
export declare const acquireLeadership: (options: AcquireLeadershipOptions) => void;
|
|
51
|
+
/**
|
|
52
|
+
* Releases platform leadership through the bonded provider so another
|
|
53
|
+
* instance can take over immediately.
|
|
54
|
+
*
|
|
55
|
+
* @returns Resolves once the release settles; a no-op when no provider is bonded.
|
|
56
|
+
*/
|
|
57
|
+
export declare const releaseLeadership: () => Promise<void>;
|
|
58
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,eAAe,EAChB,MAAM,YAAY,CAAA;AAInB;;;;;GAKG;AACH,eAAO,MAAM,WAAW,GAAI,UAAU,0BAA0B,KAAG,IAElE,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,WAAW,QAAO,0BAS9B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,WAAW,QAAO,OAE9B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,QAAQ,QAAO,OAE3B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,eAAe,QAAO,eAElC,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,SAAS,wBAAwB,KAAG,IAErE,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,QAAa,OAAO,CAAC,IAAI,CAEtD,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accessors for the bonded instance-leadership provider.
|
|
3
|
+
*
|
|
4
|
+
* A provider bond (e.g. `@molecule/api-instance-leadership-postgresql`) calls
|
|
5
|
+
* `setProvider()` during application startup. Application code uses the
|
|
6
|
+
* convenience accessors (`acquireLeadership`, `isLeader`, …), which delegate to
|
|
7
|
+
* the bonded provider.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import { bond, isBonded, require as bondRequire } from '@molecule/api-bond';
|
|
12
|
+
const BOND_TYPE = 'instance-leadership';
|
|
13
|
+
/**
|
|
14
|
+
* Registers an instance-leadership provider as the active singleton. Called by
|
|
15
|
+
* a bond package during application startup.
|
|
16
|
+
*
|
|
17
|
+
* @param provider - The provider implementation to bond.
|
|
18
|
+
*/
|
|
19
|
+
export const setProvider = (provider) => {
|
|
20
|
+
bond(BOND_TYPE, provider);
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the bonded instance-leadership provider, throwing if none is wired.
|
|
24
|
+
*
|
|
25
|
+
* @returns The bonded instance-leadership provider.
|
|
26
|
+
* @throws {Error} If no instance-leadership provider has been bonded.
|
|
27
|
+
*/
|
|
28
|
+
export const getProvider = () => {
|
|
29
|
+
try {
|
|
30
|
+
return bondRequire(BOND_TYPE);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
throw new Error('No instance-leadership provider set. Bond one (e.g. @molecule/api-instance-leadership-postgresql) at startup.', { cause: error });
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Checks whether an instance-leadership provider is currently bonded.
|
|
38
|
+
*
|
|
39
|
+
* @returns `true` if a provider is bonded.
|
|
40
|
+
*/
|
|
41
|
+
export const hasProvider = () => {
|
|
42
|
+
return isBonded(BOND_TYPE);
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Whether THIS instance currently holds platform leadership.
|
|
46
|
+
*
|
|
47
|
+
* @returns `false` when no provider is bonded or this instance is a follower.
|
|
48
|
+
*/
|
|
49
|
+
export const isLeader = () => {
|
|
50
|
+
return hasProvider() ? getProvider().isLeader() : false;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* The current leadership phase of this instance.
|
|
54
|
+
*
|
|
55
|
+
* @returns The bonded provider's state, or `'follower'` when none is bonded.
|
|
56
|
+
*/
|
|
57
|
+
export const leadershipState = () => {
|
|
58
|
+
return hasProvider() ? getProvider().state() : 'follower';
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Starts contending for platform leadership through the bonded provider.
|
|
62
|
+
*
|
|
63
|
+
* @param options - What to start on acquisition and stop on loss.
|
|
64
|
+
* @throws {Error} If no instance-leadership provider has been bonded.
|
|
65
|
+
*/
|
|
66
|
+
export const acquireLeadership = (options) => {
|
|
67
|
+
getProvider().acquire(options);
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Releases platform leadership through the bonded provider so another
|
|
71
|
+
* instance can take over immediately.
|
|
72
|
+
*
|
|
73
|
+
* @returns Resolves once the release settles; a no-op when no provider is bonded.
|
|
74
|
+
*/
|
|
75
|
+
export const releaseLeadership = async () => {
|
|
76
|
+
if (hasProvider())
|
|
77
|
+
await getProvider().release();
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAQ3E,MAAM,SAAS,GAAG,qBAAqB,CAAA;AAEvC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAAoC,EAAQ,EAAE;IACxE,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AAC3B,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAA+B,EAAE;IAC1D,IAAI,CAAC;QACH,OAAO,WAAW,CAA6B,SAAS,CAAC,CAAA;IAC3D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,+GAA+G,EAC/G,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAA;IACH,CAAC;AACH,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAY,EAAE;IACvC,OAAO,QAAQ,CAAC,SAAS,CAAC,CAAA;AAC5B,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,GAAY,EAAE;IACpC,OAAO,WAAW,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,KAAK,CAAA;AACzD,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,GAAoB,EAAE;IACnD,OAAO,WAAW,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,UAAU,CAAA;AAC3D,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,OAAiC,EAAQ,EAAE;IAC3E,WAAW,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;AAChC,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,IAAmB,EAAE;IACzD,IAAI,WAAW,EAAE;QAAE,MAAM,WAAW,EAAE,CAAC,OAAO,EAAE,CAAA;AAClD,CAAC,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for the instance-leadership core interface.
|
|
3
|
+
*
|
|
4
|
+
* Several subsystems of a multi-instance API assume exactly ONE runner across
|
|
5
|
+
* the whole platform rather than one per instance: pre-warm pool managers,
|
|
6
|
+
* hibernation and archive sweeps, status recorders, periodic billing jobs.
|
|
7
|
+
* Running them twice ranges from wasteful to corrupting. Instance leadership
|
|
8
|
+
* elects one instance — "the leader" — and runs those subsystems only there.
|
|
9
|
+
*
|
|
10
|
+
* The contract is deliberately small: the core names what leadership IS (a lock
|
|
11
|
+
* every instance contends for, held by one, lost when its connection dies) and
|
|
12
|
+
* a provider bond implements it against a shared store (e.g. a PostgreSQL
|
|
13
|
+
* session advisory lock in `@molecule/api-instance-leadership-postgresql`).
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* The leadership phase of one instance.
|
|
19
|
+
*
|
|
20
|
+
* - `follower` — the instance is contending for the lock but does not hold it.
|
|
21
|
+
* - `leader` — the instance holds the lock and runs the singleton work.
|
|
22
|
+
* - `stopped` — `release()` was called; the instance no longer contends.
|
|
23
|
+
*/
|
|
24
|
+
export type LeadershipState = 'follower' | 'leader' | 'stopped';
|
|
25
|
+
/**
|
|
26
|
+
* One dedicated lock session, as a provider bond sees it.
|
|
27
|
+
*
|
|
28
|
+
* A session must be backed by a connection the provider owns exclusively: the
|
|
29
|
+
* lock lives and dies with that connection, so an operator never has to tune a
|
|
30
|
+
* TTL and a crashed instance never leaves a stale lock row behind.
|
|
31
|
+
*/
|
|
32
|
+
export interface LeaderLockSession {
|
|
33
|
+
/**
|
|
34
|
+
* Ask the shared store for the lock WITHOUT blocking.
|
|
35
|
+
*
|
|
36
|
+
* @returns `true` when this session now holds it.
|
|
37
|
+
*/
|
|
38
|
+
tryLock(): Promise<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* Release the lock (a no-op when this session does not hold it) and close
|
|
41
|
+
* the connection.
|
|
42
|
+
*/
|
|
43
|
+
release(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Register the "this connection is gone" handler. Called at most once, when
|
|
46
|
+
* the connection errors or the store ends the session.
|
|
47
|
+
*
|
|
48
|
+
* @param handler - Invoked on connection loss.
|
|
49
|
+
*/
|
|
50
|
+
onLost(handler: () => void): void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates a fresh lock session.
|
|
54
|
+
*
|
|
55
|
+
* Injected rather than imported so a host application can reuse its own
|
|
56
|
+
* database connection settings, and so tests can drive several simulated
|
|
57
|
+
* instances through one fake store.
|
|
58
|
+
*/
|
|
59
|
+
export type LeaderLockFactory = () => Promise<LeaderLockSession>;
|
|
60
|
+
/**
|
|
61
|
+
* What {@link InstanceLeadershipProvider.acquire} needs from the application.
|
|
62
|
+
*/
|
|
63
|
+
export interface AcquireLeadershipOptions {
|
|
64
|
+
/**
|
|
65
|
+
* Start the singleton subsystems. Invoked once per acquisition — including a
|
|
66
|
+
* RE-acquisition after a connection loss, so it must be safe to call again
|
|
67
|
+
* after {@link AcquireLeadershipOptions.onLost} stopped everything.
|
|
68
|
+
*/
|
|
69
|
+
onAcquired: () => void | Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Stop the singleton subsystems. Invoked when a leader loses its lock
|
|
72
|
+
* connection, BEFORE the provider tries to re-acquire: the store has (or is
|
|
73
|
+
* about to) release the lock, another instance may already be taking over,
|
|
74
|
+
* and two live leaders is exactly what leadership exists to prevent.
|
|
75
|
+
*/
|
|
76
|
+
onLost?: () => void | Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The contract every instance-leadership provider implements.
|
|
80
|
+
*
|
|
81
|
+
* Application code reaches the bonded provider through the accessors in
|
|
82
|
+
* `@molecule/api-instance-leadership` (`setProvider`, `getProvider`,
|
|
83
|
+
* `isLeader`, `acquireLeadership`, `releaseLeadership`) rather than importing
|
|
84
|
+
* a bond.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* import { acquireLeadership, isLeader } from '@molecule/api-instance-leadership'
|
|
89
|
+
*
|
|
90
|
+
* acquireLeadership({
|
|
91
|
+
* onAcquired: () => startPeriodicSweeps(),
|
|
92
|
+
* onLost: () => stopPeriodicSweeps(),
|
|
93
|
+
* })
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export interface InstanceLeadershipProvider {
|
|
97
|
+
/**
|
|
98
|
+
* Whether THIS instance currently holds the leadership lock.
|
|
99
|
+
*
|
|
100
|
+
* @returns `true` from the moment the lock is acquired until it is lost or
|
|
101
|
+
* released.
|
|
102
|
+
*/
|
|
103
|
+
isLeader(): boolean;
|
|
104
|
+
/**
|
|
105
|
+
* The current leadership phase.
|
|
106
|
+
*
|
|
107
|
+
* @returns The {@link LeadershipState}.
|
|
108
|
+
*/
|
|
109
|
+
state(): LeadershipState;
|
|
110
|
+
/**
|
|
111
|
+
* Start contending for the lock, retrying forever in the background. Never
|
|
112
|
+
* rejects and never blocks — the caller starts its singleton subsystems from
|
|
113
|
+
* {@link AcquireLeadershipOptions.onAcquired}.
|
|
114
|
+
*
|
|
115
|
+
* Calling `acquire()` again while already contending or leading is a no-op.
|
|
116
|
+
*
|
|
117
|
+
* @param options - What to start on acquisition and stop on loss.
|
|
118
|
+
*/
|
|
119
|
+
acquire(options: AcquireLeadershipOptions): void;
|
|
120
|
+
/**
|
|
121
|
+
* Stop contending and release the lock, so another instance can take over
|
|
122
|
+
* immediately instead of waiting for this process to exit.
|
|
123
|
+
*
|
|
124
|
+
* @returns Resolves once the lock release and the connection close settle.
|
|
125
|
+
*/
|
|
126
|
+
release(): Promise<void>;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG,UAAU,GAAG,QAAQ,GAAG,SAAS,CAAA;AAE/D;;;;;;GAMG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;IAE3B;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IAExB;;;;;OAKG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI,CAAA;CAClC;AAED;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,OAAO,CAAC,iBAAiB,CAAC,CAAA;AAEhE;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC;;;;OAIG;IACH,UAAU,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAEtC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACpC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;;;OAKG;IACH,QAAQ,IAAI,OAAO,CAAA;IAEnB;;;;OAIG;IACH,KAAK,IAAI,eAAe,CAAA;IAExB;;;;;;;;OAQG;IACH,OAAO,CAAC,OAAO,EAAE,wBAAwB,GAAG,IAAI,CAAA;IAEhD;;;;;OAKG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACzB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for the instance-leadership core interface.
|
|
3
|
+
*
|
|
4
|
+
* Several subsystems of a multi-instance API assume exactly ONE runner across
|
|
5
|
+
* the whole platform rather than one per instance: pre-warm pool managers,
|
|
6
|
+
* hibernation and archive sweeps, status recorders, periodic billing jobs.
|
|
7
|
+
* Running them twice ranges from wasteful to corrupting. Instance leadership
|
|
8
|
+
* elects one instance — "the leader" — and runs those subsystems only there.
|
|
9
|
+
*
|
|
10
|
+
* The contract is deliberately small: the core names what leadership IS (a lock
|
|
11
|
+
* every instance contends for, held by one, lost when its connection dies) and
|
|
12
|
+
* a provider bond implements it against a shared store (e.g. a PostgreSQL
|
|
13
|
+
* session advisory lock in `@molecule/api-instance-leadership-postgresql`).
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-instance-leadership",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Instance-leadership core interface for molecule.dev: elect one API instance to run platform-wide singleton work, with demotion and takeover when the holder's connection is lost.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"dev": "tsc --watch",
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"test:watch": "vitest",
|
|
23
|
+
"test:coverage": "vitest run --coverage"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"molecule",
|
|
27
|
+
"leadership",
|
|
28
|
+
"leader election",
|
|
29
|
+
"singleton",
|
|
30
|
+
"distributed lock",
|
|
31
|
+
"multi-instance"
|
|
32
|
+
],
|
|
33
|
+
"author": "Molecule Dev, Inc. (https://molecule.dev)",
|
|
34
|
+
"license": "Apache-2.0",
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=20"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@molecule/api-bond": "^1.0.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@molecule/api-bond": "1.0.2",
|
|
43
|
+
"@types/node": "26.1.2",
|
|
44
|
+
"typescript": "6.0.3",
|
|
45
|
+
"vitest": "4.1.11"
|
|
46
|
+
},
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
50
|
+
"directory": "packages/api/core/instance-leadership"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://www.molecule.dev/packages/api-instance-leadership",
|
|
53
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|