Skip to content

Commit

Permalink
feat: add a background api call function
Browse files Browse the repository at this point in the history
  • Loading branch information
kelp404 committed Oct 23, 2020
1 parent daeb101 commit 85fe30b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
50 changes: 35 additions & 15 deletions lib/frontend/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {eventTypes} = require('./constants');
const socket = {
url: null,
pool: {},
backgroundPool: {},
ws: null,

/**
Expand All @@ -27,6 +28,9 @@ const socket = {
Object.keys(socket.pool).forEach(requestId => {
socket.send(requestId);
});
Object.keys(socket.backgroundPool).forEach(requestId => {
socket.send(requestId);
});
};

socket.ws.onmessage = message => {
Expand All @@ -36,12 +40,18 @@ const socket = {
if (content.status >= 200 && content.status < 300) {
if (socket.pool[content.id]) {
socket.pool[content.id].deferred.resolve(content);
} else if (socket.backgroundPool[content.id]) {
socket.backgroundPool[content.id].deferred.resolve(content);
}
} else if (socket.pool[content.id]) {
} else {
const error = new Error(content && content.body && content.body.message);

if (socket.pool[content.id]) {
const error = new Error(content && content.body && content.body.message);
Object.assign(error, content);
socket.pool[content.id].deferred.reject(error);
} else if (socket.backgroundPool[content.id]) {
Object.assign(error, content);
socket.backgroundPool[content.id].deferred.reject(error);
}
}
} else if (content.type === 'notification') {
Expand All @@ -63,13 +73,15 @@ const socket = {

/**
* If the args is string, it will send the content from @socket.pool.
* @param {string|{method: string, url: string, body: Object}} args
* @param {string|{method: string, url: string, body: Object, isBackground: boolean}} args
* @returns {Promise<{type: string, status: number, id: string, body: {Object}}>}
*/
send(args = {}) {
const pool = args.isBackground ? socket.backgroundPool : socket.pool;
let socketContent;

if (typeof args === 'string') {
socketContent = socket.pool[args];
socketContent = socket.pool[args] || socket.backgroundPool[args];
} else {
socketContent = {
id: Math.random().toString(36).substr(2),
Expand All @@ -78,18 +90,19 @@ const socket = {
body: args.body,
deferred: {}
};
socket.pool[socketContent.id] = socketContent;
const promise = new Promise((resolve, reject) => {
socketContent.deferred.resolve = resolve;
socketContent.deferred.reject = reject;
});

pool[socketContent.id] = socketContent;
promise
.then(result => {
delete socket.pool[socketContent.id];
delete pool[socketContent.id];
return result;
})
.catch(error => {
delete socket.pool[socketContent.id];
delete pool[socketContent.id];
throw error;
})
.finally(socket.updateApiStatus);
Expand All @@ -106,7 +119,7 @@ const socket = {
}));
setTimeout(() => {
// Timeout 60s.
if (socket.pool[socketContent.id]) {
if (pool[socketContent.id]) {
socketContent.deferred.reject(new Error('Timeout 60s.'));
}
}, 60000);
Expand Down Expand Up @@ -156,33 +169,40 @@ module.exports = {
};
},
job: {
countAllStateJobs: queueName => socket.send({
countAllStateJobs: (queueName, options) => socket.send({
...options,
method: 'post',
url: `/queues/${queueName}/jobs/_count`
}),
cleanJobs: (queueName, state) => socket.send({
cleanJobs: (queueName, state, options) => socket.send({
...options,
method: 'post',
url: `/queues/${queueName}/jobs/_clean?${queryString.stringify({state})}`
}),
getJobs: (queueName, {state, index, size} = {}) => socket.send({
getJobs: (queueName, {state, index, size} = {}, options = {}) => socket.send({
...options,
method: 'get',
url: `/queues/${queueName}/jobs?${queryString.stringify({state, index, size})}`
}),
getJob: (queueName, jobId) => socket.send({
getJob: (queueName, jobId, options) => socket.send({
...options,
method: 'get',
url: `/queues/${queueName}/jobs/${jobId}`
}),
retryJob: (queueName, jobId) => socket.send({
retryJob: (queueName, jobId, options) => socket.send({
...options,
method: 'post',
url: `/queues/${queueName}/jobs/${jobId}/_retry`
}),
deleteJob: (queueName, jobId) => socket.send({
deleteJob: (queueName, jobId, options) => socket.send({
...options,
method: 'delete',
url: `/queues/${queueName}/jobs/${jobId}`
})
},
queue: {
getQueues: () => socket.send({
getQueues: options => socket.send({
...options,
method: 'get',
url: '/queues'
})
Expand Down
2 changes: 1 addition & 1 deletion lib/frontend/src/pages/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = class Jobs extends Base {
return;
}

api.job.getJob(queueName, jobId)
api.job.getJob(queueName, jobId, {isBackground: true})
.then(({body}) => this.setState(prevState => {
const job = body;

Expand Down
2 changes: 1 addition & 1 deletion lib/frontend/src/pages/shared/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = class Layout extends Base {
}

this.countJobsTimeout[queueName] = setTimeout(() => {
api.job.countAllStateJobs(queueName).then(({body}) => {
api.job.countAllStateJobs(queueName, {isBackground: true}).then(({body}) => {
const counts = body;

if (this.countJobsTimeout[queueName] == null) {
Expand Down

0 comments on commit 85fe30b

Please sign in to comment.