Skip to content

Commit 9276338

Browse files
committed
This might work
1 parent e68bf79 commit 9276338

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/blueapi/blueapi.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,56 @@ export function submitPlan(
7676
}).then((res) => res.json().then((res) => res["task_id"]));
7777
}
7878

79+
// Note. fetch only rejects a promise on network errors, but http errors
80+
// must be caught by checking the response
7981
export function submitTask(
8082
planName: string,
8183
planParams: object
82-
): Promise<object | void> {
84+
): Promise<string | void> {
8385
return blueApiCall("/tasks", "POST", {
8486
name: planName,
8587
params: planParams,
8688
}).then((res) => {
8789
if (!res.ok) {
8890
throw new Error(
89-
`Unable to POST request,response error ${res.statusText}`
91+
`Unable to POST request, response error ${res.statusText}`
9092
);
9193
}
92-
res.json();
94+
res.json().then((res) => res["task_id"]);
95+
});
96+
}
97+
98+
export function runTask(taskId: string): Promise<string | void> {
99+
return blueApiCall("/worker/task", "PUT", { task_id: taskId }).then((res) => {
100+
if (!res.ok) {
101+
throw new Error(`Unable to run task, response error ${res.statusText}`);
102+
}
103+
res.json().then((res) => res["task_id"]);
93104
});
94105
}
95106

96107
export function submitAndRunPlanImmediately(
97108
planName: string,
98109
planParams: object
99-
): Promise<string> {
100-
return submitPlan(planName, planParams)
101-
.then((res) =>
102-
// TODO make sure submitPlan was succesful before then putting it to the worker
103-
// See https://github.com/DiamondLightSource/mx-daq-ui/issues/17
104-
blueApiCall("/worker/task", "PUT", { task_id: res }).then((res) =>
105-
res.json().then((res) => res["task_id"])
106-
)
107-
)
110+
): Promise<string | void> {
111+
return submitTask(planName, planParams)
112+
.then((res) => {
113+
if (res) {
114+
runTask(res);
115+
}
116+
})
108117
.catch((error) => console.log(error));
109118
}
119+
// return submitPlan(planName, planParams)
120+
// .then((res) =>
121+
// // TODO make sure submitPlan was succesful before then putting it to the worker
122+
// // See https://github.com/DiamondLightSource/mx-daq-ui/issues/17
123+
// blueApiCall("/worker/task", "PUT", { task_id: res }).then((res) =>
124+
// res.json().then((res) => res["task_id"])
125+
// )
126+
// )
127+
// .catch((error) => console.log(error));
128+
// }
110129

111130
export function abortCurrentPlan(): Promise<BlueApiWorkerState> {
112131
return blueApiCall("/worker/state", "PUT", {

0 commit comments

Comments
 (0)