Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/services/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { getUserToken } from "../managers/user-manager";
export function UserNetworkInstance() {
var instance = axios.create({
baseURL: settings.GIST_QUEUE_API_ENDPOINT[Gist.config.env],
timeout: 20000 // 20 seconds, TODO: should we reconsider?
})
timeout: 5000
});
if (settings.getQueueAPIVersion() === "3") {
instance.defaults.baseURL = settings.GIST_QUEUE_V3_API_ENDPOINT[Gist.config.env];
}
instance.defaults.headers.common['X-CIO-Site-Id'] = Gist.config.siteId;
instance.defaults.headers.common['X-CIO-Client-Platform'] = 'web';
var userToken = getUserToken();
Expand Down
34 changes: 31 additions & 3 deletions src/services/queue-service.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { UserNetworkInstance } from './network';
import { setKeyToLocalStore } from '../utilities/local-storage';
import { getKeyFromLocalStore, setKeyToLocalStore } from '../utilities/local-storage';
import { log } from "../utilities/log";
import { isUsingGuestUserToken } from '../managers/user-manager';
import { getUserLocale } from '../managers/locale-manager';
import { settings } from './settings';
import { v4 as uuidv4 } from 'uuid';

const defaultPollingDelayInSeconds = 600;
var currentPollingDelayInSeconds = defaultPollingDelayInSeconds;
var checkInProgress = false;

export const userQueueNextPullCheckLocalStoreName = "gist.web.userQueueNextPullCheck";
export const sessionIdLocalStoreName = "gist.web.sessionId";
export async function getUserQueue() {
var response;
try {
if (!checkInProgress) {
var timestamp = new Date().getTime();
checkInProgress = true;
var headers = {
"X-Gist-User-Anonymous": isUsingGuestUserToken(),
"Content-Language": getUserLocale()
}
response = await UserNetworkInstance().post(`/api/v2/users?timestamp=${timestamp}`, {}, { headers: headers });

if (settings.getQueueAPIVersion() === "3") {
response = await UserNetworkInstance().post(`/api/v3/users?sessionId=${getSessionId()}`, {}, { headers: headers });
} else {
var timestamp = new Date().getTime();
response = await UserNetworkInstance().post(`/api/v2/users?timestamp=${timestamp}`, {}, { headers: headers });
}
}
} catch (error) {
if (error.response) {
Expand All @@ -30,11 +38,31 @@ export async function getUserQueue() {
} finally {
checkInProgress = false;
scheduleNextQueuePull(response);
setQueueAPIVersion(response);
}

return response;
}

function setQueueAPIVersion(response) {
if (response && response.headers) {
var queueVersion = response.headers["x-cio-queue-version"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker to merge but I was wondering if the "x-cio-queue-version" should be a constant. That was the one hard coded value that made me pause. It isn't in multiple places or anything. I gave it another thought as something that isn't hardcoded thinking it was important and shouldn't change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were using it in many places yeah, but in this case I think its easier to read.

if (queueVersion) {
settings.setQueueAPIVersion(queueVersion);
}
}
}

function getSessionId() {
var sessionId = getKeyFromLocalStore(sessionIdLocalStoreName);
if (!sessionId) {
sessionId = uuidv4();
}
// The session ID TTL is renewed with every poll request and extended by 10 minutes.
setKeyToLocalStore(sessionIdLocalStoreName, sessionId, new Date(new Date().getTime() + 600000));
return sessionId;
}

function scheduleNextQueuePull(response) {
if (response && response.headers) {
var pollingInterval = response.headers['x-gist-queue-polling-interval'];
Expand Down
16 changes: 16 additions & 0 deletions src/services/settings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { getKeyFromLocalStore, setKeyToLocalStore } from '../utilities/local-storage';
import { log } from '../utilities/log';
const userQueueVersionLocalStoreName = "gist.web.userQueueVersion";

export const settings = {
ENGINE_API_ENDPOINT: {
"prod": "https://engine.api.gist.build",
Expand All @@ -9,9 +13,21 @@ export const settings = {
"dev": "https://gist-queue-consumer-api.cloud.dev.gist.build",
"local": "http://api.local.gist.build:86"
},
GIST_QUEUE_V3_API_ENDPOINT: {
"prod": "https://consumer.cloud.gist.build",
"dev": "https://consumer.cloud.dev.gist.build",
"local": "http://api.local.gist.build:86"
},
GIST_VIEW_ENDPOINT: {
"prod": "https://renderer.gist.build/3.0",
"dev": "https://renderer.gist.build/3.0",
"local": "http://app.local.gist.build:8080/web"
},
getQueueAPIVersion: function() {
return getKeyFromLocalStore(userQueueVersionLocalStoreName) ?? "2";
},
setQueueAPIVersion: function(version) {
setKeyToLocalStore(userQueueVersionLocalStoreName, version);
log(`Set user queue version to "${version}"`);
}
}