mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 09:24:35 +00:00
Merge pull request #31 from sasjs/session-cleanup
feat(session-cleanup): delete a session after it has been used
This commit is contained in:
@@ -29,7 +29,11 @@ export class SASViyaApiClient {
|
||||
}
|
||||
private csrfToken: CsrfToken | null = null;
|
||||
private rootFolder: Folder | null = null;
|
||||
private sessionManager = new SessionManager(this.serverUrl, this.contextName, this.setCsrfToken);
|
||||
private sessionManager = new SessionManager(
|
||||
this.serverUrl,
|
||||
this.contextName,
|
||||
this.setCsrfToken
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns a map containing the directory structure in the currently set root folder.
|
||||
@@ -113,9 +117,7 @@ export class SASViyaApiClient {
|
||||
`test-${context.name}`,
|
||||
linesOfCode,
|
||||
context.name,
|
||||
accessToken,
|
||||
undefined,
|
||||
true
|
||||
accessToken
|
||||
).catch(() => null);
|
||||
});
|
||||
const results = await Promise.all(promises);
|
||||
@@ -201,11 +203,11 @@ export class SASViyaApiClient {
|
||||
linesOfCode: string[],
|
||||
contextName: string,
|
||||
accessToken?: string,
|
||||
sessionId = "",
|
||||
silent = false,
|
||||
data = null,
|
||||
debug = false
|
||||
) {
|
||||
silent = !debug;
|
||||
const headers: any = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
@@ -314,6 +316,8 @@ export class SASViyaApiClient {
|
||||
).then((res: any) => res.result.items.map((i: any) => i.line).join("\n"));
|
||||
}
|
||||
|
||||
await this.sessionManager.clearSession(executionSessionId, accessToken);
|
||||
|
||||
return { result: jobResult?.result, log };
|
||||
// } else {
|
||||
// console.error(
|
||||
@@ -623,12 +627,16 @@ export class SASViyaApiClient {
|
||||
await this.populateRootFolder(accessToken);
|
||||
}
|
||||
if (!this.rootFolder) {
|
||||
console.error("Root folder was not found");
|
||||
throw new Error("Root folder was not found");
|
||||
}
|
||||
if (!this.rootFolderMap.size) {
|
||||
await this.populateRootFolderMap(accessToken);
|
||||
}
|
||||
if (!this.rootFolderMap.size) {
|
||||
console.error(
|
||||
`The job ${sasJob} was not found in ${this.rootFolderName}`
|
||||
);
|
||||
throw new Error(
|
||||
`The job ${sasJob} was not found in ${this.rootFolderName}`
|
||||
);
|
||||
@@ -647,6 +655,7 @@ export class SASViyaApiClient {
|
||||
(l) => l.rel === "getResource"
|
||||
);
|
||||
if (!jobDefinitionLink) {
|
||||
console.error("Job definition URI was not found.");
|
||||
throw new Error("Job definition URI was not found.");
|
||||
}
|
||||
const { result: jobDefinition } = await this.request<JobDefinition>(
|
||||
@@ -661,7 +670,6 @@ export class SASViyaApiClient {
|
||||
linesToExecute,
|
||||
contextName,
|
||||
accessToken,
|
||||
"",
|
||||
true,
|
||||
data,
|
||||
debug
|
||||
@@ -1020,7 +1028,7 @@ export class SASViyaApiClient {
|
||||
requestInfo
|
||||
).catch((err) => {
|
||||
return { result: null };
|
||||
})
|
||||
});
|
||||
|
||||
if (!folder) return undefined;
|
||||
return `/folders/folders/${folder.id}`;
|
||||
@@ -1042,6 +1050,11 @@ export class SASViyaApiClient {
|
||||
[this.csrfToken.headerName]: this.csrfToken.value,
|
||||
};
|
||||
}
|
||||
return await makeRequest<T>(url, options, this.setCsrfTokenLocal, contentType);
|
||||
return await makeRequest<T>(
|
||||
url,
|
||||
options,
|
||||
this.setCsrfTokenLocal,
|
||||
contentType
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import {
|
||||
SASjsWaitingRequest,
|
||||
ServerType,
|
||||
CsrfToken,
|
||||
UploadFile
|
||||
UploadFile,
|
||||
} from "./types";
|
||||
import { SASViyaApiClient } from "./SASViyaApiClient";
|
||||
import { SAS9ApiClient } from "./SAS9ApiClient";
|
||||
@@ -122,7 +122,6 @@ export default class SASjs {
|
||||
linesOfCode,
|
||||
contextName,
|
||||
accessToken,
|
||||
sessionId,
|
||||
silent
|
||||
);
|
||||
}
|
||||
@@ -591,7 +590,7 @@ export default class SASjs {
|
||||
} else {
|
||||
reject({ MESSAGE: e || "Job execution failed" });
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
);
|
||||
return sasjsWaitingRequest.requestPromise.promise;
|
||||
@@ -662,7 +661,7 @@ export default class SASjs {
|
||||
}
|
||||
}
|
||||
|
||||
reject({ MESSAGE: (e && e.message) || "Job execution failed" })
|
||||
reject({ MESSAGE: (e && e.message) || "Job execution failed" });
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,21 @@ export class SessionManager {
|
||||
async getSession(accessToken?: string) {
|
||||
await this.createSessions(accessToken);
|
||||
this.createAndWaitForSession(accessToken);
|
||||
return this.sessions.pop();
|
||||
const session = this.sessions.pop();
|
||||
return session;
|
||||
}
|
||||
|
||||
async clearSession(id: string, accessToken?: string) {
|
||||
const deleteSessionRequest = {
|
||||
method: "DELETE",
|
||||
headers: this.getHeaders(accessToken),
|
||||
};
|
||||
return await this.request<Session>(
|
||||
`${this.serverUrl}/compute/sessions/${id}`,
|
||||
deleteSessionRequest
|
||||
).then(() => {
|
||||
this.sessions = this.sessions.filter((s) => s.id !== id);
|
||||
});
|
||||
}
|
||||
|
||||
private async createSessions(accessToken?: string) {
|
||||
|
||||
@@ -44,24 +44,32 @@ export async function makeRequest<T>(
|
||||
if (needsRetry(body)) {
|
||||
if (retryCount < retryLimit) {
|
||||
retryCount++;
|
||||
let retryResponse = await makeRequest(url, retryRequest || request, callback, contentType);
|
||||
let retryResponse = await makeRequest(
|
||||
url,
|
||||
retryRequest || request,
|
||||
callback,
|
||||
contentType
|
||||
);
|
||||
retryCount = 0;
|
||||
|
||||
return retryResponse;
|
||||
} else {
|
||||
retryCount = 0;
|
||||
|
||||
throw new Error('Request retry limit exceeded');
|
||||
throw new Error("Request retry limit exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject({ status: response.status, body });
|
||||
}
|
||||
} else {
|
||||
if (response.status === 204) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
const responseTransformed = await responseTransform(response);
|
||||
let responseText = '';
|
||||
let responseText = "";
|
||||
|
||||
if (typeof responseTransformed === 'string') {
|
||||
if (typeof responseTransformed === "string") {
|
||||
responseText = responseTransformed;
|
||||
} else {
|
||||
responseText = JSON.stringify(responseTransformed);
|
||||
@@ -74,14 +82,19 @@ export async function makeRequest<T>(
|
||||
if (needsRetry(responseText)) {
|
||||
if (retryCount < retryLimit) {
|
||||
retryCount++;
|
||||
let retryResponse = await makeRequest(url, retryRequest || request, callback, contentType);
|
||||
const retryResponse = await makeRequest(
|
||||
url,
|
||||
retryRequest || request,
|
||||
callback,
|
||||
contentType
|
||||
);
|
||||
retryCount = 0;
|
||||
|
||||
return retryResponse;
|
||||
} else {
|
||||
retryCount = 0;
|
||||
|
||||
throw new Error('Request retry limit exceeded');
|
||||
throw new Error("Request retry limit exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
export const needsRetry = (responseText: string): boolean => {
|
||||
return (
|
||||
(responseText.includes('"errorCode":403') &&
|
||||
!!responseText &&
|
||||
((responseText.includes('"errorCode":403') &&
|
||||
responseText.includes("_csrf") &&
|
||||
responseText.includes("X-CSRF-TOKEN")) ||
|
||||
(responseText.includes('"status":403') &&
|
||||
responseText.includes('"error":"Forbidden"')) ||
|
||||
(responseText.includes('"status":449') &&
|
||||
responseText.includes("Authentication success, retry original request"))
|
||||
responseText.includes(
|
||||
"Authentication success, retry original request"
|
||||
)))
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user