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

fix(streamlog): use filepath if provided

This commit is contained in:
Krishna Acondy
2021-07-22 09:25:55 +01:00
parent c02eac196e
commit 0bb42c5e3c
4 changed files with 63 additions and 44 deletions

View File

@@ -5,7 +5,7 @@ import { RequestClient } from '../../request/RequestClient'
import { JobStatePollError } from '../../types/errors'
import { generateTimestamp } from '@sasjs/utils/time'
import { saveLog } from './saveLog'
import { createWriteStream } from '@sasjs/utils/file'
import { createWriteStream, isFolder } from '@sasjs/utils/file'
import { WriteStream } from 'fs'
import { Link } from '../../types'
@@ -53,12 +53,20 @@ export async function pollJobState(
let logFileStream
if (pollOptions?.streamLog) {
const logFileName = `${postedJob.name || 'job'}-${generateTimestamp()}.log`
const logFilePath = `${
pollOptions?.logFolderPath || process.cwd()
}/${logFileName}`
const logPath = pollOptions?.logFolderPath || process.cwd()
const isFolderPath = await isFolder(logPath)
if (isFolderPath) {
const logFileName = `${
postedJob.name || 'job'
}-${generateTimestamp()}.log`
const logFilePath = `${
pollOptions?.logFolderPath || process.cwd()
}/${logFileName}`
logFileStream = await createWriteStream(logFilePath)
logFileStream = await createWriteStream(logFilePath)
} else {
logFileStream = await createWriteStream(logPath)
}
}
let result = await doPoll(

View File

@@ -1,4 +1,5 @@
import { Logger, LogLevel } from '@sasjs/utils'
import * as path from 'path'
import * as fileModule from '@sasjs/utils/file'
import { RequestClient } from '../../../request/RequestClient'
import { mockAuthConfig, mockJob } from './mockResponses'
@@ -85,6 +86,35 @@ describe('pollJobState', () => {
expect(saveLogModule.saveLog).toHaveBeenCalledTimes(2)
})
it('should use the given log path if it points to a file', async () => {
mockSimplePoll()
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
...defaultPollOptions,
streamLog: true,
logFolderPath: path.join(__dirname, 'test.log')
})
expect(fileModule.createWriteStream).toHaveBeenCalledWith(
path.join(__dirname, 'test.log')
)
})
it('should generate a log file path with a timestamp if it points to a folder', async () => {
mockSimplePoll()
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
...defaultPollOptions,
streamLog: true,
logFolderPath: path.join(__dirname)
})
expect(fileModule.createWriteStream).not.toHaveBeenCalledWith(__dirname)
expect(fileModule.createWriteStream).toHaveBeenCalledWith(
expect.stringContaining(__dirname + '/20')
)
})
it('should not attempt to fetch and save the log after each poll when streamLog is false', async () => {
mockSimplePoll()