-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskbridge.js
More file actions
145 lines (127 loc) · 4.75 KB
/
Copy pathtaskbridge.js
File metadata and controls
145 lines (127 loc) · 4.75 KB
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
import CONFIG from './config.json' with {type: 'json'}
// Adss a task of a specific type with the given data
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#add-a-task
async function addTask(type, data, file, requirements) {
const body = {
type: type
}
if (data) body.data = data
if (requirements) body.requirements = requirements
const formData = new FormData()
formData.append('json', JSON.stringify(body))
if (file) formData.append('file', file)
const response = await fetch(`${CONFIG.apiRoot}/tasks/add/`, {
method: 'POST',
body: formData
})
const result = await response.json()
return result
}
// Retreive the version of the TaskBridge used for API calls
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#get-version-of-taskbridge
async function getTaskBridgeVersion() {
const response = await fetch(`${CONFIG.apiRoot}/version`)
const version = await response.text()
return version
}
// Returns complete task details
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#get-all-details-of-a-task
async function getTaskDetails(taskId) {
const response = await fetch(`${CONFIG.apiRoot}/tasks/details/${taskId}`)
const details = await response.json()
return details
}
// Retreive a list of all tasks
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#list-all-tasks
async function getTaskList() {
const response = await fetch(`${CONFIG.apiRoot}/tasks/list/`)
const tasks = await response.json()
return tasks
}
// Retreive the result of a task
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#get-the-results-of-a-completed-task
async function getTaskResult(taskId) {
const response = await fetch(`${CONFIG.apiRoot}/tasks/result/${taskId}`)
const json = await response.json()
return json.result
}
// Retreive statistics of all task types
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#get-task-statistics
async function getTaskStatistics() {
const response = await fetch(`${CONFIG.apiRoot}/tasks/statistics/`)
const statistics = await response.json()
return statistics
}
// Retreive the status and progress for a task
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#get-status-information-about-a-task
async function getTaskStatus(taskId) {
const response = await fetch(`${CONFIG.apiRoot}/tasks/status/${taskId}`)
const status = response.status !== 404 ? await response.json() : undefined
return status
}
// Returns the WebUI version defined in config.json
function getWebUiVersion() {
return CONFIG.version
}
// Retreive a list of all connected workers and their status
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#list-all-workers
async function getWorkerList() {
const response = await fetch(`${CONFIG.apiRoot}/workers/list/`)
const workers = await response.json()
return workers
}
// Retreive statistics about all workers
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#get-worker-statistics
async function getWorkerStatistics() {
const response = await fetch(`${CONFIG.apiRoot}/tasks/workerstatistics/`)
const statistics = await response.json()
return statistics
}
// Removes a task
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#remove-a-task
async function removeTask(taskId) {
await fetch(`${CONFIG.apiRoot}/tasks/remove/${taskId}`, { method: 'DELETE' })
}
// Restarts a task
// https://github.com/hilderonny/taskbridge/blob/main/doc/API.md#restart-a-task
async function restartTask(taskId) {
await fetch(`${CONFIG.apiRoot}/tasks/restart/${taskId}`)
}
// Wait for a task completion and reports the results and the status
async function waitForTaskCompletion(taskId, statusCallback, completionCallback) {
let intervalId
const waiter = async function() {
const taskStatus = await getTaskStatus(taskId)
if (taskStatus === undefined) {
// Has been deleted
clearInterval(intervalId)
await completionCallback()
} else if (taskStatus.status === "completed") {
if (completionCallback) {
const taskResult = await getTaskResult(taskId)
await completionCallback(taskResult)
}
await removeTask(taskId)
clearInterval(intervalId)
} else {
if (statusCallback) await statusCallback(taskStatus)
}
}
intervalId = setInterval(waiter, 1000)
waiter()
}
export default {
addTask,
getTaskBridgeVersion,
getTaskDetails,
getTaskList,
getTaskResult,
getTaskStatistics,
getTaskStatus,
getWebUiVersion,
getWorkerList,
getWorkerStatistics,
removeTask,
restartTask,
waitForTaskCompletion
}