Skip to content

Commit a88b87e

Browse files
authored
perf(scheduler): fast-path legacy key shape detection
1 parent 7c943b9 commit a88b87e

1 file changed

Lines changed: 49 additions & 6 deletions

File tree

src/classes/job-scheduler.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,62 @@ export const LEGACY_REPEATABLE_JOBS_MIGRATION_URL =
2525
* - a cron pattern (contains spaces), or
2626
* - an `every` interval (purely numeric).
2727
*/
28+
/**
29+
* Returns true when key[from..to) is composed only of ASCII digits.
30+
*/
31+
function isNumericSegment(key: string, from: number, to: number): boolean {
32+
if (from >= to) {
33+
return false;
34+
}
35+
36+
for (let i = from; i < to; i++) {
37+
const charCode = key.charCodeAt(i);
38+
if (charCode < 48 || charCode > 57) {
39+
return false;
40+
}
41+
}
42+
43+
return true;
44+
}
2845
export function hasLegacyRepeatableKeyShape(key: string): boolean {
29-
const parts = key.split(':');
30-
if (parts.length < 5) {
46+
const firstColon = key.indexOf(':');
47+
if (firstColon === -1) {
48+
return false;
49+
}
50+
51+
const secondColon = key.indexOf(':', firstColon + 1);
52+
if (secondColon === -1) {
3153
return false;
3254
}
3355

34-
const legacyEndDate = parts[2];
35-
if (legacyEndDate !== '' && !/^\d+$/.test(legacyEndDate)) {
56+
const thirdColon = key.indexOf(':', secondColon + 1);
57+
if (thirdColon === -1) {
3658
return false;
3759
}
3860

39-
const legacySuffix = parts.slice(4).join(':');
40-
return legacySuffix.includes(' ') || /^\d+$/.test(legacySuffix);
61+
const fourthColon = key.indexOf(':', thirdColon + 1);
62+
if (fourthColon === -1) {
63+
return false;
64+
}
65+
66+
// endDate can be empty or numeric in legacy keys.
67+
if (
68+
secondColon + 1 < thirdColon &&
69+
!isNumericSegment(key, secondColon + 1, thirdColon)
70+
) {
71+
return false;
72+
}
73+
74+
const suffixStart = fourthColon + 1;
75+
if (suffixStart >= key.length) {
76+
return false;
77+
}
78+
79+
if (key.indexOf(' ', suffixStart) !== -1) {
80+
return true;
81+
}
82+
83+
return isNumericSegment(key, suffixStart, key.length);
4184
}
4285

4386
export const isLegacyRepeatableJobKey = hasLegacyRepeatableKeyShape;

0 commit comments

Comments
 (0)