Skip to content

Commit

Permalink
Support for HAPROXY load balancer (#6)
Browse files Browse the repository at this point in the history
* 1) Added support for HAPROXY load balancer and example in docker-compose
2) Fixed Readme

* 1) Fixed compose and README.md for it.

* Forgot to fix in click

Co-authored-by: Yatskov <[email protected]>
  • Loading branch information
Yatskov and Yatskov authored Sep 10, 2020
1 parent f385ec1 commit c8a7e9f
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 18 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ So you should use `docker run --cap-add=SYS_ADMIN` option.
$ docker run -d -p 3000:3000 --name scrapy-puppeter-service --cap-add SYS_ADMIN isprascrawlers/scrapy-puppeteer-service
```

To run example which shows how to deploy several instances of service with load balancer use this command.
```shell script
$ docker-compose up -d
```

## API

Expand All @@ -29,10 +33,9 @@ For All requests puppeteer browser creates new incognito browser context and new
If your want to reuse your browser context simple send context_id in your query.
All request return their context ids in response.
Also you could reuse your browser page and more actions with it.
In order to so you should send closePage=false in your previous request, that would made service save current page and
return its page_id.
If you want to send request to this use page_id and context_id params in your next request.

In order to do so you should send in your request pageId that is returned in your previous request,
that would make service reuse current page and return again its pageId.
If you want to close the page you are working with you should send in query param "closePage" with non-empty value.
If you want your requests on page make through proxy, just add to normal request "proxy" param.
Proxy username and password params are optional.
Also you can add extra http headers to each request that is made on page.
Expand Down Expand Up @@ -153,4 +156,4 @@ You need to explicitly close the browser tab once you don't need it (e.g. at the
- [x] support of extra headers
- [ ] error handling for requests
- [ ] har support
- [ ] scaling to several docker containers
- [x] scaling to several docker containers
15 changes: 15 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3.7"

services:

scrapy-puppeteer-service-1:
build:
context: .
ports:
- "3001:3000"

scrapy-puppeteer-service-2:
build:
context: .
ports:
- "3002:3000"
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.7"

services:
haproxy:
image: "haproxy"
ports:
- "3000:3000"
volumes:
- "./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro"

scrapy-puppeteer-service-1:
image: "isprascrawlers/scrapy-puppeteer-service"
environment:
TIMEOUT: 600000
cap_add:
- SYS_ADMIN
restart: always

scrapy-puppeteer-service-2:
image: "isprascrawlers/scrapy-puppeteer-service"
environment:
TIMEOUT: 600000
cap_add:
- SYS_ADMIN
restart: always
15 changes: 15 additions & 0 deletions haproxy/haproxy.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
frontend entrypoint
bind 0.0.0.0:3000
mode http
default_backend balancer

backend balancer
mode http
option httpclose
balance leastconn
stick-table type string len 64 size 100k expire 15m
stick store-response res.hdr(scrapy-puppeteer-service-context-id)
stick match urlp(contextId)
server app1 scrapy-puppeteer-service-1:3000 check
server app2 scrapy-puppeteer-service-2:3000 check

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrapy-puppeteer-service",
"version": "0.0.3",
"version": "0.0.4",
"private": true,
"scripts": {
"start": "node ./bin/www"
Expand Down
4 changes: 2 additions & 2 deletions routes/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ router.post('/', async function (req, res, next) {
}

let response = await utils.perfomAction(req, action);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(response));
res.header('scrapy-puppeteer-service-context-id', response.contextId);
res.send(response);
} catch (e) {
next(e);
}
Expand Down
4 changes: 2 additions & 2 deletions routes/click.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ router.post('/', async function (req, res, next) {

try {
let response = await utils.perfomAction(req, action);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(response));
res.header('scrapy-puppeteer-service-context-id', response.contextId);
res.send(response)
} catch (e) {
next(e);
}
Expand Down
3 changes: 1 addition & 2 deletions routes/goback.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ router.post('/', async function (req, res, next) {

try {
let response = await utils.perfomAction(req, action);
res.header('scrapy-puppeteer-service-context-id', response.contextId);
res.send(response)
// res.setHeader('Content-Type', 'application/json');
// res.send(JSON.stringify(response));
} catch (e) {
next(e);
}
Expand Down
3 changes: 1 addition & 2 deletions routes/goforward.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ router.post('/', async function (req, res, next) {

try {
let response = await utils.perfomAction(req, action);
res.header('scrapy-puppeteer-service-context-id', response.contextId);
res.send(response)
// res.setHeader('Content-Type', 'application/json');
// res.send(JSON.stringify(response));
} catch (e) {
next(e);
}
Expand Down
1 change: 1 addition & 0 deletions routes/goto.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ router.post('/', async function (req, res, next) {

try {
let response = await utils.perfomAction(req, action);
res.header('scrapy-puppeteer-service-context-id', response.contextId);
res.send(response);
} catch (e) {
next(e);
Expand Down
1 change: 1 addition & 0 deletions routes/har.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ router.post('/', async function (req, res, next) {

try {
let response = await utils.perfomAction(req, action);
res.header('scrapy-puppeteer-service-context-id', response.contextId);
res.send(response);
} catch (e) {
next(e);
Expand Down
4 changes: 2 additions & 2 deletions routes/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ router.post('/', async function (req, res, next) {

try {
let response = await utils.perfomAction(req, action);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(response));
res.header('scrapy-puppeteer-service-context-id', response.contextId);
res.send(response);
} catch (e) {
next(e);
}
Expand Down
4 changes: 2 additions & 2 deletions routes/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ router.post('/', async function (req, res, next) {

try {
let response = await utils.perfomAction(req, action);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(response));
res.header('scrapy-puppeteer-service-context-id', response.contextId);
res.send(response);
} catch (e) {
next(e);
}
Expand Down

0 comments on commit c8a7e9f

Please sign in to comment.