@steve31415/baselib 3.76.0 → 3.77.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/README.md CHANGED
@@ -35,7 +35,8 @@ Bins: `check-test-owners` — the fleet's structural test-coverage gate; every
35
35
  app runs it from `npm run verify`. `pw-deploy` / `pw-rollback` — the fleet
36
36
  deploy (`~/plasticine-way/docs/OPERATIONS.md`, "Deployment safety"); an
37
37
  app's hooks module (`DeployHooks` in `src/deploy/types.ts`) extends both,
38
- `postRollback` being the one hook `pw-rollback` runs.
38
+ `preRollback` (before traffic moves; a throw refuses) and `postRollback`
39
+ (after) being the hooks `pw-rollback` runs.
39
40
  `pw-sql` / `pw-logs` — read-only production queries (database, Axiom), the
40
41
  first step of any production investigation (same doc, "Investigating
41
42
  production state").
@@ -93,6 +93,34 @@ export async function runRollback(options) {
93
93
  throw new Error(`services would land on different images for ${target.slice(0, 12)}: ` +
94
94
  targets.map((t) => `${t.name}=${t.imageDigest.slice(-12)}`).join(' '));
95
95
  }
96
+ const hookContext = {
97
+ sha: target,
98
+ baseSha: servingShas.length === 1 ? servingShas[0] : null,
99
+ config,
100
+ repoRoot: options.repoRoot,
101
+ contextDir: '',
102
+ evidenceDir,
103
+ imageDigest: targets[0].imageDigest,
104
+ serving,
105
+ exec: runner,
106
+ log,
107
+ };
108
+ // A hooks module that fails to load must not stand between an incident
109
+ // and the traffic move: log it and go on (postRollback below reports it
110
+ // again, after the move). Only a hook that loads and throws refuses.
111
+ let hooks = null;
112
+ let hooksLoadError = null;
113
+ try {
114
+ hooks = await loadHooks(options.repoRoot, config);
115
+ }
116
+ catch (error) {
117
+ hooksLoadError = error;
118
+ log(`hooks module failed to load; rolling back without preRollback: ${String(error)}`);
119
+ }
120
+ if (hooks?.preRollback) {
121
+ timer.enter('preRollback');
122
+ await hooks.preRollback(hookContext);
123
+ }
96
124
  timer.enter('traffic');
97
125
  for (const t of targets) {
98
126
  await must(runner, 'gcloud', [
@@ -152,26 +180,12 @@ export async function runRollback(options) {
152
180
  throw new Error(`${service.name}: post-rollback smoke failed (${lastDetail})`);
153
181
  log(`${service.name}: smoke passed`);
154
182
  }
155
- // The hooks module is loaded only now: a broken one must not stand
156
- // between an incident and the traffic move.
157
183
  timer.enter('hooks');
158
184
  rolledBackTo = target;
159
- const hooks = await loadHooks(options.repoRoot, config);
160
- if (hooks.postRollback) {
161
- const ctx = {
162
- sha: target,
163
- baseSha: servingShas.length === 1 ? servingShas[0] : null,
164
- config,
165
- repoRoot: options.repoRoot,
166
- contextDir: '',
167
- evidenceDir,
168
- imageDigest: targets[0].imageDigest,
169
- serving,
170
- exec: runner,
171
- log,
172
- };
173
- await hooks.postRollback(ctx);
174
- }
185
+ if (hooksLoadError)
186
+ throw hooksLoadError;
187
+ if (hooks?.postRollback)
188
+ await hooks.postRollback(hookContext);
175
189
  timer.close();
176
190
  log(`rolled back to ${target.slice(0, 12)}`);
177
191
  log(`hold placed on ${servingShas.map((sha) => sha.slice(0, 12)).join(', ')} — ` +
@@ -185,7 +199,7 @@ export async function runRollback(options) {
185
199
  console.error(`rollback failed in phase: ${timer.current}`);
186
200
  if (rolledBackTo) {
187
201
  console.error(`traffic IS on ${rolledBackTo.slice(0, 12)} and the hold is placed; only the app's ` +
188
- 'postRollback hook failed — finish what it does by hand');
202
+ 'hooks module or its postRollback hook failed — finish what postRollback does by hand');
189
203
  }
190
204
  if (evidenceDir)
191
205
  console.error(`evidence: ${evidenceDir}`);
@@ -123,6 +123,15 @@ export interface DeployHooks {
123
123
  smoke?: (ctx: HookContext) => Promise<void>;
124
124
  /** Runs after everything else succeeded (e.g. notes's release state). */
125
125
  postSuccess?: (ctx: HookContext) => Promise<void>;
126
+ /** pw-rollback only: runs after the target is chosen, the migration check
127
+ * passed and the target's revisions are found, before any traffic moves —
128
+ * for a refusal only the app can judge (e.g. notes refuses a target
129
+ * without its document relay while the serving revision allows more than
130
+ * one copy). Same context as postRollback. A throw aborts the rollback:
131
+ * no traffic moves and no hold is placed. A hooks module that fails to
132
+ * load does not block the rollback here (it is logged; postRollback then
133
+ * reports it). */
134
+ preRollback?: (ctx: HookContext) => Promise<void>;
126
135
  /** pw-rollback only: runs after traffic is back on the target build, the
127
136
  * hold is placed and the smoke passed — for what a traffic move does not
128
137
  * carry (e.g. podcasts' render Job image). `sha` and `imageDigest` are the
@@ -50,6 +50,7 @@ export declare class DbBroadcastBridge {
50
50
  private readonly heartbeatTimeoutMs;
51
51
  private readonly reconnectDelaysMs;
52
52
  private listener;
53
+ private listenerAttachedAt;
53
54
  private stopped;
54
55
  private connecting;
55
56
  /** Set once start() has returned. From then on the app may have sessions
@@ -60,6 +61,12 @@ export declare class DbBroadcastBridge {
60
61
  private reconnectTimer;
61
62
  private heartbeatTimer;
62
63
  constructor(options: DbBroadcastBridgeOptions);
64
+ /** When the current LISTEN connection was attached (`Date.now()`), or
65
+ * null while there is none: before the first connect lands, from the
66
+ * moment a loss is noticed until the reconnect lands, and after stop().
67
+ * For an app that must act on a lost listener (e.g. Notes' document
68
+ * relay, which polls while nothing listens) without parsing the log. */
69
+ get connectedSince(): number | null;
63
70
  /** Makes the first connect attempt; never rejects. A failure is logged
64
71
  * and retried on the reconnect ladder (see the header). */
65
72
  start(): Promise<void>;
@@ -39,6 +39,7 @@ export class DbBroadcastBridge {
39
39
  heartbeatTimeoutMs;
40
40
  reconnectDelaysMs;
41
41
  listener = null;
42
+ listenerAttachedAt = null;
42
43
  stopped = true;
43
44
  connecting = false;
44
45
  /** Set once start() has returned. From then on the app may have sessions
@@ -62,6 +63,14 @@ export class DbBroadcastBridge {
62
63
  this.heartbeatTimeoutMs = options.heartbeatTimeoutMs ?? DB_BRIDGE_HEARTBEAT_TIMEOUT_MS;
63
64
  this.reconnectDelaysMs = options.reconnectDelaysMs ?? DB_BRIDGE_RECONNECT_DELAYS_MS;
64
65
  }
66
+ /** When the current LISTEN connection was attached (`Date.now()`), or
67
+ * null while there is none: before the first connect lands, from the
68
+ * moment a loss is noticed until the reconnect lands, and after stop().
69
+ * For an app that must act on a lost listener (e.g. Notes' document
70
+ * relay, which polls while nothing listens) without parsing the log. */
71
+ get connectedSince() {
72
+ return this.listenerAttachedAt;
73
+ }
65
74
  /** Makes the first connect attempt; never rejects. A failure is logged
66
75
  * and retried on the reconnect ladder (see the header). */
67
76
  async start() {
@@ -148,6 +157,7 @@ export class DbBroadcastBridge {
148
157
  return;
149
158
  }
150
159
  this.listener = client;
160
+ this.listenerAttachedAt = Date.now();
151
161
  this.adjustPoolMin(1);
152
162
  client.on('notification', (notification) => this.onNotification(notification.payload));
153
163
  this.scheduleHeartbeat(client);
@@ -191,6 +201,7 @@ export class DbBroadcastBridge {
191
201
  if (!listener)
192
202
  return null;
193
203
  this.listener = null;
204
+ this.listenerAttachedAt = null;
194
205
  if (this.heartbeatTimer)
195
206
  clearTimeout(this.heartbeatTimer);
196
207
  this.heartbeatTimer = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "3.76.0",
3
+ "version": "3.77.0",
4
4
  "description": "Plasticine new-world shared platform library: logging, auth, service-to-service auth, db, HTTP, sync, app updates",
5
5
  "type": "module",
6
6
  "license": "MIT",