mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-15 16:10:06 +00:00
fix(session-expiry-retry): retry job with new session when current session has expired
This commit is contained in:
@@ -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 = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
};
|
||||||
|
|
||||||
const headers: any = {
|
if (accessToken) {
|
||||||
"Content-Type": "application/json",
|
headers.Authorization = `Bearer ${accessToken}`;
|
||||||
};
|
}
|
||||||
|
|
||||||
if (accessToken) {
|
let executionSessionId: string;
|
||||||
headers.Authorization = `Bearer ${accessToken}`;
|
const session = await this.sessionManager.getSession(accessToken);
|
||||||
}
|
executionSessionId = session!.id;
|
||||||
|
|
||||||
let executionSessionId: string;
|
const jobArguments: { [key: string]: any } = {
|
||||||
const session = await this.sessionManager.getSession(accessToken);
|
_contextName: contextName,
|
||||||
executionSessionId = session!.id;
|
_OMITJSONLISTING: true,
|
||||||
|
_OMITJSONLOG: true,
|
||||||
|
_OMITSESSIONRESULTS: true,
|
||||||
|
_OMITTEXTLISTING: true,
|
||||||
|
_OMITTEXTLOG: true,
|
||||||
|
};
|
||||||
|
|
||||||
const jobArguments: { [key: string]: any } = {
|
if (debug) {
|
||||||
_contextName: contextName,
|
jobArguments["_OMITTEXTLOG"] = false;
|
||||||
_OMITJSONLISTING: true,
|
jobArguments["_OMITSESSIONRESULTS"] = false;
|
||||||
_OMITJSONLOG: true,
|
jobArguments["_DEBUG"] = 131;
|
||||||
_OMITSESSIONRESULTS: true,
|
}
|
||||||
_OMITTEXTLISTING: true,
|
|
||||||
_OMITTEXTLOG: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (debug) {
|
const fileName = `exec-${
|
||||||
jobArguments["_OMITTEXTLOG"] = false;
|
jobName.includes("/") ? jobName.split("/")[1] : jobName
|
||||||
jobArguments["_OMITSESSIONRESULTS"] = false;
|
}`;
|
||||||
jobArguments["_DEBUG"] = 131;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileName = `exec-${
|
let jobVariables: any = {
|
||||||
jobName.includes("/") ? jobName.split("/")[1] : jobName
|
SYS_JES_JOB_URI: "",
|
||||||
}`;
|
_program: this.rootFolderName + "/" + jobName,
|
||||||
|
};
|
||||||
|
|
||||||
let jobVariables: any = {
|
let files: any[] = [];
|
||||||
SYS_JES_JOB_URI: "",
|
|
||||||
_program: this.rootFolderName + "/" + jobName,
|
|
||||||
};
|
|
||||||
|
|
||||||
let files: any[] = [];
|
if (data) {
|
||||||
|
if (JSON.stringify(data).includes(";")) {
|
||||||
|
files = await this.uploadTables(data, accessToken);
|
||||||
|
jobVariables["_webin_file_count"] = files.length;
|
||||||
|
files.forEach((fileInfo, index) => {
|
||||||
|
jobVariables[
|
||||||
|
`_webin_fileuri${index + 1}`
|
||||||
|
] = `/files/files/${fileInfo.file.id}`;
|
||||||
|
jobVariables[`_webin_name${index + 1}`] = fileInfo.tableName;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
jobVariables = { ...jobVariables, ...formatDataForRequest(data) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (data) {
|
// Execute job in session
|
||||||
if (JSON.stringify(data).includes(";")) {
|
const postJobRequest = {
|
||||||
files = await this.uploadTables(data, accessToken);
|
method: "POST",
|
||||||
jobVariables["_webin_file_count"] = files.length;
|
headers,
|
||||||
files.forEach((fileInfo, index) => {
|
body: JSON.stringify({
|
||||||
jobVariables[
|
name: fileName,
|
||||||
`_webin_fileuri${index + 1}`
|
description: "Powered by SASjs",
|
||||||
] = `/files/files/${fileInfo.file.id}`;
|
code: linesOfCode,
|
||||||
jobVariables[`_webin_name${index + 1}`] = fileInfo.tableName;
|
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));
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user