Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: type improvement for schedulers #6136

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/lib/features/scheduler/schedule-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const scheduleServices = async (
apiTokenService.fetchActiveTokens.bind(apiTokenService),
minutesToMilliseconds(1),
'fetchActiveTokens',
0, // no jitter -> run immediately

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Getting worse: Large Method
scheduleServices increases from 135 to 136 lines of code, threshold = 70

Why does this problem occur?

Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function. Read more.

To ignore this warning click here.

);

schedulerService.schedule(
Expand Down Expand Up @@ -131,16 +132,16 @@ export const scheduleServices = async (
);

schedulerService.schedule(
() => {
clientMetricsServiceV2.bulkAdd().catch(console.error);
async () => {
await clientMetricsServiceV2.bulkAdd().catch(console.error);
Copy link
Contributor

@kwasniew kwasniew Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() => clientMetricsServiceV2.bulkAdd().catch(console.error); is almost the same since the last statement and returns Promise. The main difference is that it returns Promise with results value and not just Promise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but there's no return... I could add one, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it's more idiomatic to do () => Promise.resolve() over async () => { await Promise.resolve(); }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() => Promise.resolve() returns the promise when you don't add {} around it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() => Promise.resolve() is more idiomatic than () => { return Promise.resolve() }

},
secondsToMilliseconds(5),
'bulkAddMetrics',
);

schedulerService.schedule(
() => {
clientMetricsServiceV2.clearMetrics(48).catch(console.error);
async () => {
await clientMetricsServiceV2.clearMetrics(48).catch(console.error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

},
hoursToMilliseconds(12),
'clearMetrics',
Expand Down
2 changes: 1 addition & 1 deletion src/lib/features/scheduler/scheduler-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class SchedulerService {
}

async schedule(
scheduledFunction: () => void,
scheduledFunction: () => Promise<unknown>,
timeMs: number,
id: string,
jitter = randomJitter(2 * 1000, 30 * 1000, timeMs),
Expand Down