mentie 0.1.13 → 0.1.15
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/modules/promises.js +18 -8
- package/package.json +1 -1
package/modules/promises.js
CHANGED
|
@@ -6,20 +6,20 @@
|
|
|
6
6
|
* @param {number} [options.cooldown_in_s=10] - The cooldown time in seconds between retries.
|
|
7
7
|
* @param {boolean} [options.cooldown_entropy=true] - Whether to add randomness to the cooldown time.
|
|
8
8
|
* @param {Function} [options.logger=null] - The logger function to log retry attempts.
|
|
9
|
-
* @returns {Function} - The retryable function.
|
|
9
|
+
* @returns {Promise<Function>} - The retryable function.
|
|
10
10
|
* @example
|
|
11
11
|
* make_retryable( do_thing )
|
|
12
12
|
* make_retryable( () => fetch( 'https://api.com/data' ) )
|
|
13
13
|
* @see {@link https://www.npmjs.com/package/promise-retry}
|
|
14
14
|
*/
|
|
15
|
-
export async function make_retryable( async_function, { retry_times=5, cooldown_in_s=10, cooldown_entropy=true, logger=null } ) {
|
|
15
|
+
export async function make_retryable( async_function, { retry_times = 5, cooldown_in_s = 10, cooldown_entropy = true, logger = null } ) {
|
|
16
16
|
|
|
17
17
|
// Function dependencies
|
|
18
18
|
const { default: Retrier } = await import( 'promise-retry' )
|
|
19
19
|
const { wait } = await import( './time.js' )
|
|
20
20
|
|
|
21
21
|
// Set a default logger that does nothing if none was provided
|
|
22
|
-
if( !logger ) logger = () => {}
|
|
22
|
+
if( !logger ) logger = () => { }
|
|
23
23
|
|
|
24
24
|
// Formulate retry logic
|
|
25
25
|
const retryable_function = () => Retrier( ( do_retry, retry_counter ) => {
|
|
@@ -34,7 +34,7 @@ export async function make_retryable( async_function, { retry_times=5, cooldown_
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
// If retries left, retry with a progressive delay
|
|
37
|
-
const entropy = !cooldown_entropy ? 0 :
|
|
37
|
+
const entropy = !cooldown_entropy ? 0 : .1 + Math.random() // Add some randomness to cooldown
|
|
38
38
|
const cooldown_in_ms = ( cooldown_in_s + entropy ) * 1000 // Convert cooldown to milliseconds
|
|
39
39
|
const cooldown = cooldown_in_ms + cooldown_in_ms * ( retry_counter - 1 ) // Increase cooldown with each retry
|
|
40
40
|
|
|
@@ -69,16 +69,25 @@ export async function make_retryable( async_function, { retry_times=5, cooldown_
|
|
|
69
69
|
* @returns {Promise<Array>} - A promise that resolves to an array of results from the async functions.
|
|
70
70
|
* @see {@link https://www.npmjs.com/package/promise-parallel-throttle}
|
|
71
71
|
*/
|
|
72
|
-
export async function throttle_and_retry( async_function_array=[], { max_parallel=2, retry_times, cooldown_in_s, cooldown_entropy, logger, fail_fast=true } ) {
|
|
72
|
+
export async function throttle_and_retry( async_function_array = [], { max_parallel = 2, retry_times, cooldown_in_s, cooldown_entropy, logger, fail_fast = true } ) {
|
|
73
73
|
|
|
74
74
|
// Function dependencies
|
|
75
75
|
const { default: Throttle } = await import( 'promise-parallel-throttle' )
|
|
76
76
|
|
|
77
|
+
// Check that async_function_array is an array with functions in it
|
|
78
|
+
if( !Array.isArray( async_function_array ) || !async_function_array.every( f => typeof f === 'function' ) ) {
|
|
79
|
+
throw new Error( 'async_function_array must be an array of functions' )
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Set a default logger that does nothing if none was provided
|
|
83
|
+
if( !logger ) logger = () => { }
|
|
84
|
+
|
|
77
85
|
// Create array of retryable functions
|
|
78
|
-
|
|
79
|
-
|
|
86
|
+
logger( `Creating ${ async_function_array.length } retryable functions` )
|
|
87
|
+
const retryable_async_functions = await Promise.all( async_function_array.map( async async_function => {
|
|
88
|
+
const retryable_function = await make_retryable( async_function, { retry_times, cooldown_in_s, logger, cooldown_entropy } )
|
|
80
89
|
return retryable_function
|
|
81
|
-
} )
|
|
90
|
+
} ) )
|
|
82
91
|
|
|
83
92
|
// Throttle configuration
|
|
84
93
|
const throttle_config = {
|
|
@@ -88,6 +97,7 @@ export async function throttle_and_retry( async_function_array=[], { max_paralle
|
|
|
88
97
|
}
|
|
89
98
|
|
|
90
99
|
// Return throttler
|
|
100
|
+
logger( `Starting queue of ${ async_function_array.length } functions with options: `, throttle_config )
|
|
91
101
|
return Throttle.all( retryable_async_functions, throttle_config )
|
|
92
102
|
|
|
93
103
|
}
|