mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-16 03:04:35 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f27444bc52 | ||
|
|
de426c9a92 | ||
|
|
a006ead205 | ||
|
|
422c2a1fd5 | ||
|
|
0c6409e402 | ||
|
|
68b864cf75 | ||
|
|
75a11cdff4 |
@@ -206,138 +206,149 @@ export class SASViyaApiClient {
|
|||||||
silent = false,
|
silent = false,
|
||||||
data = null,
|
data = null,
|
||||||
debug = false
|
debug = false
|
||||||
) {
|
): Promise<any> {
|
||||||
silent = !debug;
|
silent = !debug;
|
||||||
|
try {
|
||||||
const headers: any = {
|
const headers: any = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
};
|
};
|
||||||
|
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
headers.Authorization = `Bearer ${accessToken}`;
|
headers.Authorization = `Bearer ${accessToken}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let executionSessionId: string;
|
let executionSessionId: string;
|
||||||
const session = await this.sessionManager.getSession(accessToken);
|
const session = await this.sessionManager.getSession(accessToken);
|
||||||
executionSessionId = session!.id;
|
executionSessionId = session!.id;
|
||||||
|
|
||||||
const jobArguments: { [key: string]: any } = {
|
const jobArguments: { [key: string]: any } = {
|
||||||
_contextName: contextName,
|
_contextName: contextName,
|
||||||
_OMITJSONLISTING: true,
|
_OMITJSONLISTING: true,
|
||||||
_OMITJSONLOG: true,
|
_OMITJSONLOG: true,
|
||||||
_OMITSESSIONRESULTS: true,
|
_OMITSESSIONRESULTS: true,
|
||||||
_OMITTEXTLISTING: true,
|
_OMITTEXTLISTING: true,
|
||||||
_OMITTEXTLOG: true,
|
_OMITTEXTLOG: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
jobArguments["_OMITTEXTLOG"] = false;
|
jobArguments["_OMITTEXTLOG"] = false;
|
||||||
jobArguments["_OMITSESSIONRESULTS"] = false;
|
jobArguments["_OMITSESSIONRESULTS"] = false;
|
||||||
jobArguments["_DEBUG"] = 131;
|
jobArguments["_DEBUG"] = 131;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileName = `exec-${
|
const fileName = `exec-${
|
||||||
jobName.includes("/") ? jobName.split("/")[1] : jobName
|
jobName.includes("/") ? jobName.split("/")[1] : jobName
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
let jobVariables: any = {
|
let jobVariables: any = {
|
||||||
SYS_JES_JOB_URI: "",
|
SYS_JES_JOB_URI: "",
|
||||||
_program: this.rootFolderName + "/" + jobName,
|
_program: this.rootFolderName + "/" + jobName,
|
||||||
};
|
};
|
||||||
|
|
||||||
let files: any[] = [];
|
let files: any[] = [];
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
if (JSON.stringify(data).includes(";")) {
|
if (JSON.stringify(data).includes(";")) {
|
||||||
files = await this.uploadTables(data, accessToken);
|
files = await this.uploadTables(data, accessToken);
|
||||||
jobVariables["_webin_file_count"] = files.length;
|
jobVariables["_webin_file_count"] = files.length;
|
||||||
files.forEach((fileInfo, index) => {
|
files.forEach((fileInfo, index) => {
|
||||||
jobVariables[
|
jobVariables[
|
||||||
`_webin_fileuri${index + 1}`
|
`_webin_fileuri${index + 1}`
|
||||||
] = `/files/files/${fileInfo.file.id}`;
|
] = `/files/files/${fileInfo.file.id}`;
|
||||||
jobVariables[`_webin_name${index + 1}`] = fileInfo.tableName;
|
jobVariables[`_webin_name${index + 1}`] = fileInfo.tableName;
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
jobVariables = { ...jobVariables, ...formatDataForRequest(data) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute job in session
|
||||||
|
const postJobRequest = {
|
||||||
|
method: "POST",
|
||||||
|
headers,
|
||||||
|
body: JSON.stringify({
|
||||||
|
name: fileName,
|
||||||
|
description: "Powered by SASjs",
|
||||||
|
code: linesOfCode,
|
||||||
|
variables: jobVariables,
|
||||||
|
arguments: jobArguments,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const { result: postedJob, etag } = await this.request<Job>(
|
||||||
|
`${this.serverUrl}/compute/sessions/${executionSessionId}/jobs`,
|
||||||
|
postJobRequest
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!silent) {
|
||||||
|
console.log(`Job has been submitted for ${fileName}`);
|
||||||
|
console.log(
|
||||||
|
`You can monitor the job progress at ${this.serverUrl}${
|
||||||
|
postedJob.links.find((l: any) => l.rel === "state")!.href
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const jobStatus = await this.pollJobState(
|
||||||
|
postedJob,
|
||||||
|
etag,
|
||||||
|
accessToken,
|
||||||
|
silent
|
||||||
|
);
|
||||||
|
|
||||||
|
const { result: currentJob } = await this.request<Job>(
|
||||||
|
`${this.serverUrl}/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`,
|
||||||
|
{ headers }
|
||||||
|
);
|
||||||
|
|
||||||
|
let jobResult, log;
|
||||||
|
|
||||||
|
const logLink = currentJob.links.find((l) => l.rel === "log");
|
||||||
|
|
||||||
|
if (true && logLink) {
|
||||||
|
log = await this.request<any>(
|
||||||
|
`${this.serverUrl}${logLink.href}/content?limit=10000`,
|
||||||
|
{
|
||||||
|
headers,
|
||||||
|
}
|
||||||
|
).then((res: any) =>
|
||||||
|
res.result.items.map((i: any) => i.line).join("\n")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jobStatus === "failed" || jobStatus === "error") {
|
||||||
|
return Promise.reject({ error: currentJob.error, log: log });
|
||||||
|
}
|
||||||
|
const resultLink = `/compute/sessions/${executionSessionId}/filerefs/_webout/content`;
|
||||||
|
|
||||||
|
if (resultLink) {
|
||||||
|
jobResult = await this.request<any>(
|
||||||
|
`${this.serverUrl}${resultLink}`,
|
||||||
|
{ headers },
|
||||||
|
"text"
|
||||||
|
).catch((e) => ({
|
||||||
|
result: JSON.stringify(e),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.sessionManager.clearSession(executionSessionId, accessToken);
|
||||||
|
|
||||||
|
return { result: jobResult?.result, log };
|
||||||
|
} catch (e) {
|
||||||
|
if (e && e.status === 404) {
|
||||||
|
return this.executeScript(
|
||||||
|
jobName,
|
||||||
|
linesOfCode,
|
||||||
|
contextName,
|
||||||
|
accessToken,
|
||||||
|
silent,
|
||||||
|
data,
|
||||||
|
debug
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
jobVariables = { ...jobVariables, ...formatDataForRequest(data) };
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute job in session
|
|
||||||
const postJobRequest = {
|
|
||||||
method: "POST",
|
|
||||||
headers,
|
|
||||||
body: JSON.stringify({
|
|
||||||
name: fileName,
|
|
||||||
description: "Powered by SASjs",
|
|
||||||
code: linesOfCode,
|
|
||||||
variables: jobVariables,
|
|
||||||
arguments: jobArguments,
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
|
|
||||||
const { result: postedJob, etag } = await this.request<Job>(
|
|
||||||
`${this.serverUrl}/compute/sessions/${executionSessionId}/jobs`,
|
|
||||||
postJobRequest
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!silent) {
|
|
||||||
console.log(`Job has been submitted for ${fileName}`);
|
|
||||||
console.log(
|
|
||||||
`You can monitor the job progress at ${this.serverUrl}${
|
|
||||||
postedJob.links.find((l: any) => l.rel === "state")!.href
|
|
||||||
}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const jobStatus = await this.pollJobState(
|
|
||||||
postedJob,
|
|
||||||
etag,
|
|
||||||
accessToken,
|
|
||||||
silent
|
|
||||||
);
|
|
||||||
|
|
||||||
const { result: currentJob } = await this.request<Job>(
|
|
||||||
`${this.serverUrl}/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`,
|
|
||||||
{ headers }
|
|
||||||
);
|
|
||||||
|
|
||||||
let jobResult, log;
|
|
||||||
|
|
||||||
const logLink = currentJob.links.find((l) => l.rel === "log");
|
|
||||||
|
|
||||||
if (true && logLink) {
|
|
||||||
log = await this.request<any>(
|
|
||||||
`${this.serverUrl}${logLink.href}/content?limit=10000`,
|
|
||||||
{
|
|
||||||
headers,
|
|
||||||
}
|
|
||||||
).then((res: any) => res.result.items.map((i: any) => i.line).join("\n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (jobStatus === "failed" || jobStatus === "error") {
|
|
||||||
return Promise.reject({error: currentJob.error, log: log});
|
|
||||||
}
|
|
||||||
const resultLink = `/compute/sessions/${executionSessionId}/filerefs/_webout/content`;
|
|
||||||
|
|
||||||
if (resultLink) {
|
|
||||||
jobResult = await this.request<any>(
|
|
||||||
`${this.serverUrl}${resultLink}`,
|
|
||||||
{ headers },
|
|
||||||
"text"
|
|
||||||
).catch((e) => ({
|
|
||||||
result: JSON.stringify(e),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.sessionManager.clearSession(executionSessionId, accessToken);
|
|
||||||
|
|
||||||
return { result: jobResult?.result, log };
|
|
||||||
// } else {
|
|
||||||
// console.error(
|
|
||||||
// `Unable to find execution context ${contextName}.\nPlease check the contextName in the tgtDeployVars and try again.`
|
|
||||||
// );
|
|
||||||
// console.error("Response from server: ", JSON.stringify(this.contexts));
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -719,12 +730,12 @@ export class SASViyaApiClient {
|
|||||||
`The job ${sasJob} was not found in ${this.rootFolderName}`
|
`The job ${sasJob} was not found in ${this.rootFolderName}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
const jobName = path.basename(sasJob);
|
const jobName = path.basename(sasJob);
|
||||||
const jobFolder = sasJob.replace(`/${jobName}`, "");
|
const jobFolder = sasJob.replace(`/${jobName}`, "");
|
||||||
const allJobsInFolder = this.rootFolderMap.get(jobFolder.replace("/", ""));
|
const allJobsInFolder = this.rootFolderMap.get(jobFolder.replace("/", ""));
|
||||||
|
|||||||
@@ -291,7 +291,7 @@ export default class SASjs {
|
|||||||
public async checkSession() {
|
public async checkSession() {
|
||||||
const loginResponse = await fetch(this.loginUrl.replace(".do", ""));
|
const loginResponse = await fetch(this.loginUrl.replace(".do", ""));
|
||||||
const responseText = await loginResponse.text();
|
const responseText = await loginResponse.text();
|
||||||
const isLoggedIn = /You have signed in./gm.test(responseText);
|
const isLoggedIn = /<button.+onClick.+logout/gm.test(responseText);
|
||||||
|
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
isLoggedIn,
|
isLoggedIn,
|
||||||
@@ -730,7 +730,7 @@ export default class SASjs {
|
|||||||
}`;
|
}`;
|
||||||
|
|
||||||
const requestParams = {
|
const requestParams = {
|
||||||
...this.getRequestParamsWeb(),
|
...this.getRequestParamsWeb(config),
|
||||||
};
|
};
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
@@ -941,14 +941,14 @@ export default class SASjs {
|
|||||||
this.sasjsWaitingRequests = [];
|
this.sasjsWaitingRequests = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private getRequestParamsWeb(): any {
|
private getRequestParamsWeb(config: any): any {
|
||||||
const requestParams: any = {};
|
const requestParams: any = {};
|
||||||
|
|
||||||
if (this.csrfTokenWeb) {
|
if (this.csrfTokenWeb) {
|
||||||
requestParams["_csrf"] = this.csrfTokenWeb.value;
|
requestParams["_csrf"] = this.csrfTokenWeb.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.sasjsConfig.debug) {
|
if (config.debug) {
|
||||||
requestParams["_omittextlog"] = "false";
|
requestParams["_omittextlog"] = "false";
|
||||||
requestParams["_omitsessionresults"] = "false";
|
requestParams["_omitsessionresults"] = "false";
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,16 @@ export class SessionManager {
|
|||||||
await this.createSessions(accessToken);
|
await this.createSessions(accessToken);
|
||||||
this.createAndWaitForSession(accessToken);
|
this.createAndWaitForSession(accessToken);
|
||||||
const session = this.sessions.pop();
|
const session = this.sessions.pop();
|
||||||
|
const secondsSinceSessionCreation =
|
||||||
|
(new Date().getTime() - new Date(session!.creationTimeStamp).getTime()) /
|
||||||
|
1000;
|
||||||
|
if (
|
||||||
|
secondsSinceSessionCreation >= session!.attributes.sessionInactiveTimeout
|
||||||
|
) {
|
||||||
|
await this.createSessions(accessToken);
|
||||||
|
const freshSession = this.sessions.pop();
|
||||||
|
return freshSession;
|
||||||
|
}
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,4 +4,8 @@ export interface Session {
|
|||||||
id: string;
|
id: string;
|
||||||
state: string;
|
state: string;
|
||||||
links: Link[];
|
links: Link[];
|
||||||
|
attributes: {
|
||||||
|
sessionInactiveTimeout: number;
|
||||||
|
};
|
||||||
|
creationTimeStamp: string;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user