Skip to content

Commit ef8a136

Browse files
committed
refactor: enhance API query handling with timeout and improve service exports
Updated the ApiReportService to implement a timeout for GraphQL queries, enhancing reliability during data fetching. Modified the CliInternalClientService to include a fetch timeout mechanism. Additionally, improved the export structure in the shared package to include the ApiKeyService and other relevant modules for better accessibility.
1 parent 254816c commit ef8a136

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

.claude/settings.local.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
"Bash(pnpm --filter @unraid/shared build)",
4040
"Bash(pnpm --filter ./api test src/unraid-api/cli/__test__/report.command.test.ts --run)",
4141
"Bash(pnpm:*)",
42-
"Bash(git checkout:*)"
42+
"Bash(git checkout:*)",
43+
"Bash(node:*)",
44+
"Bash(time node:*)"
4345
]
4446
},
4547
"enableAllProjectMcpServers": false

api/src/unraid-api/cli/admin-key.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
22

3+
import type { ApiKeyService } from '@unraid/shared/services/api-key.js';
34
import { Role } from '@unraid/shared/graphql.model.js';
4-
import { ApiKeyService } from '@unraid/shared/services/api-key.js';
55
import { API_KEY_SERVICE_TOKEN } from '@unraid/shared/tokens.js';
66

77
/**

api/src/unraid-api/cli/api-report.service.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,14 @@ export class ApiReportService {
140140
// Query system data
141141
let systemResult: { data: SystemReportQuery } | null = null;
142142
try {
143-
systemResult = await client.query({
144-
query: SYSTEM_REPORT_QUERY,
145-
});
143+
systemResult = await Promise.race([
144+
client.query({
145+
query: SYSTEM_REPORT_QUERY,
146+
}),
147+
new Promise<never>((_, reject) =>
148+
setTimeout(() => reject(new Error('Query timeout after 3 seconds')), 3000)
149+
),
150+
]);
146151
} catch (error) {
147152
this.logger.error('Error querying system data: ' + error);
148153
return this.createApiReportData({
@@ -154,9 +159,14 @@ export class ApiReportService {
154159
// Try to query connect status
155160
let connectData: ConnectStatusQuery['connect'] | null = null;
156161
try {
157-
const connectResult = await client.query({
158-
query: CONNECT_STATUS_QUERY,
159-
});
162+
const connectResult = await Promise.race([
163+
client.query({
164+
query: CONNECT_STATUS_QUERY,
165+
}),
166+
new Promise<never>((_, reject) =>
167+
setTimeout(() => reject(new Error('Connect query timeout after 3 seconds')), 3000)
168+
),
169+
]);
160170
connectData = connectResult.data.connect;
161171
} catch (error) {
162172
this.logger.debug('Connect plugin not available: ' + error);
@@ -165,9 +175,14 @@ export class ApiReportService {
165175
// Query services
166176
let servicesData: ServiceInfo[] = [];
167177
try {
168-
const servicesResult = await client.query({
169-
query: SERVICES_QUERY,
170-
});
178+
const servicesResult = await Promise.race([
179+
client.query({
180+
query: SERVICES_QUERY,
181+
}),
182+
new Promise<never>((_, reject) =>
183+
setTimeout(() => reject(new Error('Services query timeout after 3 seconds')), 3000)
184+
),
185+
]);
171186
servicesData = servicesResult.data.services || [];
172187
} catch (error) {
173188
this.logger.debug('Error querying services: ' + error);

api/src/unraid-api/cli/internal-client.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ export class CliInternalClientService {
7979

8080
const httpLink = new HttpLink({
8181
uri: httpUri,
82-
fetch,
82+
fetch: (uri, options) => {
83+
const controller = new AbortController();
84+
const timeoutId = setTimeout(() => controller.abort(), 3000); // 3 second timeout
85+
86+
return fetch(uri, {
87+
...options,
88+
signal: controller.signal,
89+
}).finally(() => clearTimeout(timeoutId));
90+
},
8391
headers: {
8492
Origin: '/var/run/unraid-cli.sock',
8593
'x-api-key': apiKey,
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export {}
1+
export { ApiKeyService } from './services/api-key.js';
2+
export * from './graphql.model.js';
3+
export * from './tokens.js';
4+
export * from './use-permissions.directive.js';

0 commit comments

Comments
 (0)