diff --git a/api/src/app.ts b/api/src/app.ts index 634ed5a..27aff05 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -15,7 +15,7 @@ const app = express() const { MODE, CORS, PORT_WEB } = process.env const whiteList = [ `http://localhost:${PORT_WEB ?? 3000}`, - 'https://sas.analytium.co.uk:8343/' + 'https://sas.analytium.co.uk:8343' ] if (MODE?.trim() !== 'server' || CORS?.trim() === 'enable') { diff --git a/api/src/controllers/internal/Session.ts b/api/src/controllers/internal/Session.ts index e9bf581..f3baf6c 100644 --- a/api/src/controllers/internal/Session.ts +++ b/api/src/controllers/internal/Session.ts @@ -12,7 +12,8 @@ import { createFile, fileExists, generateTimestamp, - readFile + readFile, + moveFile } from '@sasjs/utils' const execFilePromise = promisify(execFile) @@ -32,8 +33,9 @@ export class SessionController { return session } - private async createSession() { + private async createSession(): Promise { const sessionId = generateUniqueFileName(generateTimestamp()) + console.log('creating session', sessionId) const sessionFolder = path.join(getTmpSessionsFolderPath(), sessionId) const creationTimeStamp = sessionId.split('-').pop() as string @@ -100,25 +102,35 @@ export class SessionController { // SAS has been triggered but we can't use it until // the autoexec deletes the code.sas file - await this.waitForSession(session) + if (!(await this.waitForSession(session))) { + console.log('session is crashed', sessionId) + return this.createSession() + } + + console.log('session is ready', sessionId) return session } - public async waitForSession(session: Session) { + private async waitForSession(session: Session) { const codeFilePath = path.join(session.path, 'code.sas') // TODO: don't wait forever while ((await fileExists(codeFilePath)) && !session.crashed) {} console.log('session crashed?', !!session.crashed, session.crashed || '') + if (session.crashed) { + await this.deleteSession(session) + return false + } session.ready = true - return Promise.resolve(session) + return true } public async deleteSession(session: Session) { // remove the temporary files, to avoid buildup - await deleteFolder(session.path) + if (session.crashed) await moveFile(session.path, `${session.path}-crashed`) + else await deleteFolder(session.path) // remove the session from the session array if (session.ready) {