@sasjs/adapter 4.19.3 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ContextManager.js +290 -0
- package/DISCLOSURE +45 -0
- package/README.md +1 -1
- package/SAS9ApiClient.d.ts +1 -1
- package/SAS9ApiClient.js +90 -0
- package/SASViyaApiClient.js +918 -0
- package/SASjs.js +794 -0
- package/SASjsApiClient.d.ts +1 -0
- package/SASjsApiClient.js +132 -0
- package/SessionManager.js +353 -0
- package/api/viya/executeOnComputeApi.js +198 -0
- package/api/viya/getFileStream.js +32 -0
- package/api/viya/pollJobState.js +247 -0
- package/api/viya/saveLog.js +45 -0
- package/api/viya/uploadTables.js +45 -0
- package/api/viya/writeStream.js +25 -0
- package/auth/AuthManager.js +305 -0
- package/auth/getAccessTokenForSasjs.js +43 -0
- package/auth/getAccessTokenForViya.js +54 -0
- package/auth/getTokenRequestErrorPrefix.js +70 -0
- package/auth/getTokens.js +110 -0
- package/auth/index.js +20 -0
- package/auth/isAuthorizeFormRequired.js +7 -0
- package/auth/isLoginRequired.js +9 -0
- package/auth/loginHeader.js +87 -0
- package/auth/openWebPage.js +31 -0
- package/auth/refreshTokensForSasjs.js +42 -0
- package/auth/refreshTokensForViya.js +53 -0
- package/auth/verifySas9Login.js +32 -0
- package/auth/verifySasViyaLogin.js +44 -0
- package/dist/minified_sas9.js +10892 -0
- package/dist/sasjs-adapter.js +19569 -0
- package/file/generateFileUploadForm.js +43 -0
- package/file/generateTableUploadForm.js +38 -0
- package/index.js +9 -80376
- package/job-execution/ComputeJobExecutor.js +55 -0
- package/job-execution/FileUploader.js +114 -0
- package/job-execution/JesJobExecutor.js +55 -0
- package/job-execution/JobExecutor.js +40 -0
- package/job-execution/Sas9JobExecutor.d.ts +1 -1
- package/job-execution/Sas9JobExecutor.js +115 -0
- package/job-execution/SasjsJobExecutor.js +100 -0
- package/job-execution/WebJobExecutor.js +254 -0
- package/job-execution/index.js +23 -0
- package/minified/sas9/SASjs.js +198 -0
- package/minified/sas9/WebJobExecutor.js +105 -0
- package/minified/sas9/index.js +22 -0
- package/node/index.d.ts +2 -9
- package/node/index.js +1 -30570
- package/package.json +41 -12
- package/request/RequestClient.d.ts +1 -1
- package/request/RequestClient.js +717 -0
- package/request/Sas9RequestClient.d.ts +1 -1
- package/request/Sas9RequestClient.js +121 -0
- package/request/SasjsRequestClient.js +77 -0
- package/types/Context.js +2 -0
- package/types/CsrfToken.js +2 -0
- package/types/ExecuteScript.js +2 -0
- package/types/File.js +2 -0
- package/types/FileResource.js +2 -0
- package/types/Folder.js +2 -0
- package/types/Job.js +2 -0
- package/types/JobDefinition.js +2 -0
- package/types/JobResult.js +2 -0
- package/types/Link.js +2 -0
- package/types/LogStatistics.js +2 -0
- package/types/Login.js +2 -0
- package/types/PollOptions.js +2 -0
- package/types/RequestClient.js +2 -0
- package/types/SASjsConfig.d.ts +1 -1
- package/types/SASjsConfig.js +91 -0
- package/types/Session.js +14 -0
- package/types/Tables.js +28 -0
- package/types/UploadFile.js +2 -0
- package/types/WriteStream.d.ts +1 -1
- package/types/WriteStream.js +2 -0
- package/types/errors/ArgumentError.js +12 -0
- package/types/errors/AuthorizeError.js +13 -0
- package/types/errors/CertificateError.js +12 -0
- package/types/errors/ComputeJobExecutionError.js +13 -0
- package/types/errors/ErrorResponse.js +23 -0
- package/types/errors/InternalServerError.js +11 -0
- package/types/errors/InvalidJsonError.js +11 -0
- package/types/errors/InvalidSASjsCsrfError.js +12 -0
- package/types/errors/JobExecutionError.js +14 -0
- package/types/errors/JobStatePollError.js +12 -0
- package/types/errors/JsonParseArrayError.js +11 -0
- package/types/errors/LoginRequiredError.js +14 -0
- package/types/errors/NoSessionStateError.js +14 -0
- package/types/errors/NotFoundError.js +12 -0
- package/types/errors/RootFolderNotFoundError.js +22 -0
- package/types/errors/SAS9AuthError.js +11 -0
- package/types/errors/WeboutResponseError.js +12 -0
- package/types/errors/index.js +31 -0
- package/types/index.js +34 -0
- package/utils/appendExtraResponseAttributes.js +25 -0
- package/utils/asyncForEach.js +20 -0
- package/utils/compareTimestamps.js +11 -0
- package/utils/convertToCsv.js +154 -0
- package/utils/createAxiosInstance.d.ts +1 -1
- package/utils/createAxiosInstance.js +9 -0
- package/utils/delay.js +5 -0
- package/{src/utils/extractWeboutBlob.ts → utils/extractWeboutBlob.js} +19 -20
- package/utils/fetchLogByChunks.js +56 -0
- package/utils/formatDataForRequest.js +39 -0
- package/utils/getFormData.js +10 -0
- package/{node/utils/getUserLanguage.d.ts → utils/getUserLanguage.js} +5 -1
- package/utils/getValidJson.js +27 -0
- package/utils/host.d.ts +24 -0
- package/utils/host.js +36 -0
- package/utils/index.js +41 -0
- package/utils/isIeOrEdge.js +30 -0
- package/utils/isNode.js +7 -0
- package/utils/isRelativePath.js +5 -0
- package/utils/isUri.js +9 -0
- package/utils/isUrl.js +20 -0
- package/utils/loginPrompt/index.js +180 -0
- package/utils/needsRetry.js +14 -0
- package/utils/parseGeneratedCode.js +10 -0
- package/utils/parseSasViyaLog.js +17 -0
- package/utils/parseSourceCode.js +9 -0
- package/utils/parseViyaDebugResponse.js +46 -0
- package/utils/parseViyaLogDebugResponse.js +32 -0
- package/utils/parseWeboutResponse.js +22 -0
- package/utils/sas9/extractUserLongNameSas9.js +30 -0
- package/utils/serialize.js +20 -0
- package/utils/splitChunks.js +13 -0
- package/utils/validateInput.js +83 -0
- package/.env.example +0 -2
- package/.git-hooks/commit-msg +0 -39
- package/.git-hooks/pre-commit +0 -12
- package/.gitattributes +0 -1
- package/.pre-commit-config.yaml +0 -25
- package/.vscode/settings.json +0 -3
- package/SASViyaApiClient.spec.d.ts +0 -1
- package/__mocks__/axios.d.ts +0 -3
- package/api/viya/spec/executeScript.spec.d.ts +0 -1
- package/api/viya/spec/getFileStream.spec.d.ts +0 -1
- package/api/viya/spec/mockResponses.d.ts +0 -10
- package/api/viya/spec/pollJobState.spec.d.ts +0 -1
- package/api/viya/spec/saveLog.spec.d.ts +0 -1
- package/api/viya/spec/uploadTables.spec.d.ts +0 -1
- package/api/viya/spec/writeStream.spec.d.ts +0 -1
- package/auth/spec/AuthManager.spec.d.ts +0 -4
- package/auth/spec/getAccessTokenForSasjs.spec.d.ts +0 -1
- package/auth/spec/getAccessTokenForViya.spec.d.ts +0 -1
- package/auth/spec/getTokenRequestErrorPrefix.spec.d.ts +0 -1
- package/auth/spec/getTokens.spec.d.ts +0 -1
- package/auth/spec/loginHeader.spec.d.ts +0 -4
- package/auth/spec/mockResponses.d.ts +0 -23
- package/auth/spec/openWebPage.spec.d.ts +0 -1
- package/auth/spec/refreshTokensForSasjs.spec.d.ts +0 -1
- package/auth/spec/refreshTokensForViya.spec.d.ts +0 -1
- package/auth/spec/verifySas9Login.spec.d.ts +0 -1
- package/auth/spec/verifySasViyaLogin.spec.d.ts +0 -1
- package/checkNodeVersion.js +0 -16
- package/createTSDocs.js +0 -35
- package/cypress.config.js +0 -18
- package/example.html +0 -109
- package/file/spec/generateFileUploadForm.spec.d.ts +0 -1
- package/file/spec/generateTableUploadForm.spec.d.ts +0 -1
- package/index-dev.js +0 -80394
- package/job-execution/spec/ComputeJobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/JesJobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/Sas9JobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/SasjsJobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/WebJobExecutor.debug.spec.d.ts +0 -1
- package/job-execution/spec/WebJobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/executionTasks.spec.d.ts +0 -1
- package/minified_sas9.js +0 -14039
- package/node/ContextManager.d.ts +0 -41
- package/node/SAS9ApiClient.d.ts +0 -30
- package/node/SASViyaApiClient.d.ts +0 -298
- package/node/SASjs.d.ts +0 -387
- package/node/SASjsApiClient.d.ts +0 -65
- package/node/SessionManager.d.ts +0 -115
- package/node/api/viya/executeOnComputeApi.d.ts +0 -21
- package/node/api/viya/getFileStream.d.ts +0 -2
- package/node/api/viya/pollJobState.d.ts +0 -57
- package/node/api/viya/saveLog.d.ts +0 -15
- package/node/api/viya/uploadTables.d.ts +0 -13
- package/node/api/viya/writeStream.d.ts +0 -2
- package/node/auth/AuthManager.d.ts +0 -49
- package/node/auth/getAccessTokenForSasjs.d.ts +0 -11
- package/node/auth/getAccessTokenForViya.d.ts +0 -10
- package/node/auth/getTokenRequestErrorPrefix.d.ts +0 -44
- package/node/auth/getTokens.d.ts +0 -22
- package/node/auth/index.d.ts +0 -4
- package/node/auth/isAuthorizeFormRequired.d.ts +0 -1
- package/node/auth/isLoginRequired.d.ts +0 -1
- package/node/auth/loginHeader.d.ts +0 -17
- package/node/auth/openWebPage.d.ts +0 -6
- package/node/auth/refreshTokensForSasjs.d.ts +0 -10
- package/node/auth/refreshTokensForViya.d.ts +0 -11
- package/node/auth/verifySas9Login.d.ts +0 -3
- package/node/auth/verifySasViyaLogin.d.ts +0 -4
- package/node/file/generateFileUploadForm.d.ts +0 -11
- package/node/file/generateTableUploadForm.d.ts +0 -5
- package/node/job-execution/ComputeJobExecutor.d.ts +0 -8
- package/node/job-execution/FileUploader.d.ts +0 -9
- package/node/job-execution/JesJobExecutor.d.ts +0 -9
- package/node/job-execution/JobExecutor.d.ts +0 -17
- package/node/job-execution/Sas9JobExecutor.d.ts +0 -19
- package/node/job-execution/SasjsJobExecutor.d.ts +0 -9
- package/node/job-execution/WebJobExecutor.d.ts +0 -18
- package/node/job-execution/index.d.ts +0 -7
- package/node/request/RequestClient.d.ts +0 -125
- package/node/request/Sas9RequestClient.d.ts +0 -25
- package/node/request/SasjsRequestClient.d.ts +0 -12
- package/node/types/Context.d.ts +0 -42
- package/node/types/CsrfToken.d.ts +0 -4
- package/node/types/ExecuteScript.d.ts +0 -4
- package/node/types/File.d.ts +0 -7
- package/node/types/FileResource.d.ts +0 -32
- package/node/types/Folder.d.ts +0 -7
- package/node/types/Job.d.ts +0 -14
- package/node/types/JobDefinition.d.ts +0 -3
- package/node/types/JobResult.d.ts +0 -3
- package/node/types/Link.d.ts +0 -7
- package/node/types/LogStatistics.d.ts +0 -4
- package/node/types/Login.d.ts +0 -14
- package/node/types/PollOptions.d.ts +0 -8
- package/node/types/RequestClient.d.ts +0 -46
- package/node/types/SASjsConfig.d.ts +0 -94
- package/node/types/Session.d.ts +0 -30
- package/node/types/Tables.d.ts +0 -7
- package/node/types/UploadFile.d.ts +0 -8
- package/node/types/WriteStream.d.ts +0 -7
- package/node/types/errors/ArgumentError.d.ts +0 -4
- package/node/types/errors/AuthorizeError.d.ts +0 -5
- package/node/types/errors/CertificateError.d.ts +0 -3
- package/node/types/errors/ComputeJobExecutionError.d.ts +0 -6
- package/node/types/errors/ErrorResponse.d.ts +0 -9
- package/node/types/errors/InternalServerError.d.ts +0 -3
- package/node/types/errors/InvalidJsonError.d.ts +0 -3
- package/node/types/errors/InvalidSASjsCsrfError.d.ts +0 -3
- package/node/types/errors/JobExecutionError.d.ts +0 -6
- package/node/types/errors/JobStatePollError.d.ts +0 -4
- package/node/types/errors/JsonParseArrayError.d.ts +0 -3
- package/node/types/errors/LoginRequiredError.d.ts +0 -3
- package/node/types/errors/NoSessionStateError.d.ts +0 -6
- package/node/types/errors/NotFoundError.d.ts +0 -4
- package/node/types/errors/RootFolderNotFoundError.d.ts +0 -3
- package/node/types/errors/SAS9AuthError.d.ts +0 -3
- package/node/types/errors/WeboutResponseError.d.ts +0 -4
- package/node/types/errors/index.d.ts +0 -15
- package/node/types/index.d.ts +0 -18
- package/node/utils/appendExtraResponseAttributes.d.ts +0 -2
- package/node/utils/asyncForEach.d.ts +0 -1
- package/node/utils/compareTimestamps.d.ts +0 -6
- package/node/utils/convertToCsv.d.ts +0 -12
- package/node/utils/createAxiosInstance.d.ts +0 -2
- package/node/utils/delay.d.ts +0 -1
- package/node/utils/extractWeboutBlob.d.ts +0 -13
- package/node/utils/fetchLogByChunks.d.ts +0 -20
- package/node/utils/formatDataForRequest.d.ts +0 -1
- package/node/utils/getFormData.d.ts +0 -2
- package/node/utils/getValidJson.d.ts +0 -5
- package/node/utils/index.d.ts +0 -25
- package/node/utils/isNode.d.ts +0 -1
- package/node/utils/isRelativePath.d.ts +0 -1
- package/node/utils/isUri.d.ts +0 -5
- package/node/utils/isUrl.d.ts +0 -5
- package/node/utils/loginPrompt/index.d.ts +0 -1
- package/node/utils/needsRetry.d.ts +0 -1
- package/node/utils/parseGeneratedCode.d.ts +0 -1
- package/node/utils/parseSasViyaLog.d.ts +0 -5
- package/node/utils/parseSourceCode.d.ts +0 -1
- package/node/utils/parseViyaDebugResponse.d.ts +0 -12
- package/node/utils/parseViyaLogDebugResponse.d.ts +0 -8
- package/node/utils/parseWeboutResponse.d.ts +0 -1
- package/node/utils/sas9/extractUserLongNameSas9.d.ts +0 -6
- package/node/utils/serialize.d.ts +0 -1
- package/node/utils/splitChunks.d.ts +0 -1
- package/node/utils/validateInput.d.ts +0 -14
- package/request/spec/SasjsRequestClient.spec.d.ts +0 -1
- package/screenshots/session-manager-first-request.png +0 -0
- package/screenshots/subsequent-session-request.png +0 -0
- package/spec/SAS9ApiClient.spec.d.ts +0 -1
- package/spec/SASjs.startComputeJob.spec.d.ts +0 -1
- package/spec/SASjsApiClient.spec.d.ts +0 -1
- package/src/ContextManager.ts +0 -454
- package/src/SAS9ApiClient.ts +0 -100
- package/src/SASViyaApiClient.ts +0 -1417
- package/src/SASjs.ts +0 -1268
- package/src/SASjsApiClient.ts +0 -177
- package/src/SessionManager.ts +0 -465
- package/src/__mocks__/axios.ts +0 -7
- package/src/api/viya/executeOnComputeApi.ts +0 -307
- package/src/api/viya/getFileStream.ts +0 -17
- package/src/api/viya/pollJobState.ts +0 -393
- package/src/api/viya/saveLog.ts +0 -55
- package/src/api/viya/spec/mockResponses.ts +0 -75
- package/src/api/viya/uploadTables.ts +0 -41
- package/src/api/viya/writeStream.ts +0 -16
- package/src/auth/AuthManager.ts +0 -387
- package/src/auth/getAccessTokenForSasjs.ts +0 -48
- package/src/auth/getAccessTokenForViya.ts +0 -61
- package/src/auth/getTokenRequestErrorPrefix.ts +0 -88
- package/src/auth/getTokens.ts +0 -127
- package/src/auth/index.ts +0 -4
- package/src/auth/isAuthorizeFormRequired.ts +0 -3
- package/src/auth/isLoginRequired.ts +0 -5
- package/src/auth/loginHeader.ts +0 -97
- package/src/auth/openWebPage.ts +0 -40
- package/src/auth/refreshTokensForSasjs.ts +0 -45
- package/src/auth/refreshTokensForViya.ts +0 -66
- package/src/auth/spec/mockResponses.ts +0 -53
- package/src/auth/verifySas9Login.ts +0 -25
- package/src/auth/verifySasViyaLogin.ts +0 -43
- package/src/file/generateFileUploadForm.ts +0 -48
- package/src/file/generateTableUploadForm.ts +0 -50
- package/src/index.ts +0 -9
- package/src/job-execution/ComputeJobExecutor.ts +0 -76
- package/src/job-execution/FileUploader.ts +0 -152
- package/src/job-execution/JesJobExecutor.ts +0 -78
- package/src/job-execution/JobExecutor.ts +0 -63
- package/src/job-execution/Sas9JobExecutor.ts +0 -137
- package/src/job-execution/SasjsJobExecutor.ts +0 -152
- package/src/job-execution/WebJobExecutor.ts +0 -341
- package/src/job-execution/index.ts +0 -7
- package/src/minified/sas9/SASjs.ts +0 -274
- package/src/minified/sas9/WebJobExecutor.ts +0 -156
- package/src/minified/sas9/index.ts +0 -3
- package/src/request/RequestClient.ts +0 -941
- package/src/request/Sas9RequestClient.ts +0 -132
- package/src/request/SasjsRequestClient.ts +0 -82
- package/src/test/SAS_server_app.ts +0 -47
- package/src/types/Context.ts +0 -39
- package/src/types/CsrfToken.ts +0 -4
- package/src/types/ExecuteScript.ts +0 -4
- package/src/types/File.ts +0 -8
- package/src/types/FileResource.ts +0 -33
- package/src/types/Folder.ts +0 -8
- package/src/types/Job.ts +0 -15
- package/src/types/JobDefinition.ts +0 -3
- package/src/types/JobResult.ts +0 -3
- package/src/types/Link.ts +0 -7
- package/src/types/LogStatistics.ts +0 -4
- package/src/types/Login.ts +0 -15
- package/src/types/PollOptions.ts +0 -9
- package/src/types/Process.d.ts +0 -5
- package/src/types/RequestClient.ts +0 -55
- package/src/types/SASjsConfig.ts +0 -95
- package/src/types/Session.ts +0 -34
- package/src/types/Tables.ts +0 -29
- package/src/types/UploadFile.ts +0 -8
- package/src/types/WriteStream.ts +0 -10
- package/src/types/errors/ArgumentError.ts +0 -7
- package/src/types/errors/AuthorizeError.ts +0 -10
- package/src/types/errors/CertificateError.ts +0 -12
- package/src/types/errors/ComputeJobExecutionError.ts +0 -12
- package/src/types/errors/ErrorResponse.ts +0 -28
- package/src/types/errors/InternalServerError.ts +0 -9
- package/src/types/errors/InvalidJsonError.ts +0 -7
- package/src/types/errors/InvalidSASjsCsrfError.ts +0 -9
- package/src/types/errors/JobExecutionError.ts +0 -11
- package/src/types/errors/JobStatePollError.ts +0 -14
- package/src/types/errors/JsonParseArrayError.ts +0 -7
- package/src/types/errors/LoginRequiredError.ts +0 -11
- package/src/types/errors/NoSessionStateError.ts +0 -15
- package/src/types/errors/NotFoundError.ts +0 -7
- package/src/types/errors/RootFolderNotFoundError.ts +0 -24
- package/src/types/errors/SAS9AuthError.ts +0 -9
- package/src/types/errors/WeboutResponseError.ts +0 -7
- package/src/types/errors/index.ts +0 -15
- package/src/types/index.ts +0 -18
- package/src/types/system/global.d.ts +0 -1
- package/src/utils/appendExtraResponseAttributes.ts +0 -22
- package/src/utils/asyncForEach.ts +0 -5
- package/src/utils/compareTimestamps.ts +0 -9
- package/src/utils/convertToCsv.ts +0 -198
- package/src/utils/createAxiosInstance.ts +0 -7
- package/src/utils/delay.ts +0 -2
- package/src/utils/fetchLogByChunks.ts +0 -63
- package/src/utils/formatDataForRequest.ts +0 -48
- package/src/utils/getFormData.ts +0 -5
- package/src/utils/getUserLanguage.ts +0 -10
- package/src/utils/getValidJson.ts +0 -23
- package/src/utils/index.ts +0 -25
- package/src/utils/isIeOrEdge.ts +0 -34
- package/src/utils/isNode.ts +0 -4
- package/src/utils/isRelativePath.ts +0 -2
- package/src/utils/isUri.ts +0 -5
- package/src/utils/isUrl.ts +0 -17
- package/src/utils/loginPrompt/index.ts +0 -177
- package/src/utils/needsRetry.ts +0 -14
- package/src/utils/parseGeneratedCode.ts +0 -7
- package/src/utils/parseSasViyaLog.ts +0 -12
- package/src/utils/parseSourceCode.ts +0 -6
- package/src/utils/parseViyaDebugResponse.ts +0 -46
- package/src/utils/parseViyaLogDebugResponse.ts +0 -22
- package/src/utils/parseWeboutResponse.ts +0 -20
- package/src/utils/sas9/extractUserLongNameSas9.ts +0 -31
- package/src/utils/serialize.ts +0 -15
- package/src/utils/splitChunks.ts +0 -12
- package/src/utils/validateInput.ts +0 -93
- package/test/ContextManager.spec.d.ts +0 -1
- package/test/FileUploader.spec.d.ts +0 -4
- package/test/FolderOperations.spec.d.ts +0 -1
- package/test/RequestClient.spec.d.ts +0 -1
- package/test/SAS_server_app.d.ts +0 -10
- package/test/SessionManager.spec.d.ts +0 -1
- package/test/getJobsInFolder.spec.d.ts +0 -1
- package/test/utils/formatDataForRequest.spec.d.ts +0 -1
- package/test/utils/getValidJson.spec.d.ts +0 -1
- package/test/utils/isUrl.spec.d.ts +0 -1
- package/test/utils/parseGeneratedCode.spec.d.ts +0 -1
- package/test/utils/parseSourceCode.spec.d.ts +0 -1
- package/types/Tables.spec.d.ts +0 -1
- package/types/errors/RootFolderNotFoundError.spec.d.ts +0 -1
- package/types/errors/spec/SAS9AuthError.spec.d.ts +0 -1
- package/utils/spec/convertToCsv.spec.d.ts +0 -1
- package/utils/spec/extractUserLongNameSas9.spec.d.ts +0 -1
- package/utils/spec/getFormData.spec.d.ts +0 -1
- package/utils/spec/parseSasViyaLog.spec.d.ts +0 -1
- package/utils/spec/parseViyaDebugResponse.spec.d.ts +0 -1
- package/utils/spec/parseViyaLogDebugResponse.spec.d.ts +0 -1
- package/utils/spec/validateInput.spec.d.ts +0 -1
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
|
|
2
|
-
import { AuthConfig, MacroVar, ServerType } from '@sasjs/utils/types'
|
|
3
|
-
import { prefixMessage } from '@sasjs/utils/error'
|
|
4
|
-
import {
|
|
5
|
-
PollOptions,
|
|
6
|
-
Job,
|
|
7
|
-
ComputeJobExecutionError,
|
|
8
|
-
NotFoundError
|
|
9
|
-
} from '../..'
|
|
10
|
-
import { getTokens, OnTokensRefreshed } from '../../auth/getTokens'
|
|
11
|
-
import { RequestClient } from '../../request/RequestClient'
|
|
12
|
-
import { SessionManager } from '../../SessionManager'
|
|
13
|
-
import { isRelativePath, fetchLogByChunks } from '../../utils'
|
|
14
|
-
import { formatDataForRequest } from '../../utils/formatDataForRequest'
|
|
15
|
-
import { pollJobState, JobState } from './pollJobState'
|
|
16
|
-
import { uploadTables } from './uploadTables'
|
|
17
|
-
|
|
18
|
-
interface JobRequestBody {
|
|
19
|
-
[key: string]: number | string | string[]
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Executes SAS program on the current SAS Viya server using Compute API.
|
|
24
|
-
* @param jobPath - the path to the file being submitted for execution.
|
|
25
|
-
* @param linesOfCode - an array of code lines to execute.
|
|
26
|
-
* @param contextName - the context to execute the code in.
|
|
27
|
-
* @param authConfig - an object containing an access token, refresh token, client ID and secret.
|
|
28
|
-
* @param data - execution data.
|
|
29
|
-
* @param debug - when set to true, the log will be returned.
|
|
30
|
-
* @param expectWebout - when set to true, the automatic _webout fileref will be checked for content, and that content returned. This fileref is used when the Job contains a SASjs web request (as opposed to executing arbitrary SAS code).
|
|
31
|
-
* @param waitForResult - when set to true, function will return the session
|
|
32
|
-
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts.
|
|
33
|
-
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
|
34
|
-
* @param variables - an object that represents macro variables.
|
|
35
|
-
* @param onTokensRefreshed - optional callback invoked with the rotated token pair after a successful internal refresh, so consumers that persist tokens can store the new pair.
|
|
36
|
-
*/
|
|
37
|
-
export async function executeOnComputeApi(
|
|
38
|
-
requestClient: RequestClient,
|
|
39
|
-
sessionManager: SessionManager,
|
|
40
|
-
rootFolderName: string,
|
|
41
|
-
jobPath: string,
|
|
42
|
-
linesOfCode: string[],
|
|
43
|
-
contextName: string,
|
|
44
|
-
authConfig?: AuthConfig,
|
|
45
|
-
data: any = null,
|
|
46
|
-
debug: boolean = false,
|
|
47
|
-
expectWebout = false,
|
|
48
|
-
waitForResult = true,
|
|
49
|
-
pollOptions?: PollOptions,
|
|
50
|
-
printPid = false,
|
|
51
|
-
variables?: MacroVar,
|
|
52
|
-
onTokensRefreshed?: OnTokensRefreshed
|
|
53
|
-
): Promise<any> {
|
|
54
|
-
let access_token = (authConfig || {}).access_token
|
|
55
|
-
|
|
56
|
-
if (authConfig) {
|
|
57
|
-
;({ access_token } = await getTokens(
|
|
58
|
-
requestClient,
|
|
59
|
-
authConfig,
|
|
60
|
-
ServerType.SasViya,
|
|
61
|
-
onTokensRefreshed
|
|
62
|
-
))
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const logger = process.logger || console
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
let executionSessionId: string
|
|
69
|
-
|
|
70
|
-
const session = await sessionManager
|
|
71
|
-
.getSession(access_token)
|
|
72
|
-
.catch((err) => {
|
|
73
|
-
throw prefixMessage(err, 'Error while getting session. ')
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
executionSessionId = session!.id
|
|
77
|
-
|
|
78
|
-
if (printPid) {
|
|
79
|
-
const { result: jobIdVariable } = await sessionManager
|
|
80
|
-
.getVariable(executionSessionId, 'SYSJOBID', access_token)
|
|
81
|
-
.catch((err) => {
|
|
82
|
-
throw prefixMessage(err, 'Error while getting session variable. ')
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
if (jobIdVariable && jobIdVariable.value) {
|
|
86
|
-
const relativeJobPath = rootFolderName
|
|
87
|
-
? jobPath.split(rootFolderName).join('').replace(/^\//, '')
|
|
88
|
-
: jobPath
|
|
89
|
-
|
|
90
|
-
const logger = process.logger || console
|
|
91
|
-
|
|
92
|
-
logger.info(
|
|
93
|
-
`Triggering '${relativeJobPath}' with PID ${
|
|
94
|
-
jobIdVariable.value
|
|
95
|
-
} at ${timestampToYYYYMMDDHHMMSS()}`
|
|
96
|
-
)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
let fileName
|
|
101
|
-
|
|
102
|
-
if (isRelativePath(jobPath)) {
|
|
103
|
-
fileName = `exec-${
|
|
104
|
-
jobPath.includes('/') ? jobPath.split('/')[1] : jobPath
|
|
105
|
-
}`
|
|
106
|
-
} else {
|
|
107
|
-
const jobPathParts = jobPath.split('/')
|
|
108
|
-
|
|
109
|
-
fileName = jobPathParts.pop()
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
let jobVariables: any = {
|
|
113
|
-
SYS_JES_JOB_URI: '',
|
|
114
|
-
_program: isRelativePath(jobPath)
|
|
115
|
-
? rootFolderName + '/' + jobPath
|
|
116
|
-
: jobPath
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (variables) jobVariables = { ...jobVariables, ...variables }
|
|
120
|
-
if (debug) jobVariables = { ...jobVariables, _DEBUG: 131 }
|
|
121
|
-
|
|
122
|
-
let files: any[] = []
|
|
123
|
-
|
|
124
|
-
if (data) {
|
|
125
|
-
if (JSON.stringify(data).includes(';')) {
|
|
126
|
-
files = await uploadTables(requestClient, data, access_token).catch(
|
|
127
|
-
(err) => {
|
|
128
|
-
throw prefixMessage(err, 'Error while uploading tables. ')
|
|
129
|
-
}
|
|
130
|
-
)
|
|
131
|
-
|
|
132
|
-
jobVariables['_webin_file_count'] = files.length
|
|
133
|
-
|
|
134
|
-
files.forEach((fileInfo, index) => {
|
|
135
|
-
jobVariables[`_webin_fileuri${index + 1}`] =
|
|
136
|
-
`/files/files/${fileInfo.file.id}`
|
|
137
|
-
jobVariables[`_webin_name${index + 1}`] = fileInfo.tableName
|
|
138
|
-
})
|
|
139
|
-
} else {
|
|
140
|
-
jobVariables = { ...jobVariables, ...formatDataForRequest(data) }
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Execute job in session
|
|
145
|
-
const jobRequestBody: JobRequestBody = {
|
|
146
|
-
name: fileName || 'Default Job Name',
|
|
147
|
-
description: 'Powered by SASjs',
|
|
148
|
-
code: linesOfCode,
|
|
149
|
-
variables: jobVariables,
|
|
150
|
-
version: 2
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const { result: postedJob, etag } = await requestClient
|
|
154
|
-
.post<Job>(
|
|
155
|
-
`/compute/sessions/${executionSessionId}/jobs`,
|
|
156
|
-
jobRequestBody,
|
|
157
|
-
access_token
|
|
158
|
-
)
|
|
159
|
-
.catch((err) => {
|
|
160
|
-
throw prefixMessage(err, 'Error while posting job. ')
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
if (!waitForResult) return session
|
|
164
|
-
|
|
165
|
-
if (debug) {
|
|
166
|
-
logger.info(`Job has been submitted for '${fileName}'.`)
|
|
167
|
-
logger.info(
|
|
168
|
-
`You can monitor the job progress at '${requestClient.getBaseUrl()}${
|
|
169
|
-
postedJob.links.find((l: any) => l.rel === 'state')!.href
|
|
170
|
-
}'.`
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const jobStatus = await pollJobState(
|
|
175
|
-
requestClient,
|
|
176
|
-
postedJob,
|
|
177
|
-
debug,
|
|
178
|
-
authConfig,
|
|
179
|
-
pollOptions,
|
|
180
|
-
{
|
|
181
|
-
session,
|
|
182
|
-
sessionManager
|
|
183
|
-
},
|
|
184
|
-
onTokensRefreshed
|
|
185
|
-
).catch(async (err) => {
|
|
186
|
-
const error = err?.response?.data
|
|
187
|
-
const result = /err=[0-9]*,/.exec(error)
|
|
188
|
-
const errorCode = '5113'
|
|
189
|
-
|
|
190
|
-
if (result?.[0]?.slice(4, -1) === errorCode) {
|
|
191
|
-
const logCount = 1000000
|
|
192
|
-
const sessionLogUrl =
|
|
193
|
-
postedJob.links.find((l: any) => l.rel === 'up')!.href + '/log'
|
|
194
|
-
|
|
195
|
-
err.log = await fetchLogByChunks(
|
|
196
|
-
requestClient,
|
|
197
|
-
access_token!,
|
|
198
|
-
sessionLogUrl,
|
|
199
|
-
logCount
|
|
200
|
-
)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
throw prefixMessage(err, 'Error while polling job status. ')
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
if (authConfig) {
|
|
207
|
-
;({ access_token } = await getTokens(
|
|
208
|
-
requestClient,
|
|
209
|
-
authConfig,
|
|
210
|
-
ServerType.SasViya,
|
|
211
|
-
onTokensRefreshed
|
|
212
|
-
))
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const { result: currentJob } = await requestClient
|
|
216
|
-
.get<Job>(
|
|
217
|
-
`/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`,
|
|
218
|
-
access_token
|
|
219
|
-
)
|
|
220
|
-
.catch((err) => {
|
|
221
|
-
throw prefixMessage(err, 'Error while getting job. ')
|
|
222
|
-
})
|
|
223
|
-
|
|
224
|
-
let jobResult
|
|
225
|
-
let log = ''
|
|
226
|
-
const logLink = currentJob.links.find((l) => l.rel === 'log')
|
|
227
|
-
|
|
228
|
-
if (debug && logLink) {
|
|
229
|
-
const logUrl = `${logLink.href}/content`
|
|
230
|
-
const logCount = currentJob.logStatistics?.lineCount ?? 1000000
|
|
231
|
-
|
|
232
|
-
log = await fetchLogByChunks(
|
|
233
|
-
requestClient,
|
|
234
|
-
access_token!,
|
|
235
|
-
logUrl,
|
|
236
|
-
logCount
|
|
237
|
-
)
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (jobStatus === JobState.Failed || jobStatus === JobState.Error) {
|
|
241
|
-
throw new ComputeJobExecutionError(currentJob, log)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (!expectWebout) return { job: currentJob, log }
|
|
245
|
-
|
|
246
|
-
const resultLink = `/compute/sessions/${executionSessionId}/filerefs/_webout/content`
|
|
247
|
-
|
|
248
|
-
jobResult = await requestClient
|
|
249
|
-
.get<any>(resultLink, access_token, 'text/plain')
|
|
250
|
-
.catch(async (e: any) => {
|
|
251
|
-
if (e instanceof NotFoundError) {
|
|
252
|
-
if (logLink) {
|
|
253
|
-
const logUrl = `${logLink.href}/content`
|
|
254
|
-
const logCount = currentJob.logStatistics?.lineCount ?? 1000000
|
|
255
|
-
|
|
256
|
-
log = await fetchLogByChunks(
|
|
257
|
-
requestClient,
|
|
258
|
-
access_token!,
|
|
259
|
-
logUrl,
|
|
260
|
-
logCount
|
|
261
|
-
)
|
|
262
|
-
|
|
263
|
-
return Promise.reject({
|
|
264
|
-
status: 500,
|
|
265
|
-
log
|
|
266
|
-
})
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return {
|
|
271
|
-
result: JSON.stringify(e)
|
|
272
|
-
}
|
|
273
|
-
})
|
|
274
|
-
|
|
275
|
-
await sessionManager
|
|
276
|
-
.clearSession(executionSessionId, access_token)
|
|
277
|
-
.catch((err) => {
|
|
278
|
-
throw prefixMessage(err, 'Error while clearing session. ')
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
return { result: jobResult?.result, log }
|
|
282
|
-
} catch (e: any) {
|
|
283
|
-
interface HttpError {
|
|
284
|
-
status: number
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
const error = e as HttpError
|
|
288
|
-
|
|
289
|
-
if (error.status === 404) {
|
|
290
|
-
return executeOnComputeApi(
|
|
291
|
-
requestClient,
|
|
292
|
-
sessionManager,
|
|
293
|
-
rootFolderName,
|
|
294
|
-
jobPath,
|
|
295
|
-
linesOfCode,
|
|
296
|
-
contextName,
|
|
297
|
-
authConfig,
|
|
298
|
-
data,
|
|
299
|
-
debug,
|
|
300
|
-
false,
|
|
301
|
-
true
|
|
302
|
-
)
|
|
303
|
-
} else {
|
|
304
|
-
throw prefixMessage(e as Error, 'Error while executing script. ')
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { isFolder } from '@sasjs/utils/file'
|
|
2
|
-
import { generateTimestamp } from '@sasjs/utils/time'
|
|
3
|
-
import { Job } from '../../types'
|
|
4
|
-
|
|
5
|
-
export const getFileStream = async (job: Job, filePath?: string) => {
|
|
6
|
-
const { createWriteStream } = require('@sasjs/utils/file')
|
|
7
|
-
const logPath = filePath || process.cwd()
|
|
8
|
-
const isFolderPath = await isFolder(logPath)
|
|
9
|
-
if (isFolderPath) {
|
|
10
|
-
const logFileName = `${job.name || 'job'}-${generateTimestamp()}.log`
|
|
11
|
-
const path = require('path')
|
|
12
|
-
const logFilePath = path.join(filePath || process.cwd(), logFileName)
|
|
13
|
-
return await createWriteStream(logFilePath)
|
|
14
|
-
} else {
|
|
15
|
-
return await createWriteStream(logPath)
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,393 +0,0 @@
|
|
|
1
|
-
import { AuthConfig, ServerType } from '@sasjs/utils/types'
|
|
2
|
-
import { Job, PollOptions, PollStrategy } from '../..'
|
|
3
|
-
import { getTokens, OnTokensRefreshed } from '../../auth/getTokens'
|
|
4
|
-
import { RequestClient } from '../../request/RequestClient'
|
|
5
|
-
import { JobStatePollError } from '../../types/errors'
|
|
6
|
-
import { Link, WriteStream, SessionState, JobSessionManager } from '../../types'
|
|
7
|
-
import { delay, isNode } from '../../utils'
|
|
8
|
-
|
|
9
|
-
export enum JobState {
|
|
10
|
-
Completed = 'completed',
|
|
11
|
-
Running = 'running',
|
|
12
|
-
Pending = 'pending',
|
|
13
|
-
Unavailable = 'unavailable',
|
|
14
|
-
NoState = '',
|
|
15
|
-
Failed = 'failed',
|
|
16
|
-
Error = 'error'
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Polls job status using default or provided poll options.
|
|
21
|
-
* @param requestClient - the pre-configured HTTP request client.
|
|
22
|
-
* @param postedJob - the relative or absolute path to the job.
|
|
23
|
-
* @param debug - sets the _debug flag in the job arguments.
|
|
24
|
-
* @param authConfig - an access token, refresh token, client and secret for an authorized user.
|
|
25
|
-
* @param pollOptions - an object containing maxPollCount, pollInterval, streamLog and logFolderPath. It will override the first default poll options in poll strategy if provided.
|
|
26
|
-
* Example pollOptions:
|
|
27
|
-
* {
|
|
28
|
-
* maxPollCount: 200,
|
|
29
|
-
* pollInterval: 300,
|
|
30
|
-
* streamLog: true, // optional, equals to false by default.
|
|
31
|
-
* pollStrategy?: // optional array of poll options that should be applied after 'maxPollCount' of the provided poll options is reached. If not provided the default (see example below) poll strategy will be used.
|
|
32
|
-
* }
|
|
33
|
-
* Example pollStrategy (values used from default poll strategy):
|
|
34
|
-
* [
|
|
35
|
-
* { maxPollCount: 200, pollInterval: 300 }, // approximately ~2 mins (including time to get response (~300ms))
|
|
36
|
-
* { maxPollCount: 300, pollInterval: 3000 }, // approximately ~5.5 mins (including time to get response (~300ms))
|
|
37
|
-
* { maxPollCount: 500, pollInterval: 30000 }, // approximately ~50.5 mins (including time to get response (~300ms))
|
|
38
|
-
* { maxPollCount: 3400, pollInterval: 60000 } // approximately ~3015 mins (~125 hours) (including time to get response (~300ms))
|
|
39
|
-
* ]
|
|
40
|
-
* @param jobSessionManager - job session object containing session object and an instance of Session Manager. Job session object is used to periodically (every 10th job state poll) check parent session state.
|
|
41
|
-
* @returns - a promise which resolves with a job state
|
|
42
|
-
*/
|
|
43
|
-
export async function pollJobState(
|
|
44
|
-
requestClient: RequestClient,
|
|
45
|
-
postedJob: Job,
|
|
46
|
-
debug: boolean,
|
|
47
|
-
authConfig?: AuthConfig,
|
|
48
|
-
pollOptions?: PollOptions,
|
|
49
|
-
jobSessionManager?: JobSessionManager,
|
|
50
|
-
onTokensRefreshed?: OnTokensRefreshed
|
|
51
|
-
): Promise<JobState> {
|
|
52
|
-
const logger = process.logger || console
|
|
53
|
-
|
|
54
|
-
const streamLog = pollOptions?.streamLog || false
|
|
55
|
-
|
|
56
|
-
const defaultPollStrategy: PollStrategy = [
|
|
57
|
-
{ maxPollCount: 200, pollInterval: 300 },
|
|
58
|
-
{ maxPollCount: 300, pollInterval: 3000 },
|
|
59
|
-
{ maxPollCount: 500, pollInterval: 30000 },
|
|
60
|
-
{ maxPollCount: 3400, pollInterval: 60000 }
|
|
61
|
-
]
|
|
62
|
-
|
|
63
|
-
let pollStrategy: PollStrategy
|
|
64
|
-
|
|
65
|
-
if (pollOptions !== undefined) {
|
|
66
|
-
pollStrategy = [pollOptions]
|
|
67
|
-
|
|
68
|
-
let { pollStrategy: providedPollStrategy } = pollOptions
|
|
69
|
-
|
|
70
|
-
if (providedPollStrategy !== undefined) {
|
|
71
|
-
validatePollStrategies(providedPollStrategy)
|
|
72
|
-
|
|
73
|
-
// INFO: sort by 'maxPollCount'
|
|
74
|
-
providedPollStrategy = providedPollStrategy.sort(
|
|
75
|
-
(strategyA: PollOptions, strategyB: PollOptions) =>
|
|
76
|
-
strategyA.maxPollCount - strategyB.maxPollCount
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
pollStrategy = [...pollStrategy, ...providedPollStrategy]
|
|
80
|
-
} else {
|
|
81
|
-
pollStrategy = [...pollStrategy, ...defaultPollStrategy]
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
pollStrategy = defaultPollStrategy
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
let defaultPollOptions: PollOptions = pollStrategy.splice(0, 1)[0]
|
|
88
|
-
|
|
89
|
-
pollOptions = { ...defaultPollOptions, ...(pollOptions || {}) }
|
|
90
|
-
|
|
91
|
-
const stateLink = postedJob.links.find((l: any) => l.rel === 'state')
|
|
92
|
-
if (!stateLink) {
|
|
93
|
-
throw new Error(`Job state link was not found.`)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
let currentState: JobState = await getJobState(
|
|
97
|
-
requestClient,
|
|
98
|
-
postedJob,
|
|
99
|
-
JobState.NoState,
|
|
100
|
-
debug,
|
|
101
|
-
authConfig
|
|
102
|
-
).catch((err) => {
|
|
103
|
-
logger.error(
|
|
104
|
-
`Error fetching job state from ${stateLink.href}. Starting poll, assuming job to be running.`,
|
|
105
|
-
err
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
return JobState.Unavailable
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
let pollCount = 0
|
|
112
|
-
|
|
113
|
-
if (currentState === JobState.Completed) {
|
|
114
|
-
return Promise.resolve(currentState)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
let logFileStream
|
|
118
|
-
if (streamLog && isNode()) {
|
|
119
|
-
const { getFileStream } = require('./getFileStream')
|
|
120
|
-
|
|
121
|
-
logFileStream = await getFileStream(postedJob, pollOptions.logFolderPath)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
let result = await doPoll(
|
|
125
|
-
requestClient,
|
|
126
|
-
postedJob,
|
|
127
|
-
currentState,
|
|
128
|
-
debug,
|
|
129
|
-
pollCount,
|
|
130
|
-
pollOptions,
|
|
131
|
-
authConfig,
|
|
132
|
-
streamLog,
|
|
133
|
-
logFileStream,
|
|
134
|
-
jobSessionManager,
|
|
135
|
-
onTokensRefreshed
|
|
136
|
-
)
|
|
137
|
-
|
|
138
|
-
currentState = result.state
|
|
139
|
-
pollCount = result.pollCount
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
!needsRetry(currentState) ||
|
|
143
|
-
(pollCount >= pollOptions.maxPollCount && !pollStrategy.length)
|
|
144
|
-
) {
|
|
145
|
-
return currentState
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// INFO: If we get to this point, this is a long-running job that needs longer polling.
|
|
149
|
-
// We will resume polling with a bigger interval according to the next polling strategy
|
|
150
|
-
while (pollStrategy.length && needsRetry(currentState)) {
|
|
151
|
-
defaultPollOptions = pollStrategy.splice(0, 1)[0]
|
|
152
|
-
|
|
153
|
-
if (pollOptions) {
|
|
154
|
-
defaultPollOptions.logFolderPath = pollOptions.logFolderPath
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
result = await doPoll(
|
|
158
|
-
requestClient,
|
|
159
|
-
postedJob,
|
|
160
|
-
currentState,
|
|
161
|
-
debug,
|
|
162
|
-
pollCount,
|
|
163
|
-
defaultPollOptions,
|
|
164
|
-
authConfig,
|
|
165
|
-
streamLog,
|
|
166
|
-
logFileStream,
|
|
167
|
-
jobSessionManager,
|
|
168
|
-
onTokensRefreshed
|
|
169
|
-
)
|
|
170
|
-
|
|
171
|
-
currentState = result.state
|
|
172
|
-
pollCount = result.pollCount
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (logFileStream) logFileStream.end()
|
|
176
|
-
|
|
177
|
-
return currentState
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const getJobState = async (
|
|
181
|
-
requestClient: RequestClient,
|
|
182
|
-
job: Job,
|
|
183
|
-
currentState: string,
|
|
184
|
-
debug: boolean,
|
|
185
|
-
authConfig?: AuthConfig,
|
|
186
|
-
onTokensRefreshed?: OnTokensRefreshed
|
|
187
|
-
): Promise<JobState> => {
|
|
188
|
-
const stateLink = job.links.find((l: any) => l.rel === 'state')!
|
|
189
|
-
|
|
190
|
-
if (needsRetry(currentState)) {
|
|
191
|
-
let tokens
|
|
192
|
-
|
|
193
|
-
if (authConfig)
|
|
194
|
-
tokens = await getTokens(
|
|
195
|
-
requestClient,
|
|
196
|
-
authConfig,
|
|
197
|
-
ServerType.SasViya,
|
|
198
|
-
onTokensRefreshed
|
|
199
|
-
)
|
|
200
|
-
|
|
201
|
-
const { result: jobState } = await requestClient
|
|
202
|
-
.get<string>(
|
|
203
|
-
`${stateLink.href}?_action=wait&wait=300`,
|
|
204
|
-
tokens?.access_token,
|
|
205
|
-
'text/plain',
|
|
206
|
-
{},
|
|
207
|
-
debug
|
|
208
|
-
)
|
|
209
|
-
.catch((err) => {
|
|
210
|
-
throw new JobStatePollError(job.id, err)
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
return jobState.trim() as JobState
|
|
214
|
-
} else {
|
|
215
|
-
return currentState as JobState
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const needsRetry = (state: string) =>
|
|
220
|
-
state === JobState.Running ||
|
|
221
|
-
state === JobState.NoState ||
|
|
222
|
-
state === JobState.Pending ||
|
|
223
|
-
state === JobState.Unavailable
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Polls job state.
|
|
227
|
-
* @param requestClient - the pre-configured HTTP request client.
|
|
228
|
-
* @param postedJob - the relative or absolute path to the job.
|
|
229
|
-
* @param currentState - current job state.
|
|
230
|
-
* @param debug - sets the _debug flag in the job arguments.
|
|
231
|
-
* @param pollCount - current poll count.
|
|
232
|
-
* @param pollOptions - an object containing maxPollCount, pollInterval, streamLog and logFolderPath.
|
|
233
|
-
* @param authConfig - an access token, refresh token, client and secret for an authorized user.
|
|
234
|
-
* @param streamLog - indicates if job log should be streamed.
|
|
235
|
-
* @param logStream - job log stream.
|
|
236
|
-
* @param jobSessionManager - job session object containing session object and an instance of Session Manager. Job session object is used to periodically (every 10th job state poll) check parent session state. Session state is considered healthy if it is equal to 'running' or 'idle'.
|
|
237
|
-
* @returns - a promise which resolves with a job state
|
|
238
|
-
*/
|
|
239
|
-
export const doPoll = async (
|
|
240
|
-
requestClient: RequestClient,
|
|
241
|
-
postedJob: Job,
|
|
242
|
-
currentState: JobState,
|
|
243
|
-
debug: boolean,
|
|
244
|
-
pollCount: number,
|
|
245
|
-
pollOptions: PollOptions,
|
|
246
|
-
authConfig?: AuthConfig,
|
|
247
|
-
streamLog?: boolean,
|
|
248
|
-
logStream?: WriteStream,
|
|
249
|
-
jobSessionManager?: JobSessionManager,
|
|
250
|
-
onTokensRefreshed?: OnTokensRefreshed
|
|
251
|
-
): Promise<{ state: JobState; pollCount: number }> => {
|
|
252
|
-
const { maxPollCount, pollInterval } = pollOptions
|
|
253
|
-
const logger = process.logger || console
|
|
254
|
-
const stateLink = postedJob.links.find((l: Link) => l.rel === 'state')!
|
|
255
|
-
let maxErrorCount = 5
|
|
256
|
-
let errorCount = 0
|
|
257
|
-
let state = currentState
|
|
258
|
-
let printedState = JobState.NoState
|
|
259
|
-
let startLogLine = 0
|
|
260
|
-
|
|
261
|
-
while (needsRetry(state) && pollCount <= maxPollCount) {
|
|
262
|
-
// Check parent session state on every 10th job state poll.
|
|
263
|
-
if (jobSessionManager && pollCount && pollCount % 10 === 0 && authConfig) {
|
|
264
|
-
const { session, sessionManager } = jobSessionManager
|
|
265
|
-
const { stateUrl, etag, id: sessionId } = session
|
|
266
|
-
const { access_token } = authConfig
|
|
267
|
-
const { id: jobId } = postedJob
|
|
268
|
-
|
|
269
|
-
// Get session state.
|
|
270
|
-
const { result: sessionState, responseStatus } = await sessionManager[
|
|
271
|
-
'getSessionState'
|
|
272
|
-
](stateUrl, etag, access_token).catch((err) => {
|
|
273
|
-
// Handle error while getting session state.
|
|
274
|
-
throw new JobStatePollError(jobId, err)
|
|
275
|
-
})
|
|
276
|
-
|
|
277
|
-
// Checks if session state is equal to 'running' or 'idle'.
|
|
278
|
-
const isSessionStatesHealthy = (state: string) =>
|
|
279
|
-
[SessionState.Running, SessionState.Idle].includes(
|
|
280
|
-
state as SessionState
|
|
281
|
-
)
|
|
282
|
-
|
|
283
|
-
// Clear parent session and throw an error if session state is not
|
|
284
|
-
// 'running', 'idle' or response status is not 200.
|
|
285
|
-
if (!isSessionStatesHealthy(sessionState) || responseStatus !== 200) {
|
|
286
|
-
sessionManager.clearSession(sessionId, access_token)
|
|
287
|
-
|
|
288
|
-
const sessionError = isSessionStatesHealthy(sessionState)
|
|
289
|
-
? `Session response status is not 200. Session response status is ${responseStatus}.`
|
|
290
|
-
: `Session state of the job is not 'running' or 'idle'. Session state is '${sessionState}'`
|
|
291
|
-
|
|
292
|
-
throw new JobStatePollError(jobId, new Error(sessionError))
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
state = await getJobState(
|
|
297
|
-
requestClient,
|
|
298
|
-
postedJob,
|
|
299
|
-
state,
|
|
300
|
-
debug,
|
|
301
|
-
authConfig,
|
|
302
|
-
onTokensRefreshed
|
|
303
|
-
).catch((err) => {
|
|
304
|
-
errorCount++
|
|
305
|
-
|
|
306
|
-
if (pollCount >= maxPollCount || errorCount >= maxErrorCount) {
|
|
307
|
-
throw err
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
logger.error(
|
|
311
|
-
`Error fetching job state from ${stateLink.href}. Resuming poll, assuming job to be running.`,
|
|
312
|
-
err
|
|
313
|
-
)
|
|
314
|
-
|
|
315
|
-
return JobState.Unavailable
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
pollCount++
|
|
319
|
-
|
|
320
|
-
const jobHref = postedJob.links.find((l: Link) => l.rel === 'self')!.href
|
|
321
|
-
|
|
322
|
-
if (streamLog) {
|
|
323
|
-
const { result: job } = await requestClient.get<Job>(
|
|
324
|
-
jobHref,
|
|
325
|
-
authConfig?.access_token
|
|
326
|
-
)
|
|
327
|
-
|
|
328
|
-
const endLogLine = job.logStatistics?.lineCount ?? 1000000
|
|
329
|
-
|
|
330
|
-
const { saveLog } = isNode() ? require('./saveLog') : { saveLog: null }
|
|
331
|
-
if (saveLog) {
|
|
332
|
-
await saveLog(
|
|
333
|
-
postedJob,
|
|
334
|
-
requestClient,
|
|
335
|
-
startLogLine,
|
|
336
|
-
endLogLine,
|
|
337
|
-
logStream,
|
|
338
|
-
authConfig?.access_token
|
|
339
|
-
)
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
startLogLine += endLogLine
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
if (debug && printedState !== state) {
|
|
346
|
-
logger.info(`Polling: ${requestClient.getBaseUrl() + jobHref}/state`)
|
|
347
|
-
logger.info(`Current job state: ${state}`)
|
|
348
|
-
|
|
349
|
-
printedState = state
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (state !== JobState.Unavailable && errorCount > 0) {
|
|
353
|
-
errorCount = 0
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
if (state !== JobState.Completed) {
|
|
357
|
-
await delay(pollInterval)
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
return { state, pollCount }
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
const validatePollStrategies = (strategy: PollStrategy) => {
|
|
365
|
-
const throwError = (message?: string, pollOptions?: PollOptions) => {
|
|
366
|
-
throw new Error(
|
|
367
|
-
`Poll strategies are not valid.${message ? ` ${message}` : ''}${
|
|
368
|
-
pollOptions
|
|
369
|
-
? ` Invalid poll strategy: \n${JSON.stringify(pollOptions, null, 2)}`
|
|
370
|
-
: ''
|
|
371
|
-
}`
|
|
372
|
-
)
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
strategy.forEach((pollOptions: PollOptions, i: number) => {
|
|
376
|
-
const { maxPollCount, pollInterval } = pollOptions
|
|
377
|
-
|
|
378
|
-
if (maxPollCount < 1) {
|
|
379
|
-
throwError(`'maxPollCount' has to be greater than 0.`, pollOptions)
|
|
380
|
-
} else if (i !== 0) {
|
|
381
|
-
const previousPollOptions = strategy[i - 1]
|
|
382
|
-
|
|
383
|
-
if (maxPollCount <= previousPollOptions.maxPollCount) {
|
|
384
|
-
throwError(
|
|
385
|
-
`'maxPollCount' has to be greater than 'maxPollCount' in previous poll strategy.`,
|
|
386
|
-
pollOptions
|
|
387
|
-
)
|
|
388
|
-
}
|
|
389
|
-
} else if (pollInterval < 1) {
|
|
390
|
-
throwError(`'pollInterval' has to be greater than 0.`, pollOptions)
|
|
391
|
-
}
|
|
392
|
-
})
|
|
393
|
-
}
|