mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-08 21:10:05 +00:00
chore(*): remove unused dependencies and variables, fix imports
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
import {
|
||||
AuthConfig,
|
||||
MacroVar,
|
||||
Logger,
|
||||
LogLevel,
|
||||
timestampToYYYYMMDDHHMMSS
|
||||
} from '@sasjs/utils'
|
||||
import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
|
||||
import { AuthConfig, MacroVar } from '@sasjs/utils/types'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
import {
|
||||
PollOptions,
|
||||
@@ -42,7 +37,7 @@ export async function executeScript(
|
||||
linesOfCode: string[],
|
||||
contextName: string,
|
||||
authConfig?: AuthConfig,
|
||||
data = null,
|
||||
data: any = null,
|
||||
debug: boolean = false,
|
||||
expectWebout = false,
|
||||
waitForResult = true,
|
||||
@@ -80,7 +75,7 @@ export async function executeScript(
|
||||
? jobPath.split(rootFolderName).join('').replace(/^\//, '')
|
||||
: jobPath
|
||||
|
||||
const logger = new Logger(debug ? LogLevel.Debug : LogLevel.Info)
|
||||
const logger = process.logger || console
|
||||
|
||||
logger.info(
|
||||
`Triggered '${relativeJobPath}' with PID ${
|
||||
@@ -235,44 +230,40 @@ export async function executeScript(
|
||||
}
|
||||
|
||||
if (jobStatus === 'failed' || jobStatus === 'error') {
|
||||
return Promise.reject(new ComputeJobExecutionError(currentJob, log))
|
||||
throw new ComputeJobExecutionError(currentJob, log)
|
||||
}
|
||||
|
||||
let resultLink
|
||||
|
||||
if (expectWebout) {
|
||||
resultLink = `/compute/sessions/${executionSessionId}/filerefs/_webout/content`
|
||||
} else {
|
||||
if (!expectWebout) {
|
||||
return { job: currentJob, log }
|
||||
}
|
||||
|
||||
if (resultLink) {
|
||||
jobResult = await requestClient
|
||||
.get<any>(resultLink, access_token, 'text/plain')
|
||||
.catch(async (e) => {
|
||||
if (e instanceof NotFoundError) {
|
||||
if (logLink) {
|
||||
const logUrl = `${logLink.href}/content`
|
||||
const logCount = currentJob.logStatistics?.lineCount ?? 1000000
|
||||
log = await fetchLogByChunks(
|
||||
requestClient,
|
||||
access_token!,
|
||||
logUrl,
|
||||
logCount
|
||||
)
|
||||
const resultLink = `/compute/sessions/${executionSessionId}/filerefs/_webout/content`
|
||||
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
log
|
||||
})
|
||||
}
|
||||
}
|
||||
jobResult = await requestClient
|
||||
.get<any>(resultLink, access_token, 'text/plain')
|
||||
.catch(async (e) => {
|
||||
if (e instanceof NotFoundError) {
|
||||
if (logLink) {
|
||||
const logUrl = `${logLink.href}/content`
|
||||
const logCount = currentJob.logStatistics?.lineCount ?? 1000000
|
||||
log = await fetchLogByChunks(
|
||||
requestClient,
|
||||
access_token!,
|
||||
logUrl,
|
||||
logCount
|
||||
)
|
||||
|
||||
return {
|
||||
result: JSON.stringify(e)
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
log
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
result: JSON.stringify(e)
|
||||
}
|
||||
})
|
||||
|
||||
await sessionManager
|
||||
.clearSession(executionSessionId, access_token)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { AuthConfig, createFile, generateTimestamp } from '@sasjs/utils'
|
||||
import { AuthConfig } from '@sasjs/utils'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
import { generateTimestamp } from '@sasjs/utils/time'
|
||||
import { createFile } from '@sasjs/utils/file'
|
||||
import { Job, PollOptions } from '../..'
|
||||
import { getTokens } from '../../auth/tokens'
|
||||
import { RequestClient } from '../../request/RequestClient'
|
||||
@@ -15,17 +17,23 @@ export async function pollJobState(
|
||||
) {
|
||||
const logger = process.logger || console
|
||||
|
||||
let POLL_INTERVAL = 300
|
||||
let MAX_POLL_COUNT = 1000
|
||||
let MAX_ERROR_COUNT = 5
|
||||
let pollInterval = 300
|
||||
let maxPollCount = 1000
|
||||
let maxErrorCount = 5
|
||||
let access_token = (authConfig || {}).access_token
|
||||
|
||||
const logFileName = `${postedJob.name || 'job'}-${generateTimestamp()}.log`
|
||||
const logFilePath = `${
|
||||
pollOptions?.logFilePath || process.cwd()
|
||||
}/${logFileName}`
|
||||
|
||||
if (authConfig) {
|
||||
;({ access_token } = await getTokens(requestClient, authConfig))
|
||||
}
|
||||
|
||||
if (pollOptions) {
|
||||
POLL_INTERVAL = pollOptions.pollInterval || POLL_INTERVAL
|
||||
MAX_POLL_COUNT = pollOptions.maxPollCount || MAX_POLL_COUNT
|
||||
pollInterval = pollOptions.pollInterval || pollInterval
|
||||
maxPollCount = pollOptions.maxPollCount || maxPollCount
|
||||
}
|
||||
|
||||
let postedJobState = ''
|
||||
@@ -89,10 +97,7 @@ export async function pollJobState(
|
||||
)
|
||||
.catch((err) => {
|
||||
errorCount++
|
||||
if (
|
||||
pollCount >= MAX_POLL_COUNT ||
|
||||
errorCount >= MAX_ERROR_COUNT
|
||||
) {
|
||||
if (pollCount >= maxPollCount || errorCount >= maxErrorCount) {
|
||||
throw prefixMessage(
|
||||
err,
|
||||
'Error while getting job state after interval. '
|
||||
@@ -123,11 +128,11 @@ export async function pollJobState(
|
||||
postedJob,
|
||||
requestClient,
|
||||
pollOptions?.streamLog || false,
|
||||
pollOptions?.logFilePath,
|
||||
logFilePath,
|
||||
access_token
|
||||
)
|
||||
|
||||
if (pollCount >= MAX_POLL_COUNT) {
|
||||
if (pollCount >= maxPollCount) {
|
||||
resolve(postedJobState)
|
||||
}
|
||||
}
|
||||
@@ -135,7 +140,7 @@ export async function pollJobState(
|
||||
clearInterval(interval)
|
||||
resolve(postedJobState)
|
||||
}
|
||||
}, POLL_INTERVAL)
|
||||
}, pollInterval)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -143,7 +148,7 @@ async function saveLog(
|
||||
job: Job,
|
||||
requestClient: RequestClient,
|
||||
shouldSaveLog: boolean,
|
||||
logFilePath?: string,
|
||||
logFilePath: string,
|
||||
accessToken?: string
|
||||
) {
|
||||
if (!shouldSaveLog) {
|
||||
@@ -157,8 +162,6 @@ async function saveLog(
|
||||
}
|
||||
|
||||
const logger = process.logger || console
|
||||
const logFileName = `${job.name || 'job'}-${generateTimestamp()}.log`
|
||||
const logPath = `${logFilePath || process.cwd()}/${logFileName}`
|
||||
const jobLogUrl = job.links.find((l) => l.rel === 'log')
|
||||
|
||||
if (!jobLogUrl) {
|
||||
@@ -173,6 +176,6 @@ async function saveLog(
|
||||
logCount
|
||||
)
|
||||
|
||||
logger.info(`Writing logs to ${logPath}`)
|
||||
await createFile(logPath, log)
|
||||
logger.info(`Writing logs to ${logFilePath}`)
|
||||
await createFile(logFilePath, log)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user