@vorplex/core 0.0.38 → 0.0.40
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.
|
@@ -3,6 +3,7 @@ export declare const TaskStatus: {
|
|
|
3
3
|
readonly Busy: "busy";
|
|
4
4
|
readonly Failed: "failed";
|
|
5
5
|
readonly Done: "done";
|
|
6
|
+
readonly Cancelled: "cancelled";
|
|
6
7
|
};
|
|
7
8
|
export type TaskStatus = (typeof TaskStatus)[keyof typeof TaskStatus];
|
|
8
9
|
export interface Attachment {
|
|
@@ -24,6 +25,8 @@ export declare class Task extends Subscribable<Task> {
|
|
|
24
25
|
tasks: Task[];
|
|
25
26
|
logs: Log[];
|
|
26
27
|
constructor(name: string, task?: Task);
|
|
28
|
+
isCancelled(): any;
|
|
29
|
+
cancel(): void;
|
|
27
30
|
do<T>(name: string, callback: (task: Task) => Promise<T> | T): Promise<T>;
|
|
28
31
|
static do<T>(name: string, callback: (task: Task) => Promise<T> | T): Promise<T>;
|
|
29
32
|
log(message: string, options?: {
|
|
@@ -9,6 +9,7 @@ export const TaskStatus = {
|
|
|
9
9
|
Busy: 'busy',
|
|
10
10
|
Failed: 'failed',
|
|
11
11
|
Done: 'done',
|
|
12
|
+
Cancelled: 'cancelled'
|
|
12
13
|
};
|
|
13
14
|
export class Task extends Subscribable {
|
|
14
15
|
startTimestamp = Date.now();
|
|
@@ -24,7 +25,19 @@ export class Task extends Subscribable {
|
|
|
24
25
|
this.parent = task;
|
|
25
26
|
this.subscribe(task => task.parent?.emit(this.parent));
|
|
26
27
|
}
|
|
28
|
+
isCancelled() {
|
|
29
|
+
return this.status === TaskStatus.Cancelled || this.parent?.isCancelled() === true;
|
|
30
|
+
}
|
|
31
|
+
cancel() {
|
|
32
|
+
if (this.status === TaskStatus.Busy) {
|
|
33
|
+
this.status = TaskStatus.Cancelled;
|
|
34
|
+
this.finishTimestamp = Date.now();
|
|
35
|
+
this.emit(this);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
27
38
|
async do(name, callback) {
|
|
39
|
+
if (this.isCancelled())
|
|
40
|
+
throw new Error('Task cancelled');
|
|
28
41
|
const task = new Task(name, this);
|
|
29
42
|
this.tasks.push(task);
|
|
30
43
|
this.emit(this);
|
|
@@ -34,7 +47,7 @@ export class Task extends Subscribable {
|
|
|
34
47
|
return result;
|
|
35
48
|
}
|
|
36
49
|
catch (error) {
|
|
37
|
-
task.fail();
|
|
50
|
+
task.fail(error);
|
|
38
51
|
throw error;
|
|
39
52
|
}
|
|
40
53
|
}
|
|
@@ -52,6 +65,8 @@ export class Task extends Subscribable {
|
|
|
52
65
|
}
|
|
53
66
|
}
|
|
54
67
|
log(message, options) {
|
|
68
|
+
if (this.isCancelled())
|
|
69
|
+
throw new Error('Task cancelled');
|
|
55
70
|
this.logs.push({
|
|
56
71
|
timestamp: Date.now(),
|
|
57
72
|
message,
|
|
@@ -61,32 +76,34 @@ export class Task extends Subscribable {
|
|
|
61
76
|
this.emit(this);
|
|
62
77
|
}
|
|
63
78
|
fail(error) {
|
|
64
|
-
if (this.status
|
|
79
|
+
if (this.status === TaskStatus.Busy) {
|
|
65
80
|
if (error)
|
|
66
81
|
this.log(error instanceof Error ? error.stack ?? error.message : error, { level: 'error' });
|
|
67
|
-
this.status =
|
|
82
|
+
this.status = TaskStatus.Failed;
|
|
68
83
|
this.finishTimestamp = Date.now();
|
|
69
84
|
this.emit(this);
|
|
70
85
|
}
|
|
71
86
|
}
|
|
72
87
|
complete() {
|
|
73
|
-
if (this.status ===
|
|
74
|
-
this.status =
|
|
88
|
+
if (this.status === TaskStatus.Busy) {
|
|
89
|
+
this.status = TaskStatus.Done;
|
|
75
90
|
this.finishTimestamp = Date.now();
|
|
76
91
|
this.emit(this);
|
|
77
92
|
}
|
|
78
93
|
}
|
|
79
94
|
getStatus() {
|
|
80
95
|
let failed;
|
|
96
|
+
if (this.isCancelled())
|
|
97
|
+
return TaskStatus.Cancelled;
|
|
81
98
|
for (const task of this.tasks) {
|
|
82
99
|
const status = task.getStatus();
|
|
83
|
-
if (status ===
|
|
100
|
+
if (status === TaskStatus.Failed)
|
|
84
101
|
failed = true;
|
|
85
|
-
if (status ===
|
|
86
|
-
return
|
|
102
|
+
if (status === TaskStatus.Busy)
|
|
103
|
+
return TaskStatus.Busy;
|
|
87
104
|
}
|
|
88
105
|
if (failed)
|
|
89
|
-
return
|
|
106
|
+
return TaskStatus.Failed;
|
|
90
107
|
return this.status;
|
|
91
108
|
}
|
|
92
109
|
hasWarning() {
|
|
@@ -104,7 +121,7 @@ export class Task extends Subscribable {
|
|
|
104
121
|
toConsoleLog() {
|
|
105
122
|
const formatter = {
|
|
106
123
|
task: (task) => {
|
|
107
|
-
const status = { done: `${Chalk.White}★ [Done]`, busy: `${Chalk.Orange}★ [Busy]`, failed: `${Chalk.Red}★ [Failed]` }[task.getStatus()];
|
|
124
|
+
const status = { done: `${Chalk.White}★ [Done]`, busy: `${Chalk.Orange}★ [Busy]`, failed: `${Chalk.Red}★ [Failed]`, cancelled: `${Chalk.Red}★ [Cancelled]` }[task.getStatus()];
|
|
108
125
|
const date = $Date.format(new Date(task.startTimestamp), '[YYYY-MM-DD hh:mm:ss]');
|
|
109
126
|
const duration = $Number.toUnitString(task.finishTimestamp - task.startTimestamp, Unit.Time);
|
|
110
127
|
return `${status} ${date} ${task.name} ${Chalk.Dim}${duration}${Chalk.Reset}`;
|