Skip to content

Commit d40bcfa

Browse files
committed
fix(service-automation): a connection already marked reconnect required answers the cause it stores, and the log names it
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9ae5df9 commit d40bcfa

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

‎packages/services/service-automation/src/connector-connection-door.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ describe('ADR-0143 D3/D6 — what a grant leaves behind', () => {
600600
// The logs did record the connect — the pin is not passing on silence.
601601
expect(out.text()).toContain("connector instance 'crm' connected for organization 'org_a' by user 'usr_admin'");
602602
expect(out.text()).toContain(
603-
"connector instance 'crm' needs to be reconnected for organization 'org_a': its authorization server refused the grant",
603+
"[Automation] connector instance 'crm' needs to be reconnected for organization 'org_a': its authorization server refused the grant",
604604
);
605605
});
606606

‎packages/services/service-automation/src/oauth2-grant/grant-handle.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ export interface OAuth2GrantHandleDeps {
7878
/** The cluster lock, when the deployment has a cluster service. Read at refresh time. */
7979
readonly lock?: () => ILock | undefined;
8080
/**
81-
* Told when the authorization server refused an organization's grant and
82-
* the connection was marked `reconnect_required`, so the plugin re-judges
81+
* Told when a refresh marked an organization's connection
82+
* `reconnect_required`, with the `reason` it stored (whose cause is the
83+
* one that happened on that path), so the plugin logs it and re-judges
8384
* the instance. A throw here is the listener's own.
8485
*/
85-
readonly onGrantRefused?: (organizationId: string) => void;
86+
readonly onGrantRefused?: (organizationId: string, reason: string) => void;
8687
readonly timeoutMs?: number;
8788
readonly fetch?: typeof fetch;
8889
readonly now?: () => number;
@@ -155,6 +156,15 @@ export function reconnectRequiredReason(
155156
+ 'uses the new connection (ADR-0143 D4)';
156157
}
157158

159+
/**
160+
* The dispatch refusal of a connection already marked `reconnect_required`:
161+
* the reason the row stores, whose cause is the one that marked it, and the
162+
* authorization server's refusal only for a row that stores none.
163+
*/
164+
function markedReason(connector: string, record: ConnectorConnectionRecord): string {
165+
return record.statusReason ?? reconnectRequiredReason(connector, record.organizationId);
166+
}
167+
158168
/** Create the handle of one `oauth2` instance. */
159169
export function createOAuth2GrantHandle(deps: OAuth2GrantHandleDeps): OAuth2GrantHandle {
160170
const { connector, config } = deps;
@@ -235,7 +245,7 @@ export function createOAuth2GrantHandle(deps: OAuth2GrantHandleDeps): OAuth2Gran
235245
throw unavailable(`the connection of connector instance '${connector}' could not be read now; the next dispatch retries`);
236246
}
237247
if (!record) throw unavailable(notConnectedReason(connector, organizationId));
238-
if (record.status === 'reconnect_required') throw unavailable(reconnectRequiredReason(connector, record.organizationId));
248+
if (record.status === 'reconnect_required') throw unavailable(markedReason(connector, record));
239249
return record;
240250
};
241251

@@ -248,7 +258,7 @@ export function createOAuth2GrantHandle(deps: OAuth2GrantHandleDeps): OAuth2Gran
248258
/* the refusal stands either way; the row is re-judged on the next dispatch */
249259
}
250260
try {
251-
deps.onGrantRefused?.(record.organizationId);
261+
deps.onGrantRefused?.(record.organizationId, reason);
252262
} catch {
253263
/* the listener's throw is its own */
254264
}
@@ -286,7 +296,7 @@ export function createOAuth2GrantHandle(deps: OAuth2GrantHandleDeps): OAuth2Gran
286296
const refreshOnce = async (organizationId: string): Promise<CachedToken> => {
287297
const current = await deps.connections.read(organizationId, connector);
288298
if (!current) throw unavailable(notConnectedReason(connector, organizationId));
289-
if (current.status === 'reconnect_required') throw unavailable(reconnectRequiredReason(connector, organizationId));
299+
if (current.status === 'reconnect_required') throw unavailable(markedReason(connector, current));
290300
let refreshToken: string | undefined;
291301
try {
292302
refreshToken = await deps.connections.resolveRefreshToken(current);

‎packages/services/service-automation/src/plugin.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,11 +3692,10 @@ export class AutomationServicePlugin implements Plugin {
36923692
},
36933693
connections: this.connectorConnectionStoreOrNone(),
36943694
lock: () => this.clusterService(ctx)?.lock,
3695-
onGrantRefused: (organizationId) => {
3696-
ctx.logger.warn(
3697-
`[Automation] connector instance '${name}': the authorization server refused the grant of organization `
3698-
+ `'${organizationId}'; it needs to be reconnected there (ADR-0143 D4).`,
3699-
);
3695+
onGrantRefused: (_organizationId, reason) => {
3696+
// The reason the connection stored: it names the instance, the
3697+
// organization and the cause that happened on that refresh path.
3698+
ctx.logger.warn(`[Automation] ${reason}.`);
37003699
void this.materializeDeclaredConnectors(ctx, { fatal: false }).catch(() => { /* logged by the reconcile */ });
37013700
},
37023701
...(typeof entry.requestTimeoutMs === 'number' ? { timeoutMs: entry.requestTimeoutMs } : {}),

0 commit comments

Comments
 (0)