@sohanemon/utils 5.1.7 → 5.1.8
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.
|
@@ -6,6 +6,7 @@ interface ScheduleOpts {
|
|
|
6
6
|
/**
|
|
7
7
|
* Runs a function asynchronously in the background.
|
|
8
8
|
* Returns immediately, retries on failure if configured.
|
|
9
|
+
* Logs total time taken.
|
|
9
10
|
*/
|
|
10
11
|
export declare function schedule(task: Task, options?: ScheduleOpts): void;
|
|
11
12
|
export {};
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Runs a function asynchronously in the background.
|
|
3
3
|
* Returns immediately, retries on failure if configured.
|
|
4
|
+
* Logs total time taken.
|
|
4
5
|
*/
|
|
5
6
|
export function schedule(task, options = {}) {
|
|
6
7
|
const { retry = 0, delay = 0 } = options;
|
|
8
|
+
const start = Date.now();
|
|
7
9
|
const attempt = async (triesLeft) => {
|
|
8
10
|
try {
|
|
9
11
|
await task();
|
|
12
|
+
const total = Date.now() - start;
|
|
13
|
+
console.log(`⚡[schedule.ts] Completed in ${total}ms`);
|
|
10
14
|
}
|
|
11
15
|
catch (err) {
|
|
12
16
|
console.log('⚡[schedule.ts] err:', err);
|
|
@@ -14,6 +18,10 @@ export function schedule(task, options = {}) {
|
|
|
14
18
|
console.log(`⚡[schedule.ts] Retrying in ${delay}ms...`);
|
|
15
19
|
setTimeout(() => attempt(triesLeft - 1), delay);
|
|
16
20
|
}
|
|
21
|
+
else {
|
|
22
|
+
const total = Date.now() - start;
|
|
23
|
+
console.log(`⚡[schedule.ts] Failed after ${total}ms`);
|
|
24
|
+
}
|
|
17
25
|
}
|
|
18
26
|
};
|
|
19
27
|
// Schedule immediately
|