Files
cloudron-box/dashboard/src/models/AppModel.js
T

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-07-14 14:48:43 +02:00
import { ISTATES } from '../constants.js';
import { fetcher } from 'pankow';
2023-07-14 14:48:43 +02:00
import { sleep } from 'pankow/utils';
export function create(origin, accessToken, id) {
return {
name: 'AppModel',
async get() {
let error, result;
try {
result = await fetcher.get(`${origin}/api/v1/apps/${id}`, { access_token: accessToken });
2023-07-14 14:48:43 +02:00
} catch (e) {
error = e;
}
if (error || result.status !== 200) {
console.error(`Invalid app ${id}`, error || result.status);
2023-07-14 14:48:43 +02:00
this.fatalError = `Invalid app ${id}`;
return;
}
return result.body;
},
async restart() {
let error, result;
try {
result = await fetcher.post(`${origin}/api/v1/apps/${id}/restart`, null, { access_token: accessToken });
2023-07-14 14:48:43 +02:00
} catch (e) {
error = e;
}
if (error || result.status !== 202) {
console.error(`Failed to restart app ${this.id}`, error || result.status);
2023-07-14 14:48:43 +02:00
return;
}
while(true) {
let result;
try {
result = await fetcher.get(`${origin}/api/v1/apps/${id}`, { access_token: accessToken });
2023-07-14 14:48:43 +02:00
} catch (e) {
console.error(e);
}
if (result && result.status === 200 && result.body.installationState === ISTATES.INSTALLED) break;
2023-07-14 14:48:43 +02:00
await sleep(2000);
}
}
};
}
export default {
create
};