-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackupCreator.ts
144 lines (125 loc) · 3.82 KB
/
backupCreator.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {CreateStatus} from "./consts";
import {validateBackend, validateBackupId, validateExcludeClassNames, validateIncludeClassNames} from "./validation";
import BackupCreateStatusGetter from "./backupCreateStatusGetter";
import Connection from "../connection";
import {CommandBase} from "../validation/commandBase";
const WAIT_INTERVAL = 1000;
export default class BackupCreator extends CommandBase {
private backend?: string;
private backupId?: string;
private excludeClassNames?: string[];
private includeClassNames?: string[];
private statusGetter: BackupCreateStatusGetter;
private waitForCompletion!: boolean;
constructor(client: Connection, statusGetter: BackupCreateStatusGetter) {
super(client)
this.statusGetter = statusGetter;
}
withIncludeClassNames(...classNames: string[]) {
let cls = classNames;
if (classNames.length && Array.isArray(classNames[0])) {
cls = classNames[0];
}
this.includeClassNames = cls;
return this;
}
withExcludeClassNames(...classNames: string[]) {
let cls = classNames;
if (classNames.length && Array.isArray(classNames[0])) {
cls = classNames[0];
}
this.excludeClassNames = cls;
return this;
}
withBackend(backend: string) {
this.backend = backend;
return this;
}
withBackupId(backupId: string) {
this.backupId = backupId;
return this;
}
withWaitForCompletion(waitForCompletion: boolean) {
this.waitForCompletion = waitForCompletion;
return this;
}
validate() {
this.addErrors([
...validateIncludeClassNames(this.includeClassNames),
...validateExcludeClassNames(this.excludeClassNames),
...validateBackend(this.backend),
...validateBackupId(this.backupId),
])
}
do() {
this.validate();
if (this.errors.length > 0) {
return Promise.reject(
new Error("invalid usage: " + this.errors.join(", "))
);
}
const payload = {
id: this.backupId,
config: {},
include: this.includeClassNames,
exclude: this.excludeClassNames,
};
if (this.waitForCompletion) {
return this._createAndWaitForCompletion(payload);
}
return this._create(payload);
}
_create(payload: any) {
return this.client.post(this._path(), payload);
}
_createAndWaitForCompletion(payload: any) {
return new Promise((resolve, reject) => {
this._create(payload)
.then((createResponse: any) => {
this.statusGetter
.withBackend(this.backend!)
.withBackupId(this.backupId!);
const loop = () => {
this.statusGetter.do()
.then((createStatusResponse: any) => {
if (createStatusResponse.status == CreateStatus.SUCCESS
|| createStatusResponse.status == CreateStatus.FAILED
) {
resolve(this._merge(createStatusResponse, createResponse));
} else {
setTimeout(loop, WAIT_INTERVAL);
}
})
.catch(reject);
};
loop();
})
.catch(reject)
});
}
_path() {
return `/backups/${this.backend}`;
}
_merge(createStatusResponse: any, createResponse: any) {
const merged: any = {};
if ('id' in createStatusResponse) {
merged.id = createStatusResponse.id;
}
if ('path' in createStatusResponse) {
merged.path = createStatusResponse.path
}
if ('backend' in createStatusResponse) {
merged.backend = createStatusResponse.backend
}
if ('status' in createStatusResponse) {
merged.status = createStatusResponse.status
}
if ('error' in createStatusResponse) {
merged.error = createStatusResponse.error
}
if ('classes' in createResponse) {
merged.classes = createResponse.classes
}
return merged;
}
}