Skip to content

Commit

Permalink
feat: cancel command
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Mar 10, 2021
1 parent 5cc36fa commit 3db580b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/scrap-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,13 @@ module.exports = async (baseUrl, options = {}) => {
console.log(e);
}

crawler.on('requeststarted', async options => {
crawler.on('requeststarted', async opts => {
const queueCount = await crawler.queueSize();
requestedCount = crawler.requestedCount() + 1;
log(`${requestedCount} ${decodeURI(options.url)} (${queueCount})`);
if (options.cancel) {
crawler.setMaxRequest(requestedCount); // cancel command
}
log(`${requestedCount} ${decodeURI(opts.url)} (${queueCount})`);
});
crawler.on('requestfailed', error => {
console.error(
Expand Down
5 changes: 5 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ function onSocketConnection(socket) {
return onScan(url, args, socket)
});

socket.on("cancel", () => {
log('cancel command...', socket);
sockets[socket.id].opts.cancel = true;
});

socket.on("disconnect", () => {
clearInterval(interval);
connections--;
Expand Down

0 comments on commit 3db580b

Please sign in to comment.