-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrequest.service.ts
215 lines (183 loc) · 5.82 KB
/
request.service.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {
Injectable,
OnDestroy,
} from "@angular/core";
import {
Headers,
Http,
RequestOptions,
Response,
} from "@angular/http";
import {
Router,
} from "@angular/router";
import { Observable } from "rxjs";
import "rxjs/Rx";
import {
MatSnackBar,
} from "@angular/material";
// Request represents TypeScript version of Request in https://github.com/gyuho/dplearn/blob/master/backend/web/web.go.
export class Request {
public data_from_frontend: string;
public create_request: boolean;
constructor(
d: string,
create: boolean,
) {
this.data_from_frontend = d;
this.create_request = create;
}
}
// Item represents TypeScript version of Item in https://github.com/gyuho/dplearn/blob/master/pkg/etcd-queue/queue.go.
export class Item {
public bucket: string;
public created_at: string;
public key: string;
public value: string;
public progress: number;
public canceled: boolean;
public error: string;
public request_id: string;
constructor(
bucket: string,
key: string,
value: string,
progress: number,
error: string,
reqID: string,
) {
this.bucket = bucket;
this.created_at = "";
this.key = key;
this.value = value;
this.progress = progress;
this.canceled = false;
this.error = error;
this.request_id = reqID;
}
}
@Injectable()
export class BackendService implements OnDestroy {
public endpoint = "";
public inputValue: string;
public result: string;
public progress = 0;
public spinnerColor = "primary";
public spinnerMode = "indeterminate";
private errorFromServer = "";
private mode = "Observable";
private requestID: string;
private needInterval: boolean;
private pollingHandler;
private url: string;
constructor(
private router: Router,
private http: Http,
private snackBar: MatSnackBar,
) {
this.inputValue = "";
this.result = "Nothing to show yet...";
this.needInterval = false;
this.url = router.url;
}
public ngOnDestroy() {
console.log("user left page; destroying!", this.url);
this.needInterval = false;
clearInterval(this.pollingHandler);
const body = JSON.stringify(new Request(this.inputValue, false));
const headers = new Headers({"Content-Type" : "application/json"});
const options = new RequestOptions({headers});
// this.http.post().map().catch() returns 'Observable<Item>'
// which is non-blocking, 'subscribe' is the blocking operation
let itemFromServer: Item;
this.http.post(this.endpoint, body, options)
.map(this.processHTTPResponseClient)
.catch(this.processHTTPErrorClient)
.subscribe(
(resp) => itemFromServer = resp,
(error) => this.errorFromServer = error as any,
() => this.processItemFromServer(itemFromServer), // on-complete
);
this.inputValue = "";
this.result = "Nothing to show yet...";
this.progress = 0;
this.errorFromServer = "";
console.log("user left page; destroyed!", this.url);
return;
}
public processItemFromServer(resp: Item) {
this.result = resp.value;
this.requestID = resp.request_id;
// set interval only after first response
if (this.needInterval) {
this.needInterval = false;
this.pollingHandler = setInterval(() => this.fetchStatus(), 500);
}
if (resp.error !== "") {
clearInterval(this.pollingHandler);
this.result = (this.result === "") ? resp.error : `${resp.value} (${resp.error})`;
}
if (resp.canceled === true) {
clearInterval(this.pollingHandler);
this.result += " - canceled!";
}
this.progress = resp.progress;
if (this.progress === 100) {
clearInterval(this.pollingHandler);
}
}
public processHTTPResponseClient(res: Response) {
return (res.json() as Item) || {};
}
public processHTTPErrorClient(error: any) {
const errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : "Server error";
console.error(errMsg);
this.errorFromServer = errMsg;
return Observable.throw(errMsg);
}
// fetchStatus requests status updates from backend server.
// Blocks until the item is completed.
// TODO: time out?
public fetchStatus() {
const headers = new Headers({
"Content-Type" : "application/plain",
"Request-Id" : this.requestID,
});
const options = new RequestOptions({headers});
let itemFromServer: Item;
this.http.get(this.endpoint, options)
.map(this.processHTTPResponseClient)
.catch(this.processHTTPErrorClient)
.subscribe(
(resp) => itemFromServer = resp,
(error) => this.errorFromServer = error as any,
() => this.processItemFromServer(itemFromServer), // on-complete
);
}
public clickPOST() {
this.snackBar.open("Job scheduled! Waiting...", "Requested!", {
duration: 2000,
});
this.progress = 0;
this.result = `[FRONTEND - ACK] Requested '${this.inputValue}' (request ID: ${this.requestID})`;
const body = JSON.stringify(new Request(this.inputValue, true));
const headers = new Headers({"Content-Type" : "application/json"});
const options = new RequestOptions({headers});
this.needInterval = true;
// this.http.post().map().catch() returns 'Observable<Item>'
// which is non-blocking, 'subscribe' is the blocking operation
let itemFromServer: Item;
this.http.post(this.endpoint, body, options)
.map(this.processHTTPResponseClient)
.catch(this.processHTTPErrorClient)
.subscribe(
(resp) => itemFromServer = resp,
(error) => this.errorFromServer = error as any,
() => this.processItemFromServer(itemFromServer), // on-complete
);
// retry in case of network glitches
// DO NOT DO THIS because this.http.post is asynchronous
// this.pollingHandler = setInterval(() => this.fetchStatus(), 500);
}
}