-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: port a lot of functionality from bull 3.x
- Loading branch information
Showing
54 changed files
with
5,329 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { BackoffOpts } from '../interfaces/backoff-opts'; | ||
|
||
interface BuiltInStrategies { | ||
[index: string]: (delay: number) => BackoffFunction; | ||
} | ||
|
||
export interface Strategies { | ||
[index: string]: BackoffFunction; | ||
} | ||
|
||
export type BackoffFunction = (attemptsMade?: number, err?: Error) => number; | ||
|
||
export class Backoffs { | ||
static builtinStrategies: BuiltInStrategies = { | ||
fixed: function(delay: number) { | ||
return function() { | ||
return delay; | ||
}; | ||
}, | ||
|
||
exponential: function(delay: number) { | ||
return function(attemptsMade: number) { | ||
return Math.round((Math.pow(2, attemptsMade) - 1) * delay); | ||
}; | ||
}, | ||
}; | ||
|
||
static normalize(backoff: number | BackoffOpts): BackoffOpts { | ||
if (Number.isFinite(<number>backoff)) { | ||
return { | ||
type: 'fixed', | ||
delay: <number>backoff, | ||
}; | ||
} else if (backoff) { | ||
return <BackoffOpts>backoff; | ||
} | ||
} | ||
|
||
static calculate( | ||
backoff: BackoffOpts, | ||
attemptsMade: number, | ||
customStrategies: Strategies, | ||
err: Error, | ||
) { | ||
if (backoff) { | ||
const strategy = lookupStrategy(backoff, customStrategies); | ||
|
||
return strategy(attemptsMade, err); | ||
} | ||
} | ||
} | ||
|
||
function lookupStrategy( | ||
backoff: BackoffOpts, | ||
customStrategies: Strategies, | ||
): BackoffFunction { | ||
if (backoff.type in (customStrategies || {})) { | ||
return customStrategies[backoff.type]; | ||
} else if (backoff.type in Backoffs.builtinStrategies) { | ||
return Backoffs.builtinStrategies[backoff.type](backoff.delay); | ||
} else { | ||
throw new Error( | ||
`Unknown backoff strategy ${ | ||
backoff.type | ||
}. If a custom backoff strategy is used, specify it when the queue is created.`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './queue'; | ||
export * from './job'; | ||
export * from './redis-connection'; | ||
export * from './scripts'; | ||
export * from './backoffs'; |
Oops, something went wrong.