Skip to content

Commit

Permalink
feat: restore connection with running scan job
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Mar 10, 2021
1 parent d874d92 commit 5cc36fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/scrap-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = async (baseUrl, options = {}) => {
const protocol = url.parse(baseUrl).protocol;

const log = (msg) => {
if (DEBUG) console.log(msg);
if (DEBUG) console.log(`${options.socket.id} ${msg}`);
socketSend(options.socket, 'status', msg);
};
options.log = log;
Expand Down
11 changes: 11 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const maxConcurrency = 2;
let scansTotal = 0;
let pagesTotal = 0;
let connections = 0;
const sockets = {};
let q;
const startedTime = Date.now();
initQueue();
Expand Down Expand Up @@ -110,6 +111,8 @@ async function onScan(url, args, socket) {
opts.consoleValidate = false; // not needed
opts.socket = socket;

sockets[socket.id].opts = opts;

// console.log('opts: ', opts);
program.outBrief(opts);

Expand Down Expand Up @@ -174,6 +177,8 @@ function sendStats(socket) {
function onSocketConnection(socket) {
log("user connected to server", socket, true);
connections++;
// console.log('socket: ', socket);
sockets[socket.id] = { socket, opts: {} };

submitQueueEvents(socket);

Expand All @@ -192,6 +197,12 @@ function onSocketConnection(socket) {
: "user authenticated: " + auth.email;
log(msg, socket, true);

// restore last connection
if (auth.connectionId && sockets[auth.connectionId]) {
console.log('restore connection: ', auth.connectionId);
sockets[auth.connectionId].opts.socket = socket; // replace socket in scan options
}

sendStats(socket);
});

Expand Down

0 comments on commit 5cc36fa

Please sign in to comment.