/
TaskRuntimeContainer.ts
106 lines (73 loc) · 1.83 KB
/
TaskRuntimeContainer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import * as _ from 'lodash';
import {TaskRun} from './TaskRun';
import {ITaskRuntimeContainer} from './ITaskRuntimeContainer';
import {TaskRuntimeLogger} from './TaskRuntimeLogger';
import {TaskRunner} from './TaskRunner';
import {ILoggerApi} from '../logging/ILoggerApi';
import {TaskState} from './TaskState';
export class TaskRuntimeContainer implements ITaskRuntimeContainer {
private readonly $_run_: TaskRun;
private $progress = 0;
private $total = 100;
private _logger: ILoggerApi;
constructor(taskRun: TaskRun) {
this.$_run_ = taskRun;
this.$total = 100;
}
/**
* Get internal statistic counter
*
* @param key
*/
counter(key: string) {
return this.$_run_.status.counters.get(key);
}
logger() {
if (!this._logger) {
this._logger = new TaskRuntimeLogger(
this.getRunner().id,
this.$_run_.taskRef().name,
this.$_run_.nr,
this.getRunner().getLogger());
}
return this._logger;
}
private getRunner(): TaskRunner {
return this.$_run_.getRunner();
}
addTask(name: string, incomings?: any): Promise<TaskState> {
const taskRun = this.$_run_.getRunner().createTaskRun(name, incomings);
this.$_run_.getRunner().enqueue(taskRun);
return taskRun.asPromise();
}
progress(nr: number) {
this.$progress = nr;
this.$_run_.update();
}
total(nr: number) {
this.$total = nr;
}
done() {
this.$progress = this.$total;
}
get name() {
return this.$_run_.taskRef().name;
}
get id() {
return this.$_run_.nr;
}
get taskNr() {
return this.$_run_.nr;
}
get runnerId() {
return this.$_run_.getRunner().id;
}
stats() {
const obj = this.$_run_.status.counters.asObject();
_.assign(obj, {
progress: this.$progress,
total: this.$total
});
return obj;
}
}