1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-05 05:40:06 +00:00

feat: improved deploy and execute endpoints

This commit is contained in:
Yury Shkoda
2021-09-30 14:39:54 +03:00
parent c4b9402f01
commit 5b4e5626fc
3 changed files with 82 additions and 52 deletions

View File

@@ -34,26 +34,23 @@ export const createFileTree = async (
} }
export const getTreeExample = () => ({ export const getTreeExample = () => ({
message: 'Provided not supported data format.', members: [
supportedFormat: { {
members: [ name: 'jobs',
{ type: 'folder',
name: 'jobs', members: [
type: 'folder', {
members: [ name: 'extract',
{ type: 'folder',
name: 'extract', members: [
type: 'folder', {
members: [ name: 'makedata1',
{ type: 'service',
name: 'makedata1', code: '%put Hello World!;'
type: 'service', }
code: '%put Hello World!;' ]
} }
] ]
} }
] ]
}
]
}
}) })

View File

@@ -1,4 +1,3 @@
import { execFile } from 'child_process'
import { import {
readFile, readFile,
generateTimestamp, generateTimestamp,
@@ -13,37 +12,45 @@ import {
getTmpLogFolderPath getTmpLogFolderPath
} from '../utils' } from '../utils'
import { configuration } from '../../package.json' import { configuration } from '../../package.json'
import { promisify } from 'util'
import { execFile } from 'child_process'
const execFilePromise = promisify(execFile)
export const processSas = async ( export const processSas = async (
query: ExecutionQuery query: ExecutionQuery
): Promise<ExecutionResult> => ): Promise<ExecutionResult> => {
new Promise(async (resolve, reject) => { let sasCodePath = path.join(getTmpFilesFolderPath(), query._program)
let sasCodePath = path.join(getTmpFilesFolderPath(), query._program) sasCodePath = sasCodePath.replace(new RegExp('/', 'g'), path.sep)
sasCodePath = sasCodePath.replace(new RegExp('/', 'g'), path.sep)
if (!(await fileExists(sasCodePath))) { if (!(await fileExists(sasCodePath))) {
reject('SAS file does not exist.') return Promise.reject('SAS file does not exist.')
} }
const sasFile: string = sasCodePath.split(path.sep).pop() || 'default' const sasFile: string = sasCodePath.split(path.sep).pop() || 'default'
const sasLogPath = path.join( const sasLogPath = path.join(
getTmpLogFolderPath(), getTmpLogFolderPath(),
[sasFile.replace(/\.sas/g, ''), '-', generateTimestamp(), '.log'].join('') [sasFile.replace(/\.sas/g, ''), '-', generateTimestamp(), '.log'].join('')
) )
execFile( const { stdout, stderr } = await execFilePromise(configuration.sasPath, [
configuration.sasPath, '-SYSIN',
['-SYSIN', sasCodePath, '-log', sasLogPath, '-nosplash'], sasCodePath,
async (err, _, stderr) => { '-log',
if (err) reject(err) sasLogPath,
if (stderr) reject(stderr) '-nosplash'
])
const log = await readFile(sasLogPath) if (stderr) return Promise.reject(stderr)
// deleteFile(sasLogPath) if (await fileExists(sasLogPath)) {
return Promise.resolve({
log: await readFile(sasLogPath),
logPath: sasLogPath
})
} else {
return Promise.reject(`Log file wasn't created.`)
}
resolve({ log: log, logPath: sasLogPath }) // deleteFile(sasLogPath)
} }
)
})

View File

@@ -22,25 +22,51 @@ router.get('/', async (req, res) => {
router.post('/deploy', async (req, res) => { router.post('/deploy', async (req, res) => {
if (!isFileTree(req.body)) { if (!isFileTree(req.body)) {
res.status(400).send(getTreeExample()) res.status(400).send({
status: 'failure',
message: 'Provided not supported data format.',
example: getTreeExample()
})
return return
} }
await createFileTree(req.body.members) await createFileTree(req.body.members)
.then(() => { .then(() => {
res.status(200).send('Files deployed successfully to @sasjs/server.') res.status(200).send({
status: 'success',
message: 'Files deployed successfully to @sasjs/server.'
})
}) })
.catch((err) => { .catch((err) => {
res.status(500).send({ message: 'Deployment failed!', ...err }) res
.status(500)
.send({ status: 'failure', message: 'Deployment failed!', ...err })
}) })
}) })
router.post('/execute', async (req, res) => { router.post('/execute', async (req, res) => {
if (req.body?._program) { if (req.body?._program) {
const result: ExecutionResult = await processSas(req.body) await processSas(req.body)
.then((result) => {
res.status(200).send({
status: 'success',
message: 'Job has been sent for execution.',
...result
})
})
.catch((err) => {
res.status(400).send({
status: 'failure',
message: 'Job execution failed.',
error: err
})
})
} else { } else {
res.status(400).send(`Please provide the location of SAS code`) res.status(400).send({
status: 'failure',
message: `Please provide the location of SAS code`
})
} }
}) })