mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-20 10:30:06 +00:00
feat(jes-api): implement job execution via API to JES
This commit is contained in:
@@ -6,6 +6,6 @@
|
|||||||
"appLoc": "/Public/app",
|
"appLoc": "/Public/app",
|
||||||
"serverType": "SASVIYA",
|
"serverType": "SASVIYA",
|
||||||
"debug": false,
|
"debug": false,
|
||||||
"contextName": null
|
"contextName": "SAS Job Execution compute context"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
} from "./utils";
|
} from "./utils";
|
||||||
import * as NodeFormData from "form-data";
|
import * as NodeFormData from "form-data";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { Job, Session, Context, Folder } from "./types";
|
import { Job, Session, Context, Folder, CsrfToken } from "./types";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A client for interfacing with the SAS Viya REST API
|
* A client for interfacing with the SAS Viya REST API
|
||||||
@@ -299,14 +299,24 @@ export class SASViyaApiClient {
|
|||||||
if (!parentFolderUri) {
|
if (!parentFolderUri) {
|
||||||
console.log(`Parent folder is not present: ${parentFolderPath}`);
|
console.log(`Parent folder is not present: ${parentFolderPath}`);
|
||||||
|
|
||||||
const newParentFolderPath = parentFolderPath.substring(0, parentFolderPath.lastIndexOf("/"));
|
const newParentFolderPath = parentFolderPath.substring(
|
||||||
|
0,
|
||||||
|
parentFolderPath.lastIndexOf("/")
|
||||||
|
);
|
||||||
const newFolderName = `${parentFolderPath.split("/").pop()}`;
|
const newFolderName = `${parentFolderPath.split("/").pop()}`;
|
||||||
if (newParentFolderPath === "") {
|
if (newParentFolderPath === "") {
|
||||||
throw new Error("Root Folder should have been present on server");
|
throw new Error("Root Folder should have been present on server");
|
||||||
}
|
}
|
||||||
console.log(`Creating Parent Folder:\n${newFolderName} in ${newParentFolderPath}`)
|
console.log(
|
||||||
const parentFolder = await this.createFolder(newFolderName, newParentFolderPath, undefined, accessToken)
|
`Creating Parent Folder:\n${newFolderName} in ${newParentFolderPath}`
|
||||||
console.log(`Parent Folder "${newFolderName}" successfully created.`)
|
);
|
||||||
|
const parentFolder = await this.createFolder(
|
||||||
|
newFolderName,
|
||||||
|
newParentFolderPath,
|
||||||
|
undefined,
|
||||||
|
accessToken
|
||||||
|
);
|
||||||
|
console.log(`Parent Folder "${newFolderName}" successfully created.`);
|
||||||
parentFolderUri = `/folders/folders/${parentFolder.id}`;
|
parentFolderUri = `/folders/folders/${parentFolder.id}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -350,7 +360,9 @@ export class SASViyaApiClient {
|
|||||||
accessToken?: string
|
accessToken?: string
|
||||||
) {
|
) {
|
||||||
if (!parentFolderPath && !parentFolderUri) {
|
if (!parentFolderPath && !parentFolderUri) {
|
||||||
throw new Error('Either parentFolderPath or parentFolderUri must be provided');
|
throw new Error(
|
||||||
|
"Either parentFolderPath or parentFolderUri must be provided"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parentFolderUri && parentFolderPath) {
|
if (!parentFolderUri && parentFolderPath) {
|
||||||
@@ -367,10 +379,10 @@ export class SASViyaApiClient {
|
|||||||
name: jobName,
|
name: jobName,
|
||||||
parameters: [
|
parameters: [
|
||||||
{
|
{
|
||||||
"name":"_addjesbeginendmacros",
|
name: "_addjesbeginendmacros",
|
||||||
"type":"CHARACTER",
|
type: "CHARACTER",
|
||||||
"defaultValue":"false"
|
defaultValue: "false",
|
||||||
}
|
},
|
||||||
],
|
],
|
||||||
type: "Compute",
|
type: "Compute",
|
||||||
code,
|
code,
|
||||||
@@ -578,6 +590,7 @@ export class SASViyaApiClient {
|
|||||||
let files: any[] = [];
|
let files: any[] = [];
|
||||||
if (data && Object.keys(data).length) {
|
if (data && Object.keys(data).length) {
|
||||||
files = await this.uploadTables(data, accessToken);
|
files = await this.uploadTables(data, accessToken);
|
||||||
|
console.log("Uploaded table files: ", files);
|
||||||
}
|
}
|
||||||
const jobName = path.basename(sasJob);
|
const jobName = path.basename(sasJob);
|
||||||
const jobFolder = sasJob.replace(`/${jobName}`, "");
|
const jobFolder = sasJob.replace(`/${jobName}`, "");
|
||||||
@@ -612,15 +625,15 @@ export class SASViyaApiClient {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
jobArguments["_omittextlog"] = "false";
|
jobArguments["_OMITTEXTLOG"] = "false";
|
||||||
jobArguments["_omitsessionresults"] = "false";
|
jobArguments["_OMITSESSIONRESULTS"] = "false";
|
||||||
jobArguments["_debug"] = 131;
|
jobArguments["_DEBUG"] = 131;
|
||||||
}
|
}
|
||||||
|
|
||||||
files.forEach((fileInfo, index) => {
|
files.forEach((fileInfo, index) => {
|
||||||
jobArguments[
|
jobArguments[
|
||||||
`_webin_fileuri${index + 1}`
|
`_webin_fileuri${index + 1}`
|
||||||
] = `/files/files/${fileInfo.id}`;
|
] = `/files/files/${fileInfo.file.id}`;
|
||||||
jobArguments[`_webin_name${index + 1}`] = fileInfo.tableName;
|
jobArguments[`_webin_name${index + 1}`] = fileInfo.tableName;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -643,16 +656,29 @@ export class SASViyaApiClient {
|
|||||||
`${this.serverUrl}/jobExecution/jobs/${postedJob.id}`,
|
`${this.serverUrl}/jobExecution/jobs/${postedJob.id}`,
|
||||||
{ headers }
|
{ headers }
|
||||||
);
|
);
|
||||||
const resultLink = currentJob.results["_webout.json"];
|
|
||||||
if (resultLink) {
|
|
||||||
const result = await this.request<any>(
|
|
||||||
`${this.serverUrl}${resultLink}/content`,
|
|
||||||
{ headers }
|
|
||||||
);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return postedJob;
|
let result, log;
|
||||||
|
if (jobStatus === "failed") {
|
||||||
|
return Promise.reject(currentJob.error);
|
||||||
|
}
|
||||||
|
const resultLink = currentJob.results["_webout.json"];
|
||||||
|
const logLink = currentJob.links.find((l) => l.rel === "log");
|
||||||
|
if (resultLink) {
|
||||||
|
result = await this.request<any>(
|
||||||
|
`${this.serverUrl}${resultLink}/content`,
|
||||||
|
{ headers },
|
||||||
|
"text"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (debug && logLink) {
|
||||||
|
log = await this.request<any>(
|
||||||
|
`${this.serverUrl}${logLink.href}/content`,
|
||||||
|
{
|
||||||
|
headers,
|
||||||
|
}
|
||||||
|
).then((res: any) => res.items.map((i: any) => i.line).join("\n"));
|
||||||
|
}
|
||||||
|
return { result, log };
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`The job ${sasJob} was not found at the location ${this.rootFolderName}`
|
`The job ${sasJob} was not found at the location ${this.rootFolderName}`
|
||||||
@@ -734,6 +760,8 @@ export class SASViyaApiClient {
|
|||||||
accessToken?: string,
|
accessToken?: string,
|
||||||
silent = false
|
silent = false
|
||||||
) {
|
) {
|
||||||
|
const MAX_POLL_COUNT = 1000;
|
||||||
|
const POLL_INTERVAL = 300;
|
||||||
let postedJobState = "";
|
let postedJobState = "";
|
||||||
let pollCount = 0;
|
let pollCount = 0;
|
||||||
const headers: any = {
|
const headers: any = {
|
||||||
@@ -767,7 +795,7 @@ export class SASViyaApiClient {
|
|||||||
console.log(`Current state: ${postedJobState}\n`);
|
console.log(`Current state: ${postedJobState}\n`);
|
||||||
}
|
}
|
||||||
pollCount++;
|
pollCount++;
|
||||||
if (pollCount >= 100) {
|
if (pollCount >= MAX_POLL_COUNT) {
|
||||||
resolve(postedJobState);
|
resolve(postedJobState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -775,7 +803,7 @@ export class SASViyaApiClient {
|
|||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
resolve(postedJobState);
|
resolve(postedJobState);
|
||||||
}
|
}
|
||||||
}, 100);
|
}, POLL_INTERVAL);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -824,11 +852,14 @@ export class SASViyaApiClient {
|
|||||||
`${this.serverUrl}${url}`,
|
`${this.serverUrl}${url}`,
|
||||||
requestInfo
|
requestInfo
|
||||||
);
|
);
|
||||||
if (!folder)
|
if (!folder) return undefined;
|
||||||
return undefined;
|
|
||||||
return `/folders/folders/${folder.id}`;
|
return `/folders/folders/${folder.id}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setCsrfToken = (csrfToken: CsrfToken) => {
|
||||||
|
this.csrfToken = csrfToken;
|
||||||
|
};
|
||||||
|
|
||||||
private async request<T>(
|
private async request<T>(
|
||||||
url: string,
|
url: string,
|
||||||
options: RequestInit,
|
options: RequestInit,
|
||||||
@@ -840,11 +871,6 @@ export class SASViyaApiClient {
|
|||||||
[this.csrfToken.headerName]: this.csrfToken.value,
|
[this.csrfToken.headerName]: this.csrfToken.value,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return await makeRequest<T>(
|
return await makeRequest<T>(url, options, this.setCsrfToken, contentType);
|
||||||
url,
|
|
||||||
options,
|
|
||||||
(csrfToken) => (this.csrfToken = csrfToken),
|
|
||||||
contentType
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
116
src/SASjs.ts
116
src/SASjs.ts
@@ -386,6 +386,18 @@ export default class SASjs {
|
|||||||
loginRequiredCallback?: any,
|
loginRequiredCallback?: any,
|
||||||
accessToken?: string
|
accessToken?: string
|
||||||
) {
|
) {
|
||||||
|
if (
|
||||||
|
this.sasjsConfig.serverType === ServerType.SASViya &&
|
||||||
|
this.sasjsConfig.contextName
|
||||||
|
) {
|
||||||
|
return await this.executeViaJesApi(
|
||||||
|
sasJob,
|
||||||
|
data,
|
||||||
|
params,
|
||||||
|
loginRequiredCallback,
|
||||||
|
accessToken
|
||||||
|
);
|
||||||
|
} else {
|
||||||
const sasjsWaitingRequest: SASjsWaitingRequest = {
|
const sasjsWaitingRequest: SASjsWaitingRequest = {
|
||||||
requestPromise: {
|
requestPromise: {
|
||||||
promise: null,
|
promise: null,
|
||||||
@@ -396,38 +408,9 @@ export default class SASjs {
|
|||||||
data,
|
data,
|
||||||
params,
|
params,
|
||||||
};
|
};
|
||||||
|
|
||||||
// if (
|
|
||||||
// this.sasjsConfig.serverType === ServerType.SASViya &&
|
|
||||||
// this.sasjsConfig.contextName
|
|
||||||
// ) {
|
|
||||||
// sasjsWaitingRequest.requestPromise.promise = new Promise(
|
|
||||||
// async (resolve, reject) => {
|
|
||||||
// const session = await this.checkSession();
|
|
||||||
|
|
||||||
// if (!session.isLoggedIn) {
|
|
||||||
// if (loginRequiredCallback) loginRequiredCallback(true);
|
|
||||||
// logInRequired = true;
|
|
||||||
// sasjsWaitingRequest.requestPromise.resolve = resolve;
|
|
||||||
// sasjsWaitingRequest.requestPromise.reject = reject;
|
|
||||||
// this.sasjsWaitingRequests.push(sasjsWaitingRequest);
|
|
||||||
// } else {
|
|
||||||
// resolve(
|
|
||||||
// await this.sasViyaApiClient?.executeJob(
|
|
||||||
// sasJob,
|
|
||||||
// this.sasjsConfig.contextName,
|
|
||||||
// this.sasjsConfig.debug,
|
|
||||||
// data,
|
|
||||||
// accessToken
|
|
||||||
// )
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
// return sasjsWaitingRequest.requestPromise.promise;
|
|
||||||
// } else {
|
|
||||||
const program = this.sasjsConfig.appLoc
|
const program = this.sasjsConfig.appLoc
|
||||||
? this.sasjsConfig.appLoc.replace(/\/?$/, "/") + sasJob.replace(/^\//, "")
|
? this.sasjsConfig.appLoc.replace(/\/?$/, "/") +
|
||||||
|
sasJob.replace(/^\//, "")
|
||||||
: sasJob;
|
: sasJob;
|
||||||
const jobUri =
|
const jobUri =
|
||||||
this.sasjsConfig.serverType === "SASVIYA"
|
this.sasjsConfig.serverType === "SASVIYA"
|
||||||
@@ -474,7 +457,9 @@ export default class SASjs {
|
|||||||
"The max length of a string value in SASjs is 32765 characters.";
|
"The max length of a string value in SASjs is 32765 characters.";
|
||||||
}
|
}
|
||||||
|
|
||||||
const file = new Blob([csv], { type: "application/csv" });
|
const file = new Blob([csv], {
|
||||||
|
type: "application/csv",
|
||||||
|
});
|
||||||
console.log("File", file);
|
console.log("File", file);
|
||||||
|
|
||||||
formData.append(name, file, `${name}.csv`);
|
formData.append(name, file, `${name}.csv`);
|
||||||
@@ -587,7 +572,9 @@ export default class SASjs {
|
|||||||
this.sasjsConfig.debug
|
this.sasjsConfig.debug
|
||||||
) {
|
) {
|
||||||
this.updateUsername(responseText);
|
this.updateUsername(responseText);
|
||||||
const jsonResponseText = this.parseSAS9Response(responseText);
|
const jsonResponseText = this.parseSAS9Response(
|
||||||
|
responseText
|
||||||
|
);
|
||||||
|
|
||||||
if (jsonResponseText !== "") {
|
if (jsonResponseText !== "") {
|
||||||
resolve(JSON.parse(jsonResponseText));
|
resolve(JSON.parse(jsonResponseText));
|
||||||
@@ -636,7 +623,7 @@ export default class SASjs {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return sasjsWaitingRequest.requestPromise.promise;
|
return sasjsWaitingRequest.requestPromise.promise;
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -695,6 +682,59 @@ export default class SASjs {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async executeViaJesApi(
|
||||||
|
sasJob: string,
|
||||||
|
data: any,
|
||||||
|
params?: any,
|
||||||
|
loginRequiredCallback?: any,
|
||||||
|
accessToken?: string
|
||||||
|
) {
|
||||||
|
const sasjsWaitingRequest: SASjsWaitingRequest = {
|
||||||
|
requestPromise: {
|
||||||
|
promise: null,
|
||||||
|
resolve: null,
|
||||||
|
reject: null,
|
||||||
|
},
|
||||||
|
SASjob: sasJob,
|
||||||
|
data,
|
||||||
|
params,
|
||||||
|
};
|
||||||
|
|
||||||
|
sasjsWaitingRequest.requestPromise.promise = new Promise(
|
||||||
|
async (resolve, reject) => {
|
||||||
|
const session = await this.checkSession();
|
||||||
|
|
||||||
|
if (!session.isLoggedIn) {
|
||||||
|
if (loginRequiredCallback) loginRequiredCallback(true);
|
||||||
|
sasjsWaitingRequest.requestPromise.resolve = resolve;
|
||||||
|
sasjsWaitingRequest.requestPromise.reject = reject;
|
||||||
|
this.sasjsWaitingRequests.push(sasjsWaitingRequest);
|
||||||
|
} else {
|
||||||
|
resolve(
|
||||||
|
await this.sasViyaApiClient
|
||||||
|
?.executeJob(
|
||||||
|
sasJob,
|
||||||
|
this.sasjsConfig.contextName,
|
||||||
|
this.sasjsConfig.debug,
|
||||||
|
data,
|
||||||
|
accessToken
|
||||||
|
)
|
||||||
|
.then((response) => {
|
||||||
|
if (!this.sasjsConfig.debug) {
|
||||||
|
this.appendSasjsRequest(null, sasJob, null);
|
||||||
|
} else {
|
||||||
|
this.appendSasjsRequest(response, sasJob, null);
|
||||||
|
}
|
||||||
|
return JSON.parse(response.result);
|
||||||
|
})
|
||||||
|
.catch((e) => reject({ MESSAGE: e.message }))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return sasjsWaitingRequest.requestPromise.promise;
|
||||||
|
}
|
||||||
|
|
||||||
private async resendWaitingRequests() {
|
private async resendWaitingRequests() {
|
||||||
for (const sasjsWaitingRequest of this.sasjsWaitingRequests) {
|
for (const sasjsWaitingRequest of this.sasjsWaitingRequests) {
|
||||||
this.request(
|
this.request(
|
||||||
@@ -856,14 +896,20 @@ export default class SASjs {
|
|||||||
let generatedCode = "";
|
let generatedCode = "";
|
||||||
let sasWork = null;
|
let sasWork = null;
|
||||||
|
|
||||||
|
if (response && response.result && response.log) {
|
||||||
|
sourceCode = parseSourceCode(response.log);
|
||||||
|
generatedCode = parseGeneratedCode(response.log);
|
||||||
|
sasWork = JSON.parse(response.result).WORK;
|
||||||
|
} else {
|
||||||
if (response) {
|
if (response) {
|
||||||
sourceCode = parseSourceCode(response);
|
sourceCode = parseSourceCode(response);
|
||||||
generatedCode = parseGeneratedCode(response);
|
generatedCode = parseGeneratedCode(response);
|
||||||
sasWork = await this.parseSasWork(response);
|
sasWork = await this.parseSasWork(response);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.sasjsRequests.push({
|
this.sasjsRequests.push({
|
||||||
logFile: response,
|
logFile: (response && response.log) || response,
|
||||||
serviceLink: program,
|
serviceLink: program,
|
||||||
timestamp: new Date(),
|
timestamp: new Date(),
|
||||||
sourceCode,
|
sourceCode,
|
||||||
|
|||||||
@@ -8,4 +8,5 @@ export interface Job {
|
|||||||
createdBy: string;
|
createdBy: string;
|
||||||
links: Link[];
|
links: Link[];
|
||||||
results: JobResult;
|
results: JobResult;
|
||||||
|
error?: any;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user