@factorearth/recordmiddleware-errorhandler 0.0.1-alpha.12 → 0.0.1-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/backOff.d.ts CHANGED
@@ -1 +1,28 @@
1
- export declare const backOff: () => Promise<void>;
1
+ interface BackoffOptions {
2
+ /**
3
+ * The initial delay you want to apply. Default to 100ms
4
+ */
5
+ delay?: number;
6
+ /**
7
+ * The number of times you want to attempt the function. Default 10
8
+ */
9
+ numberOfAttempts?: number;
10
+ /**
11
+ * An optional max delay
12
+ */
13
+ maxDelay?: number;
14
+ /**
15
+ * An initial multiplier to be taken to the power of the attempt number when deciding how long to wait. Default value of 2
16
+ */
17
+ timeMultiple?: number;
18
+ }
19
+ /**
20
+ * Given a task, attempts to execute it a designated number of times, returning the eventual response
21
+ * @author Eric Webb <ewebb@factorearth.com>
22
+ * @param task The task we want to retry potentially
23
+ * @param evaluator A function to evaluate if the task executed successfully, or if it needs to be retried. Needs to return boolean
24
+ * @param options The configurable options for the module
25
+ * @returns The result of the function
26
+ */
27
+ export declare function backoff<TaskResult, TaskArguments extends any[]>(task: (...args: TaskArguments) => Promise<TaskResult>, evaluator: (taskResult: TaskResult) => boolean, options?: BackoffOptions, ...taskArguments: TaskArguments): Promise<TaskResult | void>;
28
+ export {};
package/dist/backOff.js CHANGED
@@ -1,2 +1,37 @@
1
- export const backOff = async () => { };
1
+ import { delay } from "./delay";
2
+ // Default values
3
+ const DELAY = 100;
4
+ const NUMBER_OF_ATTEMPTS = 10;
5
+ const MAX_DELAY = Infinity;
6
+ const TIME_MULTIPLE = 2;
7
+ /**
8
+ * Given a task, attempts to execute it a designated number of times, returning the eventual response
9
+ * @author Eric Webb <ewebb@factorearth.com>
10
+ * @param task The task we want to retry potentially
11
+ * @param evaluator A function to evaluate if the task executed successfully, or if it needs to be retried. Needs to return boolean
12
+ * @param options The configurable options for the module
13
+ * @returns The result of the function
14
+ */
15
+ export async function backoff(task, evaluator, options, ...taskArguments) {
16
+ let attemptNumber = 0;
17
+ const maxAttempts = options?.numberOfAttempts || NUMBER_OF_ATTEMPTS;
18
+ const initialDelay = options?.delay || DELAY;
19
+ const maxDelay = options?.maxDelay || MAX_DELAY;
20
+ const timeMultiple = options?.timeMultiple || TIME_MULTIPLE;
21
+ while (attemptNumber < maxAttempts) {
22
+ const calculatedDelay = initialDelay * Math.pow(timeMultiple, attemptNumber);
23
+ const delayTime = Math.min(calculatedDelay, maxDelay);
24
+ await delay(delayTime);
25
+ try {
26
+ const taskResult = await task(...taskArguments);
27
+ const evaluatorResult = evaluator(taskResult);
28
+ if (evaluatorResult || !(attemptNumber + 1 < maxAttempts))
29
+ return taskResult;
30
+ }
31
+ catch (err) {
32
+ // recordSentry({ error: err, msg: "Error executing backoff task" });
33
+ }
34
+ attemptNumber++;
35
+ }
36
+ }
2
37
  //# sourceMappingURL=backOff.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"backOff.js","sourceRoot":"","sources":["../lib/backOff.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC"}
1
+ {"version":3,"file":"backOff.js","sourceRoot":"","sources":["../lib/backOff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAqBhC,iBAAiB;AACjB,MAAM,KAAK,GAAG,GAAG,CAAC;AAClB,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAC9B,MAAM,SAAS,GAAG,QAAQ,CAAC;AAC3B,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAA0C,IAAqD,EAAE,SAA8C,EAAE,OAAwB,EAAE,GAAG,aAA4B;IACtO,IAAI,aAAa,GAAG,CAAC,CAAC;IACtB,MAAM,WAAW,GAAG,OAAO,EAAE,gBAAgB,IAAI,kBAAkB,CAAC;IACpE,MAAM,YAAY,GAAG,OAAO,EAAE,KAAK,IAAI,KAAK,CAAC;IAC7C,MAAM,QAAQ,GAAG,OAAO,EAAE,QAAQ,IAAI,SAAS,CAAC;IAChD,MAAM,YAAY,GAAG,OAAO,EAAE,YAAY,IAAI,aAAa,CAAC;IAC5D,OAAO,aAAa,GAAG,WAAW,EAAE,CAAC;QACpC,MAAM,eAAe,GAAG,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;QAC7E,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC;QACtD,MAAM,KAAK,CAAC,SAAS,CAAC,CAAC;QACvB,IAAI,CAAC;YACJ,MAAM,UAAU,GAAe,MAAM,IAAI,CAAC,GAAG,aAAa,CAAC,CAAC;YAC5D,MAAM,eAAe,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC;YAC9C,IAAI,eAAe,IAAI,CAAC,CAAC,aAAa,GAAG,CAAC,GAAG,WAAW,CAAC;gBAAE,OAAO,UAAU,CAAC;QAC9E,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,qEAAqE;QACtE,CAAC;QACD,aAAa,EAAE,CAAC;IACjB,CAAC;AACF,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Creates a promise that resolvers after a provided number of ms
3
+ * @author Eric Webb <ewebb@factorearth.com>
4
+ * @param millis The number of ms you want to sleep
5
+ * @returns A void promise
6
+ */
7
+ export declare function delay(millis: number): Promise<any>;
package/dist/delay.js ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Creates a promise that resolvers after a provided number of ms
3
+ * @author Eric Webb <ewebb@factorearth.com>
4
+ * @param millis The number of ms you want to sleep
5
+ * @returns A void promise
6
+ */
7
+ export function delay(millis) {
8
+ return new Promise((resolve, _reject) => setTimeout(resolve, millis));
9
+ }
10
+ //# sourceMappingURL=delay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delay.js","sourceRoot":"","sources":["../lib/delay.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CAAC,MAAc;IACnC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;AACvE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@factorearth/recordmiddleware-errorhandler",
3
- "version": "0.0.1-alpha.12",
3
+ "version": "0.0.1-alpha.13",
4
4
  "description": "A module for handling CRUD errors in lambda",
5
5
  "author": "madtrx <marlin.makori@gmail.com>",
6
6
  "homepage": "https://github.com/FactorEarth/RecordMiddleware#readme",
@@ -25,5 +25,5 @@
25
25
  "access": "public",
26
26
  "registry": "https://registry.npmjs.org/"
27
27
  },
28
- "gitHead": "8498879d10c3f30d5167ce8fb5b9fe0d21d863e3"
28
+ "gitHead": "ac74f89b3c1db1ce3433cbafc2752b586d4270a0"
29
29
  }