Skip to content

Commit

Permalink
feat: port a lot of functionality from bull 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Feb 10, 2019
1 parent b0b2863 commit ec9f3d2
Show file tree
Hide file tree
Showing 54 changed files with 5,329 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ services:
- redis-server

script:
- npm run prettier -- --list-different
- npm run test
- yarn prettier -- --list-different
- yarn test

after_script:
- npm run coveralls
Expand All @@ -29,4 +29,4 @@ jobs:
provider: script
skip_cleanup: true
script:
- npx semantic-release
- npx semantic-release
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ const priorityQueue = new PriorityQueue('name', {});

```





# Idea for delayed jobs

A delayed job is placed in the queue, with the given timestamp. Queue works as normally.
When the delayed job reaches the tip of the queue, the diff between the created timestamp and the current timestap is calculated and if it is larger or equal than the delay it is executed, otherwise placed on the delayed set.

29 changes: 27 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,40 @@
"scripts": {
"build": "tsc",
"lint": "tslint --project tsconfig.json -c tslint.json 'src/**/*.ts'",
"test": "yarn lint && tsc && mocha './dist/**/*.spec.js' --exit",
"test": "yarn lint && tsc && ts-mocha --paths src/**/test_*.ts --exit",
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"prettier": "prettier --config package.json --write '**/*.js'",
"precommit": "yarn prettier && yarn lint && yarn test",
"semantic-release": "semantic-release"
},
"devDependencies": {
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"@types/bluebird": "^3.5.25",
"@types/chai": "^4.1.7",
"@types/ioredis": "^4.0.4",
"@types/lodash": "^4.14.119",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.18",
"@types/node-uuid": "^0.0.28",
"chai": "^4.2.0",
"coveralls": "^3.0.2",
"istanbul": "^0.4.5",
"lodash": "^4.17.11",
"mocha": "^5.2.0",
"mocha-lcov-reporter": "^1.3.0",
"prettier": "^1.15.3",
"semantic-release": "^15.13.2",
"sinon": "^7.2.2",
"ts-mocha": "^2.0.0",
"tslint": "^5.12.0",
"tslint-eslint-rules": "^5.4.0",
"typescript": "^3.2.2"
},
"prettier": {
"singleQuote": true
"singleQuote": true,
"trailingComma": "all",
"printWidth": 80
},
"husky": {
"hooks": {
Expand All @@ -37,5 +54,13 @@
"repository": {
"type": "git",
"url": "https://github.com/taskforcesh/bull.git"
},
"dependencies": {
"@types/semver": "^5.5.0",
"bluebird": "^3.5.3",
"cron-parser": "^2.7.3",
"ioredis": "^4.3.0",
"node-uuid": "^1.4.8",
"semver": "^5.6.0"
}
}
68 changes: 68 additions & 0 deletions src/classes/backoffs.ts
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.`,
);
}
}
5 changes: 5 additions & 0 deletions src/classes/index.ts
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';
Loading

0 comments on commit ec9f3d2

Please sign in to comment.