diff --git a/api/src/controllers/internal/Execution.ts b/api/src/controllers/internal/Execution.ts index 5abb742..d97d1be 100644 --- a/api/src/controllers/internal/Execution.ts +++ b/api/src/controllers/internal/Execution.ts @@ -36,6 +36,7 @@ export class ExecutionController { const sessionController = getSessionController() const session = await sessionController.getSession() + console.log('using session', session.id) session.inUse = true const logPath = path.join(session.path, 'log.log') @@ -97,13 +98,15 @@ ${program}` // (which can mean SAS trying to run a partial program, or // failing due to file lock) we first create the file THEN // we rename it. + console.log('executing session', session.id) await createFile(codePath + '.bkp', program) await moveFile(codePath + '.bkp', codePath) - // we now need to poll the session array + // we now need to poll the session status while (!session.completed) { await delay(50) } + console.log('completed session', session.id) const log = ((await fileExists(logPath)) ? await readFile(logPath) : '') + @@ -115,8 +118,8 @@ ${program}` const debugValue = typeof vars._debug === 'string' ? parseInt(vars._debug) : vars._debug + // it should be deleted by scheduleSessionDestroy session.inUse = false - sessionController.deleteSession(session) if (returnJson) { return { diff --git a/api/src/controllers/internal/Session.ts b/api/src/controllers/internal/Session.ts index f3baf6c..21ca22c 100644 --- a/api/src/controllers/internal/Session.ts +++ b/api/src/controllers/internal/Session.ts @@ -22,7 +22,7 @@ export class SessionController { private sessions: Session[] = [] public async getSession() { - const readySessions = this.sessions.filter((sess: Session) => sess.ready) + const readySessions = this.getReadySessions() const session = readySessions.length ? readySessions[0] @@ -33,6 +33,9 @@ export class SessionController { return session } + private getReadySessions = (): Session[] => + this.sessions.filter((sess: Session) => sess.ready) + private async createSession(): Promise { const sessionId = generateUniqueFileName(generateTimestamp()) console.log('creating session', sessionId) @@ -102,13 +105,10 @@ export class SessionController { // SAS has been triggered but we can't use it until // the autoexec deletes the code.sas file - if (!(await this.waitForSession(session))) { - console.log('session is crashed', sessionId) - - return this.createSession() - } + await this.waitForSession(session) console.log('session is ready', sessionId) + return session } @@ -117,27 +117,21 @@ export class SessionController { // 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 - } + + if (session.crashed) + console.log('session crashed! while waiting to be ready', session.crashed) session.ready = true - return true } public async deleteSession(session: Session) { // remove the temporary files, to avoid buildup - if (session.crashed) await moveFile(session.path, `${session.path}-crashed`) - else await deleteFolder(session.path) + await deleteFolder(session.path) // remove the session from the session array - if (session.ready) { - this.sessions = this.sessions.filter( - (sess: Session) => sess.id !== session.id - ) - } + this.sessions = this.sessions.filter( + (sess: Session) => sess.id !== session.id + ) } private scheduleSessionDestroy(session: Session) {