Require a ping call before generation

This commit is contained in:
Cohee 2024-05-12 00:18:56 +03:00
parent a93777e3b7
commit 7b472f13af
1 changed files with 32 additions and 0 deletions

View File

@ -816,6 +816,29 @@ $.ajaxPrefilter((options, originalOptions, xhr) => {
xhr.setRequestHeader('X-CSRF-Token', token);
});
/**
* Pings the STserver to check if it is reachable.
* @returns {Promise<boolean>} True if the server is reachable.
* @throws {Error} If the server is unreachable.
*/
export async function pingServer() {
try {
const result = await fetch('api/ping', {
method: 'GET',
headers: getRequestHeaders(),
});
if (!result.ok) {
return false;
}
return true;
} catch (error) {
console.error('Error pinging server', error);
return false;
}
}
async function firstLoadInit() {
try {
const tokenResponse = await fetch('/csrf-token');
@ -3082,6 +3105,15 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
}
if (!dryRun) {
// Ping server to make sure it is still alive
const pingResult = await pingServer();
if (!pingResult) {
unblockGeneration(type);
toastr.error('Verify that the server is running and accessible.', 'ST Server cannot be reached' );
throw new Error('Server unreachable');
}
// Hide swipes if not in a dry run.
hideSwipeButtons();
// If generated any message, set the flag to indicate it can't be recreated again.