You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-10Lines changed: 34 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,19 @@ It controls the complete lifecycle of tasks and their promises by:
9
9
10
10
This class is particularly well-suited for scenarios that demand precise control over the execution and termination of asynchronous tasks. It is an extension of the [delayed-async-task](https://www.npmjs.com/package/delayed-async-task) package, offering broader capabilities for managing multiple tasks.
11
11
12
-
## Key Features :sparkles:
12
+
## Table of Contents
13
+
14
+
*[Key Features](#key-features)
15
+
*[API](#api)
16
+
*[Execution Status Getters](#execution-status-getters)
17
+
*[Use Case Example: Security Incident Response System](#use-case-example)
18
+
*[Design Decision: Task Manager per Use Case](#design-decision)
19
+
*[Graceful and Deterministic Termination](#graceful-termination)
## Key Features :sparkles:<aid="key-features"></a>
13
25
14
26
*__Modern Substitute for Javascript's 'setTimeout'__: Specifically designed for scheduling asynchronous tasks, i.e., callbacks that return a Promise.
15
27
*__Execution Status Getters__: Allows users to check the task's execution status, helping to prevent potential race conditions.
@@ -23,7 +35,7 @@ This class is particularly well-suited for scenarios that demand precise control
23
35
* ES2020 Compatibility.
24
36
* TypeScript support.
25
37
26
-
## API :globe_with_meridians:
38
+
## API :globe_with_meridians:<aid="api"></a>
27
39
28
40
The `DelayedAsyncTasksManager` class provides the following methods:
29
41
@@ -39,19 +51,21 @@ The `DelayedAsyncTasksManager` class provides the following methods:
39
51
40
52
If needed, refer to the code documentation for a more comprehensive description of each method.
41
53
42
-
## Execution Status Getters :mag:
54
+
## Execution Status Getters :mag:<aid="execution-status-getters"></a>
43
55
44
56
The `DelayedAsyncTasksManager` class provides the following getter methods to reflect the current manager's state:
45
57
46
58
*__pendingTasksCount__: The number of tasks in a pending state. i.e., managed tasks which did not start their execution yet.
47
59
*__currentlyExecutingTasksCount__: The number of tasks managed by this instance that are currently executing, i.e., tasks that are neither pending nor completed.
48
60
*__uncaughtErrorsCount__: The number of uncaught task errors, that are currently stored by the instance. These errors have not yet been extracted using `extractUncaughtErrors`. Knowing the number of uncaught errors allows users to decide whether to process them immediately or wait for further accumulation.
49
61
50
-
## Use Case Example: Security Incident Response System :man_technologist:
62
+
To eliminate any ambiguity, all getter methods have **O(1)** time and space complexity, meaning they do **not** iterate through all currently managed tasks with each call. The metrics are maintained by the tasks themselves.
51
63
52
-
Consider a Security Incident Response System, that schedules delayed actions such as disabling compromised user accounts, revoking access tokens, or blocking suspicious IP addresses. Each task is associated with a unique key (e.g., user ID, token ID), enabling security teams to **delay and manage responses based on evolving threat intelligence**. Tasks can be **canceled or adjusted if the threat is mitigated** before the action is triggered.
64
+
## Use Case Example: Security Incident Response System:man_technologist:<aid="use-case-example"></a>
53
65
54
-
In real-world scenarios, responses to security incidents are often immediate to minimize damage. However, delayed tasks could be applicable in cases where actions aren't taken immediately **to gather more context or prevent premature actions** based on incomplete information. For example, the delay could be used to notify administrators or confirm suspicious behavior before taking disruptive measures, like blocking access or disabling accounts.
66
+
Consider a Security Incident Response System, that schedules delayed actions such as disabling compromised user accounts, revoking access tokens, or blocking suspicious IP addresses. Each task is associated with a unique key (e.g., user ID, token ID), enabling security teams to **delay and manage responses** based on evolving threat intelligence. Tasks can be **canceled or adjusted** if the threat is mitigated before the action is triggered.
67
+
68
+
In real-world scenarios, responses to security incidents are often immediate to minimize damage. However, delayed tasks could be applicable in cases where actions aren't taken immediately to gather more context or prevent premature actions based on incomplete information. For example, the delay could be used to notify administrators or confirm suspicious behavior before taking disruptive measures, like blocking access or disabling accounts.
55
69
56
70
Please note that this example is overly simplified. Real-world usage examples can be more complex, often involving persistency and synchronization with external resources.
57
71
@@ -111,7 +125,17 @@ class IncidentResponseSystem {
111
125
}
112
126
```
113
127
114
-
## Graceful and Deterministic Termination :hourglass:
128
+
## Design Decision: Task Manager per Use Case<aid="design-decision"></a>
129
+
130
+
Separating code into small, single-responsibility building blocks improves testability and readability. While it may seem simpler to use a single scheduler as a 'single source of truth' for all task types, this approach can lead to increased complexity as the application scales.
131
+
132
+
For instance, the *Incident Response System* code example above could benefit from employing two task managers instead of one:
133
+
* Enable Account Manager
134
+
* Disable Account Manager
135
+
136
+
One benefit is the ability to gather operation-specific metrics, such as periodically sampling the number of pending Disable Account actions through the `pendingTasksCount` getter.
137
+
138
+
## Graceful and Deterministic Termination :hourglass:<aid="graceful-termination"></a>
115
139
116
140
In the context of asynchronous tasks and schedulers, graceful and deterministic termination is **often overlooked**. `DelayedAsyncTasksManager` provides an out-of-the-box mechanism to await the completion of an asynchronous task that has already started but has not yet finished, using either the `awaitTaskCompletion` or `awaitCompletionOfAllCurrentlyExecutingTasks` method.
117
141
@@ -207,13 +231,13 @@ class Component {
207
231
208
232
Another scenario where this feature is highly recommended is when a schedule might be aborted, such as in an abort-and-reschedule situation. If the task is currently executing (which can be checked via the `isExecuting` method), it cannot be aborted. In such cases, you can ignore the reschedule request, await the current execution to complete using `awaitTaskCompletion`, or implement any other business logic that suits your requirements.
Unlike `setTimeout` in Node.js, where errors from rejected promises propagate to the event loop and trigger an `uncaughtRejection` event, this package offers robust error handling:
219
243
@@ -222,6 +246,6 @@ Unlike `setTimeout` in Node.js, where errors from rejected promises propagate to
222
246
223
247
Ideally, a delayed task should handle its own errors and **avoid** throwing uncaught exceptions.
Copy file name to clipboardExpand all lines: package.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
{
2
2
"name": "delayed-async-tasks-manager",
3
-
"version": "1.0.0",
4
-
"description": "A scheduler for one-time (non-periodic) asynchronous tasks, designed to manage delayed executions. Features status getters to communicate the execution status, the ability to abort a pending execution, and the option to gracefully await the completion of all ongoing executions or a specific task.",
3
+
"version": "1.0.1",
4
+
"description": "A scheduler for one-time (non-periodic) asynchronous tasks, designed to manage delayed executions. It features status getters to communicate the execution state, a robust error handling mechanism for capturing uncaught errors, and the ability to abort pending executions. Additionally, it offers the option to gracefully await the completion of all ongoing tasks or a specific task.",
0 commit comments