-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserverTaskWaiter.ts
91 lines (75 loc) Β· 3.38 KB
/
serverTaskWaiter.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
import { Client } from "../..";
import { ServerTask } from "../../features/serverTasks";
import { SpaceServerTaskRepository } from "../serverTasks";
export class ServerTaskWaiter {
constructor(private readonly client: Client, private readonly spaceName: string) {}
async waitForServerTasksToComplete(
serverTaskIds: string[],
statusCheckSleepCycle: number,
timeout: number,
pollingCallback?: (serverTask: ServerTask) => void
): Promise<ServerTask[]> {
const spaceServerTaskRepository = new SpaceServerTaskRepository(this.client, this.spaceName);
return this.waitForTasks(spaceServerTaskRepository, serverTaskIds, statusCheckSleepCycle, timeout, pollingCallback);
}
async waitForServerTaskToComplete(
serverTaskId: string,
statusCheckSleepCycle: number,
timeout: number,
pollingCallback?: (serverTask: ServerTask) => void
): Promise<ServerTask> {
const spaceServerTaskRepository = new SpaceServerTaskRepository(this.client, this.spaceName);
const tasks = await this.waitForTasks(spaceServerTaskRepository, [serverTaskId], statusCheckSleepCycle, timeout, pollingCallback);
return tasks[0];
}
private async waitForTasks(
spaceServerTaskRepository: SpaceServerTaskRepository,
serverTaskIds: string[],
statusCheckSleepCycle: number,
timeout: number,
pollingCallback?: (serverTask: ServerTask) => void
): Promise<ServerTask[]> {
// short circuit if no ids are passed. Sending an empty array to server here
// doesn't do what you may expect. To server, no ids == return every task the user
// has permission to see
if (serverTaskIds.length === 0) {
return [];
}
const sleep = async (ms: number) => new Promise((r) => setTimeout(r, ms));
let stop = false;
const t = setTimeout(() => {
stop = true;
}, timeout);
const completedTasks: ServerTask[] = [];
while (!stop) {
try {
const tasks = await spaceServerTaskRepository.getByIds(serverTaskIds);
const unknownTaskIds = serverTaskIds.filter((id) => tasks.filter((t) => t.Id === id).length == 0);
if (unknownTaskIds.length) {
throw new Error(`Unknown task Id(s) ${unknownTaskIds.join(", ")}`);
}
const nowCompletedTaskIds: string[] = [];
for (const task of tasks) {
if (pollingCallback) {
pollingCallback(task);
}
// once the task is complete
if (task.IsCompleted) {
nowCompletedTaskIds.push(task.Id);
completedTasks.push(task);
}
}
// filter down the ids to only those that haven't completed for the next time around the loop
serverTaskIds = serverTaskIds.filter((id) => nowCompletedTaskIds.indexOf(id) < 0);
// once all tasks have completed we can stop the loop
if (serverTaskIds.length === 0 || tasks.length === 0) {
stop = true;
}
} finally {
clearTimeout(t);
}
await sleep(statusCheckSleepCycle);
}
return completedTasks;
}
}