Skip to content

Commit 3514f6c

Browse files
fix(redis): treat ignorable Redis errors as warnings (#5325)
* fix(redis): treat intermittent errors as warnings Signed-off-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> * add changeset Signed-off-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> * fix(rush): intermittent redis errors Signed-off-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> * fix lint + move ordering Signed-off-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> * Apply suggestions from code review * destroy the client Signed-off-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> * fix types Signed-off-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> * fix api report + test build Signed-off-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> * Apply suggestion from @iclanton --------- Signed-off-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> Co-authored-by: Aramis Sennyey <aramissennyeydd@users.noreply.github.com> Co-authored-by: Ian Clanton-Thuon <iclanton@users.noreply.github.com>
1 parent b84587c commit 3514f6c

6 files changed

Lines changed: 43 additions & 17 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Treat intermittent ignored redis errors as warnings and allow build to continue.",
5+
"type": "none",
6+
"packageName": "@microsoft/rush"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "aramissennyeydd@users.noreply.github.com"
11+
}

common/config/subspaces/default/pnpm-lock.yaml

Lines changed: 5 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
22
{
3-
"pnpmShrinkwrapHash": "c643d2cc7e2c60ef034a6557fc89760140d42f66",
3+
"pnpmShrinkwrapHash": "e0b573b05fff66c7c9e7ae104e8a2f863ef58864",
44
"preferredVersionsHash": "61cd419c533464b580f653eb5f5a7e27fe7055ca"
55
}

rush-plugins/rush-redis-cobuild-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"_phase:test": "heft run --only test -- --clean"
2020
},
2121
"dependencies": {
22-
"@redis/client": "~1.5.5",
22+
"@redis/client": "~5.8.2",
2323
"@rushstack/node-core-library": "workspace:*",
2424
"@rushstack/rush-sdk": "workspace:*"
2525
},

rush-plugins/rush-redis-cobuild-plugin/src/RedisCobuildLockProvider.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ export class RedisCobuildLockProvider implements ICobuildLockProvider {
4646
string
4747
>();
4848

49-
private readonly _redisClient: RedisClientType<RedisModules, RedisFunctions, RedisScripts>;
49+
private readonly _redisClient: RedisClientType<RedisModules, RedisFunctions, RedisScripts, 2 | 3>;
5050

5151
public constructor(options: IRedisCobuildLockProviderOptions, rushSession: RushSession) {
5252
this._options = RedisCobuildLockProvider.expandOptionsWithEnvironmentVariables(options);
53+
// Provide a default reconnect strategy that prevents more than 5 reconnect attempts.
54+
this._options.socket = {
55+
reconnectStrategy: (count: number) => {
56+
this._terminal.writeErrorLine(`Redis client reconnecting attempt #${count}`);
57+
return count < 5 ? count * 1000 : false;
58+
},
59+
...this._options.socket
60+
};
5361
this._terminal = rushSession.getLogger('RedisCobuildLockProvider').terminal;
5462
try {
5563
this._redisClient = createClient(this._options);
@@ -95,11 +103,20 @@ export class RedisCobuildLockProvider implements ICobuildLockProvider {
95103
} catch (e) {
96104
throw new Error(`Failed to connect to redis server: ${e.message}`);
97105
}
106+
107+
// Register error event handler to avoid process exit when redis client error occurs.
108+
this._redisClient.on('error', (e: Error) => {
109+
if (e.message) {
110+
this._terminal.writeErrorLine(`Redis client error: ${e.message}`);
111+
} else {
112+
this._terminal.writeErrorLine(`Redis client error: ${e}`);
113+
}
114+
});
98115
}
99116

100117
public async disconnectAsync(): Promise<void> {
101118
try {
102-
await this._redisClient.disconnect();
119+
await this._redisClient.destroy();
103120
} catch (e) {
104121
throw new Error(`Failed to disconnect to redis server: ${e.message}`);
105122
}

rush-plugins/rush-redis-cobuild-plugin/src/test/RedisCobuildLockProvider.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ describe(RedisCobuildLockProvider.name, () => {
5858
get: jest.fn().mockImplementation((key: string) => {
5959
return storage[key];
6060
})
61-
} as unknown as RedisClientType;
61+
} as unknown as RedisClientType<
62+
redisAPI.RedisModules,
63+
redisAPI.RedisFunctions,
64+
redisAPI.RedisScripts,
65+
2 | 3
66+
>;
6267
});
6368
});
6469

0 commit comments

Comments
 (0)