Skip to content

Commit 189f6f5

Browse files
committed
chore: wip
1 parent 0268d7d commit 189f6f5

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

storage/framework/core/path/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ export function scriptsPath(path?: string) {
366366
return frameworkPath(`scripts/${path || ''}`)
367367
}
368368

369+
export function schedulePath() {
370+
return appPath(`Schedule.ts`)
371+
}
372+
369373
export function schedulerPath(path?: string) {
370374
return corePath(`scheduler/${path || ''}`)
371375
}
@@ -523,6 +527,7 @@ export const path = {
523527
signalsPath,
524528
slugPath,
525529
scriptsPath,
530+
schedulePath,
526531
securityPath,
527532
serverPath,
528533
serverlessPath,

storage/framework/core/queue/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ export class Job {
88
rate: JobOptions['rate']
99
tries: JobOptions['tries']
1010
backoff: JobOptions['backoff']
11+
enabled: JobOptions['enabled']
1112

12-
constructor({ name, description, handle, rate, tries, backoff, action }: JobOptions) {
13+
constructor({ name, description, handle, rate, tries, backoff, action, enabled }: JobOptions) {
1314
this.name = name
1415
this.description = description
1516
this.handle = handle
1617
this.rate = rate
1718
this.action = action
1819
this.tries = tries
1920
this.backoff = backoff
21+
this.enabled = enabled
2022
}
2123
}

storage/framework/core/scheduler/src/schedule.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export class Schedule {
2323
return this
2424
}
2525

26+
everyTwoMinutes() {
27+
this.cronPattern = '*/2 * * * * *'
28+
return this
29+
}
30+
2631
everyFiveMinutes() {
2732
this.cronPattern = '*/5 * * * *'
2833
return this

storage/framework/core/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export * from './math'
1616
export * from './p'
1717
export * from './promise'
1818
export * from './regex'
19+
export * from './retry'
1920
export * from './sleep'
2021
export * from './throttle'
2122
export * from './vendors'
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function retry(fn, options) {
2+
const { retries = 3, initialDelay = 1000, backoffFactor = 2, jitter = true } = options
3+
4+
return new Promise((resolve, reject) => {
5+
let attemptCount = 0
6+
const attempt = async () => {
7+
try {
8+
resolve(await fn())
9+
}
10+
catch (err) {
11+
if (attemptCount >= retries) {
12+
reject(err)
13+
}
14+
else {
15+
const delay = calculateDelay(attemptCount, initialDelay, backoffFactor, jitter)
16+
setTimeout(() => attempt(), delay)
17+
attemptCount++
18+
}
19+
}
20+
}
21+
attempt()
22+
})
23+
}
24+
25+
function calculateDelay(attemptCount, initialDelay, backoffFactor, jitter) {
26+
let delay = initialDelay * backoffFactor ** attemptCount
27+
if (jitter) {
28+
const random = Math.random() // Generates a number between 0 and 1
29+
const jitterValue = delay * 0.3 // Jitter will be up to 30% of the delay
30+
delay = delay + jitterValue * (random - 0.5) * 2 // Adjust delay randomly within ±30%
31+
}
32+
return delay
33+
}

0 commit comments

Comments
 (0)