mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-15 16:10:06 +00:00
fix(node): only create and write file stream if running in node
This commit is contained in:
16
src/api/viya/getFileStream.ts
Normal file
16
src/api/viya/getFileStream.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { isFolder } from '@sasjs/utils/file'
|
||||||
|
import { generateTimestamp } from '@sasjs/utils/time'
|
||||||
|
import { Job } from '../../types'
|
||||||
|
|
||||||
|
export const getFileStream = async (job: Job, filePath?: string) => {
|
||||||
|
const { createWriteStream } = require('@sasjs/utils/file')
|
||||||
|
const logPath = filePath || process.cwd()
|
||||||
|
const isFolderPath = await isFolder(logPath)
|
||||||
|
if (isFolderPath) {
|
||||||
|
const logFileName = `${job.name || 'job'}-${generateTimestamp()}.log`
|
||||||
|
const logFilePath = `${filePath || process.cwd()}/${logFileName}`
|
||||||
|
return await createWriteStream(logFilePath)
|
||||||
|
} else {
|
||||||
|
return await createWriteStream(logPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,8 @@ import { Job, PollOptions } from '../..'
|
|||||||
import { getTokens } from '../../auth/getTokens'
|
import { getTokens } from '../../auth/getTokens'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
import { JobStatePollError } from '../../types/errors'
|
import { JobStatePollError } from '../../types/errors'
|
||||||
import { generateTimestamp } from '@sasjs/utils/time'
|
import { Link, WriteStream } from '../../types'
|
||||||
import { saveLog } from './saveLog'
|
import { isNode } from '../../utils'
|
||||||
import { createWriteStream, isFolder } from '@sasjs/utils/file'
|
|
||||||
import { WriteStream } from 'fs'
|
|
||||||
import { Link } from '../../types'
|
|
||||||
|
|
||||||
export async function pollJobState(
|
export async function pollJobState(
|
||||||
requestClient: RequestClient,
|
requestClient: RequestClient,
|
||||||
@@ -55,21 +52,9 @@ export async function pollJobState(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let logFileStream
|
let logFileStream
|
||||||
if (pollOptions.streamLog) {
|
if (pollOptions.streamLog && isNode()) {
|
||||||
const logPath = pollOptions?.logFolderPath || process.cwd()
|
const { getFileStream } = require('./getFileStream')
|
||||||
const isFolderPath = await isFolder(logPath)
|
logFileStream = await getFileStream(postedJob, pollOptions.logFolderPath)
|
||||||
if (isFolderPath) {
|
|
||||||
const logFileName = `${
|
|
||||||
postedJob.name || 'job'
|
|
||||||
}-${generateTimestamp()}.log`
|
|
||||||
const logFilePath = `${
|
|
||||||
pollOptions?.logFolderPath || process.cwd()
|
|
||||||
}/${logFileName}`
|
|
||||||
|
|
||||||
logFileStream = await createWriteStream(logFilePath)
|
|
||||||
} else {
|
|
||||||
logFileStream = await createWriteStream(logPath)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Poll up to the first 100 times with the specified poll interval
|
// Poll up to the first 100 times with the specified poll interval
|
||||||
@@ -230,14 +215,17 @@ const doPoll = async (
|
|||||||
|
|
||||||
const endLogLine = job.logStatistics?.lineCount ?? 1000000
|
const endLogLine = job.logStatistics?.lineCount ?? 1000000
|
||||||
|
|
||||||
await saveLog(
|
const { saveLog } = isNode() ? require('./saveLog') : { saveLog: null }
|
||||||
postedJob,
|
if (saveLog) {
|
||||||
requestClient,
|
await saveLog(
|
||||||
startLogLine,
|
postedJob,
|
||||||
endLogLine,
|
requestClient,
|
||||||
logStream,
|
startLogLine,
|
||||||
authConfig?.access_token
|
endLogLine,
|
||||||
)
|
logStream,
|
||||||
|
authConfig?.access_token
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
startLogLine += endLogLine
|
startLogLine += endLogLine
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user