mirror of
https://github.com/sasjs/server.git
synced 2026-01-07 06:30:06 +00:00
feat(session): add SessionController and ExecutionController
This commit is contained in:
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"cSpell.words": [
|
||||||
|
"autoexec"
|
||||||
|
]
|
||||||
|
}
|
||||||
68
src/controllers/executor.ts
Normal file
68
src/controllers/executor.ts
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import { getSessionController } from './session'
|
||||||
|
import { readFile, deleteFile, fileExists, createFile } from '@sasjs/utils'
|
||||||
|
import path from 'path'
|
||||||
|
import { configuration } from '../../package.json'
|
||||||
|
import { promisify } from 'util'
|
||||||
|
import { execFile } from 'child_process'
|
||||||
|
const execFilePromise = promisify(execFile)
|
||||||
|
|
||||||
|
export class ExecutionController {
|
||||||
|
async execute(program = '', autoExec?: string, debug?: number) {
|
||||||
|
console.log(`[ExecutionController]program: `, program)
|
||||||
|
console.log(`[ExecutionController]autoExec: `, autoExec)
|
||||||
|
|
||||||
|
if (program) {
|
||||||
|
if (!(await fileExists(program))) {
|
||||||
|
throw 'SASjsServer/Executor: SAS file does not exist.'
|
||||||
|
}
|
||||||
|
|
||||||
|
program = await readFile(program)
|
||||||
|
}
|
||||||
|
|
||||||
|
const sessionController = getSessionController()
|
||||||
|
const session = await sessionController.getSession()
|
||||||
|
|
||||||
|
console.log(`[ExecutionController]session: `, session)
|
||||||
|
|
||||||
|
let log = path.join(session.path, 'log.log')
|
||||||
|
await createFile(log, '')
|
||||||
|
|
||||||
|
let webout = path.join(session.path, 'webout.txt')
|
||||||
|
await createFile(webout, '')
|
||||||
|
|
||||||
|
const code = path.join(session.path, 'code')
|
||||||
|
await createFile(code, program)
|
||||||
|
|
||||||
|
let additionalArgs: string[] = []
|
||||||
|
if (autoExec) additionalArgs = ['-AUTOEXEC', autoExec]
|
||||||
|
|
||||||
|
const { stdout, stderr } = await execFilePromise(configuration.sasPath, [
|
||||||
|
'-SYSIN',
|
||||||
|
code,
|
||||||
|
'-LOG',
|
||||||
|
log,
|
||||||
|
'-WORK',
|
||||||
|
...additionalArgs,
|
||||||
|
session.path,
|
||||||
|
process.platform === 'win32' ? '-nosplash' : ''
|
||||||
|
]).catch((err) => ({ stderr: err, stdout: '' }))
|
||||||
|
|
||||||
|
log = await readFile(log)
|
||||||
|
|
||||||
|
if (stderr) return Promise.reject({ error: stderr, log: log })
|
||||||
|
|
||||||
|
webout = await readFile(webout)
|
||||||
|
|
||||||
|
if (debug && debug >= 131) {
|
||||||
|
webout = `<html><body>
|
||||||
|
${webout}
|
||||||
|
<div style="text-align:left">
|
||||||
|
<hr /><h2>SAS Log</h2>
|
||||||
|
<pre>${log}</pre>
|
||||||
|
</div>
|
||||||
|
</body></html>`
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve(webout)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
export * from './sas'
|
export * from './sas'
|
||||||
export * from './deploy'
|
export * from './deploy'
|
||||||
|
export * from './session'
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
generateUniqueFileName
|
generateUniqueFileName
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
import { configuration } from '../../package.json'
|
import { configuration } from '../../package.json'
|
||||||
|
import { SessionController } from './session'
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'util'
|
||||||
import { execFile } from 'child_process'
|
import { execFile } from 'child_process'
|
||||||
const execFilePromise = promisify(execFile)
|
const execFilePromise = promisify(execFile)
|
||||||
@@ -21,79 +22,86 @@ export const processSas = async (query: ExecutionQuery): Promise<any> => {
|
|||||||
return Promise.reject({ error: 'SAS file does not exist.' })
|
return Promise.reject({ error: 'SAS file does not exist.' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const sasFile: string = sasCodePath.split(path.sep).pop() || 'default'
|
// FIXME
|
||||||
|
const sessionController = new SessionController()
|
||||||
|
|
||||||
const sasLogPath = path.join(
|
sessionController.getSession()
|
||||||
getTmpLogFolderPath(),
|
|
||||||
generateUniqueFileName(sasFile.replace(/\.sas/g, ''), '.log')
|
|
||||||
)
|
|
||||||
|
|
||||||
await createFile(sasLogPath, '')
|
return Promise.resolve('success')
|
||||||
|
|
||||||
const sasWeboutPath = path.join(
|
// const sasFile: string = sasCodePath.split(path.sep).pop() || 'default'
|
||||||
getTmpWeboutFolderPath(),
|
|
||||||
generateUniqueFileName(sasFile.replace(/\.sas/g, ''), '.txt')
|
|
||||||
)
|
|
||||||
|
|
||||||
await createFile(sasWeboutPath, '')
|
// const sasLogPath = path.join(
|
||||||
|
// getTmpLogFolderPath(),
|
||||||
|
// generateUniqueFileName(sasFile.replace(/\.sas/g, ''), '.log')
|
||||||
|
// )
|
||||||
|
|
||||||
let sasCode = await readFile(sasCodePath)
|
// await createFile(sasLogPath, '')
|
||||||
|
|
||||||
const vars: any = query
|
// const sasWeboutPath = path.join(
|
||||||
Object.keys(query).forEach(
|
// getTmpWeboutFolderPath(),
|
||||||
(key: string) => (sasCode = `%let ${key}=${vars[key]};\n${sasCode}`)
|
// generateUniqueFileName(sasFile.replace(/\.sas/g, ''), '.txt')
|
||||||
)
|
// )
|
||||||
|
|
||||||
sasCode = `filename _webout "${sasWeboutPath}";\n${sasCode}`
|
// await createFile(sasWeboutPath, '')
|
||||||
|
|
||||||
const tmpSasCodePath = sasCodePath.replace(
|
// let sasCode = await readFile(sasCodePath)
|
||||||
sasFile,
|
|
||||||
generateUniqueFileName(sasFile)
|
|
||||||
)
|
|
||||||
|
|
||||||
await createFile(tmpSasCodePath, sasCode)
|
// const vars: any = query
|
||||||
|
// Object.keys(query).forEach(
|
||||||
|
// (key: string) => (sasCode = `%let ${key}=${vars[key]};\n${sasCode}`)
|
||||||
|
// )
|
||||||
|
|
||||||
const { stdout, stderr } = await execFilePromise(configuration.sasPath, [
|
// sasCode = `filename _webout "${sasWeboutPath}";\n${sasCode}`
|
||||||
'-SYSIN',
|
|
||||||
tmpSasCodePath,
|
|
||||||
'-log',
|
|
||||||
sasLogPath,
|
|
||||||
process.platform === 'win32' ? '-nosplash' : ''
|
|
||||||
]).catch((err) => ({ stderr: err, stdout: '' }))
|
|
||||||
|
|
||||||
let log = ''
|
// const tmpSasCodePath = sasCodePath.replace(
|
||||||
if (sasLogPath && (await fileExists(sasLogPath))) {
|
// sasFile,
|
||||||
log = await readFile(sasLogPath)
|
// generateUniqueFileName(sasFile)
|
||||||
}
|
// )
|
||||||
|
|
||||||
await deleteFile(sasLogPath)
|
// await createFile(tmpSasCodePath, sasCode)
|
||||||
await deleteFile(tmpSasCodePath)
|
|
||||||
|
// const { stdout, stderr } = await execFilePromise(configuration.sasPath, [
|
||||||
if (stderr) return Promise.reject({ error: stderr, log: log })
|
// '-SYSIN',
|
||||||
|
// tmpSasCodePath,
|
||||||
if (await fileExists(sasWeboutPath)) {
|
// '-log',
|
||||||
let webout = await readFile(sasWeboutPath)
|
// sasLogPath,
|
||||||
|
// process.platform === 'win32' ? '-nosplash' : ''
|
||||||
await deleteFile(sasWeboutPath)
|
// ]).catch((err) => ({ stderr: err, stdout: '' }))
|
||||||
|
|
||||||
const debug = Object.keys(query).find(
|
// let log = ''
|
||||||
(key: string) => key.toLowerCase() === '_debug'
|
// if (sasLogPath && (await fileExists(sasLogPath))) {
|
||||||
)
|
// log = await readFile(sasLogPath)
|
||||||
|
// }
|
||||||
if (debug && (query as any)[debug] >= 131) {
|
|
||||||
webout = `<html><body>
|
// await deleteFile(sasLogPath)
|
||||||
${webout}
|
// await deleteFile(tmpSasCodePath)
|
||||||
<div style="text-align:left">
|
|
||||||
<hr /><h2>SAS Log</h2>
|
// if (stderr) return Promise.reject({ error: stderr, log: log })
|
||||||
<pre>${log}</pre>
|
|
||||||
</div>
|
// if (await fileExists(sasWeboutPath)) {
|
||||||
</body></html>`
|
// let webout = await readFile(sasWeboutPath)
|
||||||
}
|
|
||||||
|
// await deleteFile(sasWeboutPath)
|
||||||
return Promise.resolve(webout)
|
|
||||||
} else {
|
// const debug = Object.keys(query).find(
|
||||||
return Promise.resolve({
|
// (key: string) => key.toLowerCase() === '_debug'
|
||||||
log: log
|
// )
|
||||||
})
|
|
||||||
}
|
// if (debug && (query as any)[debug] >= 131) {
|
||||||
|
// webout = `<html><body>
|
||||||
|
// ${webout}
|
||||||
|
// <div style="text-align:left">
|
||||||
|
// <hr /><h2>SAS Log</h2>
|
||||||
|
// <pre>${log}</pre>
|
||||||
|
// </div>
|
||||||
|
// </body></html>`
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return Promise.resolve(webout)
|
||||||
|
// } else {
|
||||||
|
// return Promise.resolve({
|
||||||
|
// log: log
|
||||||
|
// })
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
109
src/controllers/session.ts
Normal file
109
src/controllers/session.ts
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
import { Session } from '../types'
|
||||||
|
import { getTmpSessionsFolderPath, generateUniqueFileName } from '../utils'
|
||||||
|
import { createFolder, createFile, generateTimestamp } from '@sasjs/utils'
|
||||||
|
import path from 'path'
|
||||||
|
import { ExecutionController } from './executor'
|
||||||
|
|
||||||
|
// /opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe/sas
|
||||||
|
// -LOG /tmp/mydir/demo.log
|
||||||
|
// -SYSIN /tmp/mydir/code.sas
|
||||||
|
// -AUTOEXEC /tmp/mydir/autoexec.sas
|
||||||
|
// -WORK /tmp/mydir
|
||||||
|
|
||||||
|
// 1. req (_program) for execution
|
||||||
|
// 2. check available session
|
||||||
|
// 2.3 spawn one more session
|
||||||
|
// 2.3.1 create folder
|
||||||
|
// 2.3.2 create autoexec
|
||||||
|
// 2.3.3 create _program.sas (empty)
|
||||||
|
// 2.3.4 /opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe/sas -LOG /tmp/sessionFolder/demo.log -SYSIN /tmp/sessionFolder/_program.sas -AUTOEXEC /tmp/sessionFolder/autoexec.sas -WORK /tmp/sessionFolder
|
||||||
|
// 2.3.5 wait for _program.sas to be deleted
|
||||||
|
// 2.3.6 add session to the session array
|
||||||
|
// ---
|
||||||
|
// 3.0 wait for session
|
||||||
|
// 3.1 create _program.sas in sessionFolder
|
||||||
|
// 3.2 poll session array
|
||||||
|
|
||||||
|
export class SessionController {
|
||||||
|
private sessions: Session[] = []
|
||||||
|
private executionController: ExecutionController
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.executionController = new ExecutionController()
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getSession() {
|
||||||
|
if (this.sessions.length) {
|
||||||
|
const session: Session = this.sessions[0]
|
||||||
|
|
||||||
|
// TODO: check if session is not expired
|
||||||
|
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.createSession()
|
||||||
|
}
|
||||||
|
|
||||||
|
private async createSession() {
|
||||||
|
if (!this.sessions.length) {
|
||||||
|
const sessionId = generateUniqueFileName(generateTimestamp())
|
||||||
|
|
||||||
|
const sessionFolder = path.join(
|
||||||
|
await getTmpSessionsFolderPath(),
|
||||||
|
sessionId
|
||||||
|
)
|
||||||
|
|
||||||
|
const autoExecContent = `data _null_;
|
||||||
|
/* remove the dummy SYSIN */
|
||||||
|
length fname $8;
|
||||||
|
rc=filename(fname,getoption('SYSIN') );
|
||||||
|
if rc = 0 and fexist(fname) then rc=fdelete(fname);
|
||||||
|
rc=filename(fname);
|
||||||
|
/* now wait for the real SYSIN */
|
||||||
|
slept=0;
|
||||||
|
do until ( fileexist(getoption('SYSIN')) or slept>(60*15) );
|
||||||
|
slept=slept+sleep(0.1,1);
|
||||||
|
end;
|
||||||
|
run;
|
||||||
|
EOL`
|
||||||
|
|
||||||
|
const autoExec = path.join(sessionFolder, 'autoexec.sas')
|
||||||
|
|
||||||
|
await createFile(autoExec, autoExecContent)
|
||||||
|
|
||||||
|
console.log(`[SessionController about to create first session]`)
|
||||||
|
|
||||||
|
this.executionController.execute('', autoExec)
|
||||||
|
|
||||||
|
const creationTimeStamp = sessionId.split('-').pop() as string
|
||||||
|
|
||||||
|
const session: Session = {
|
||||||
|
id: sessionId,
|
||||||
|
available: true,
|
||||||
|
creationTimeStamp: creationTimeStamp,
|
||||||
|
deathTimeStamp: (
|
||||||
|
parseInt(creationTimeStamp) +
|
||||||
|
15 * 60 * 1000 -
|
||||||
|
1000
|
||||||
|
).toString(),
|
||||||
|
path: sessionFolder
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[SessionController]session: `, session)
|
||||||
|
|
||||||
|
this.sessions.push(session)
|
||||||
|
|
||||||
|
return session
|
||||||
|
} else {
|
||||||
|
return this.sessions[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getSessionController = () => {
|
||||||
|
if (process.sessionController) return process.sessionController
|
||||||
|
|
||||||
|
process.sessionController = new SessionController()
|
||||||
|
|
||||||
|
return process.sessionController
|
||||||
|
}
|
||||||
5
src/types/Process.d.ts
vendored
Normal file
5
src/types/Process.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
declare namespace NodeJS {
|
||||||
|
export interface Process {
|
||||||
|
sessionController?: import('../controllers/session').SessionController
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/types/Session.ts
Normal file
7
src/types/Session.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export interface Session {
|
||||||
|
id: string
|
||||||
|
available: boolean
|
||||||
|
creationTimeStamp: string
|
||||||
|
deathTimeStamp: string
|
||||||
|
path: string
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// TODO: uppercase types
|
||||||
export * from './sas'
|
export * from './sas'
|
||||||
export * from './request'
|
export * from './request'
|
||||||
export * from './fileTree'
|
export * from './fileTree'
|
||||||
|
export * from './Session'
|
||||||
|
|||||||
@@ -12,12 +12,15 @@ export const getTmpLogFolderPath = () => path.join(getTmpFolderPath(), 'logs')
|
|||||||
export const getTmpWeboutFolderPath = () =>
|
export const getTmpWeboutFolderPath = () =>
|
||||||
path.join(getTmpFolderPath(), 'webouts')
|
path.join(getTmpFolderPath(), 'webouts')
|
||||||
|
|
||||||
|
export const getTmpSessionsFolderPath = () =>
|
||||||
|
path.join(getTmpFolderPath(), 'sessions')
|
||||||
|
|
||||||
export const generateUniqueFileName = (fileName: string, extension = '') =>
|
export const generateUniqueFileName = (fileName: string, extension = '') =>
|
||||||
[
|
[
|
||||||
fileName,
|
fileName,
|
||||||
'-',
|
'-',
|
||||||
Math.round(Math.random() * 100000),
|
Math.round(Math.random() * 100000),
|
||||||
'-',
|
'-',
|
||||||
generateTimestamp(),
|
new Date().getTime(),
|
||||||
extension
|
extension
|
||||||
].join('')
|
].join('')
|
||||||
|
|||||||
Reference in New Issue
Block a user