Skip to content

Commit e8dadf6

Browse files
chore: wip
1 parent 71bd94f commit e8dadf6

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

app/Jobs/ExampleJob.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export default new Job({
99
backoff: 3, // optional, defaults to 3-second delays between retries
1010
rate: Every.Minute, // optional, '* * * * *' in cron syntax
1111
backoffConfig: {
12-
strategy: 'fixed',
13-
initialDelay: 20000,
12+
strategy: 'linear',
13+
initialDelay: 5000,
14+
factor: 3,
1415
},
1516
handle: (payload: any) => {
1617
throw new HttpError(500, 'test')

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ async function executeJobs(queue: string | undefined): Promise<void> {
8686
}
8787
}
8888

89+
function enforceMaxDelay(maxDelay: number | undefined, delay: number): number {
90+
return maxDelay !== undefined ? Math.min(delay, maxDelay) : delay
91+
}
92+
8993
function addDelay(
9094
timestamp: number | undefined,
9195
currentAttempts: number,
@@ -97,6 +101,7 @@ function addDelay(
97101
const backOff = classPayload.backoff
98102
const backoffConfig = classPayload.backoffConfig
99103
const jitter = backoffConfig?.jitter
104+
const maxDelay = backoffConfig?.maxDelay ? meilisecondsToSeconds(backoffConfig.maxDelay) : undefined
100105

101106
// Fixed backoff strategy logic
102107
if (backoffConfig && backoffConfig.strategy === 'fixed') {
@@ -106,7 +111,7 @@ function addDelay(
106111
delay = applyJitter(delay, jitter)
107112
}
108113

109-
return effectiveTimestamp + delay
114+
return effectiveTimestamp + enforceMaxDelay(maxDelay, delay)
110115
}
111116

112117
// Exponential backoff logic
@@ -117,7 +122,7 @@ function addDelay(
117122
delay = applyJitter(delay, jitter)
118123
}
119124

120-
return effectiveTimestamp + delay
125+
return effectiveTimestamp + enforceMaxDelay(maxDelay, delay)
121126
}
122127

123128
// Linear backoff logic
@@ -128,19 +133,19 @@ function addDelay(
128133
delay = applyJitter(delay, jitter)
129134
}
130135

131-
return effectiveTimestamp + delay
136+
return effectiveTimestamp + enforceMaxDelay(maxDelay, delay)
132137
}
133138

134139
// Backoff as an array of delays (in seconds), convert to milliseconds
135140
if (Array.isArray(backOff)) {
136-
const backOffValueInMeliseconds = backOff[currentAttempts] || 0
141+
const backOffValueInMilliseconds = backOff[currentAttempts] || 0
137142

138143
if (jitter?.enabled) {
139-
const delayWithJitter = applyJitter(backOffValueInMeliseconds, jitter)
140-
return effectiveTimestamp + delayWithJitter
144+
const delayWithJitter = applyJitter(backOffValueInMilliseconds, jitter)
145+
return effectiveTimestamp + enforceMaxDelay(maxDelay, delayWithJitter)
141146
}
142147

143-
return effectiveTimestamp + meilisecondsToSeconds(backOffValueInMeliseconds)
148+
return effectiveTimestamp + enforceMaxDelay(maxDelay, meilisecondsToSeconds(backOffValueInMilliseconds))
144149
}
145150

146151
// Backoff as a single number (exponential or linear backoff), convert to milliseconds
@@ -149,14 +154,14 @@ function addDelay(
149154

150155
if (jitter?.enabled) {
151156
const delayWithJitter = applyJitter(backoffInMilliseconds, jitter)
152-
return effectiveTimestamp + delayWithJitter
157+
return effectiveTimestamp + enforceMaxDelay(maxDelay, delayWithJitter)
153158
}
154-
155-
return effectiveTimestamp + meilisecondsToSeconds(backoffInMilliseconds)
159+
160+
return effectiveTimestamp + enforceMaxDelay(maxDelay, meilisecondsToSeconds(backoffInMilliseconds))
156161
}
157162

158-
// let's default the retry delay to 3 seconds if no backoff is configured
159-
return effectiveTimestamp + 10
163+
// Default to a 10-second retry delay if no backoff is configured
164+
return effectiveTimestamp + enforceMaxDelay(maxDelay, 10)
160165
}
161166

162167
function meilisecondsToSeconds(meiliseconds: number): number {

0 commit comments

Comments
 (0)