Browse Source

Decouple processes to different docker containers#187(WIP) (#188)

analytics and server moved to separate containers, added docker-compose file
pull/1/head
Evgeny Smyshlyaev 6 years ago committed by GitHub
parent
commit
de3b64ce95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      Dockerfile
  2. 5
      analytics/.dockerignore
  3. 20
      analytics/Dockerfile
  4. 2
      analytics/config.py
  5. 19
      docker-compose.yml
  6. 4
      server/.dockerignore
  7. 26
      server/Dockerfile
  8. 1
      server/src/config.ts
  9. 17
      server/src/services/analytics_service.ts

29
Dockerfile

@ -1,29 +0,0 @@
FROM python:3.6.6
EXPOSE 8000
VOLUME [ "/var/www/data" ]
COPY . /var/www
WORKDIR /var/www/analytics
RUN pip install -r requirements.txt
RUN apt-get update && apt-get install -y \
apt-utils \
gnupg \
curl \
python \
make \
g++ \
git
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get update && apt-get install -y nodejs
WORKDIR /var/www/server
RUN npm install && npm run build
CMD ["npm", "start"]

5
analytics/.dockerignore

@ -0,0 +1,5 @@
.git
npm-debug
node_modules
__pycache__
.vscode

20
analytics/Dockerfile

@ -0,0 +1,20 @@
FROM python:3.6.6
WORKDIR /var/www/analytics
COPY ./requirements.txt /var/www/analytics
RUN pip install -r requirements.txt \
&& apt-get update && apt-get install -y \
apt-utils \
gnupg \
curl \
make \
g++ \
git
VOLUME [ "/var/www/data" ]
COPY . /var/www/analytics/
CMD ["python", "server.py"]

2
analytics/config.py

@ -25,4 +25,4 @@ def get_config_field(field, default_val = None):
raise Exception('Please configure {}'.format(field))
ZMQ_DEV_PORT = get_config_field('ZMQ_DEV_PORT', '8002')
ZMQ_CONNECTION_STRING = get_config_field('ZMQ_CONNECTION_STRING', 'tcp://*:%s' % ZMQ_DEV_PORT)
ZMQ_CONNECTION_STRING = get_config_field('ZMQ_CONNECTION_STRING', 'tcp://0.0.0.0:%s' % ZMQ_DEV_PORT)

19
docker-compose.yml

@ -0,0 +1,19 @@
version: '2'
services:
server:
image: hastic/server:latest
build: server
environment:
HASTIC_API_KEY: ${HASTIC_API_KEY}
ZMQ_CONNECTION_STRING: tcp://analytics:8002
ports:
- 8000:8000
volumes:
- data-volume:/var/www/data
analytics:
image: hastic/analytics:latest
build: analytics
volumes:
data-volume:

4
server/.dockerignore

@ -0,0 +1,4 @@
.git
node_modules
npm-debug
.vscode

26
server/Dockerfile

@ -0,0 +1,26 @@
FROM python:3.6.6
RUN apt-get install curl \
bash \
gnupg \
make \
g++ \
&& curl -sL https://deb.nodesource.com/setup_8.x | bash - \
&& apt-get update \
&& apt-get install nodejs
VOLUME [ "/var/www/data" ]
WORKDIR /var/www/server
COPY package.json /var/www/server
RUN npm install
COPY . /var/www/server
RUN npm run build
ENV INSIDE_DOCKER true
CMD ["npm", "start"]

1
server/src/config.ts

@ -20,6 +20,7 @@ export const HASTIC_PORT = getConfigField('HASTIC_PORT', '8000');
export const ZMQ_CONNECTION_STRING = getConfigField('ZMQ_CONNECTION_STRING', null);
export const ZMQ_IPC_PATH = getConfigField('ZMQ_IPC_PATH', path.join(os.tmpdir(), 'hastic'));
export const ZMQ_DEV_PORT = getConfigField('ZMQ_DEV_PORT', '8002');
export const ZMQ_HOST = getConfigField('ZMQ_HOST', '127.0.0.1');
export const HASTIC_API_KEY = getConfigField('HASTIC_API_KEY');
export const ANLYTICS_PING_INTERVAL = 500; // ms

17
server/src/services/analytics_service.ts

@ -18,8 +18,12 @@ export class AnalyticsService {
private _ipcPath: string = null;
private _analyticsPinger: NodeJS.Timer = null;
private _isClosed = false;
private _productionMode = false;
private _inDocker = false;
constructor(private _onMessage: (message: AnalyticsMessage) => void) {
this._productionMode = process.env.NODE_ENV !== 'development';
this._inDocker = process.env.INSIDE_DOCKER !== undefined;
this._init();
}
@ -67,10 +71,12 @@ export class AnalyticsService {
private async _init() {
this._requester = zmq.socket('pair');
let productionMode = process.env.NODE_ENV !== 'development';
this._zmqConnectionString = `tcp://127.0.0.1:${config.ZMQ_DEV_PORT}`; // debug mode
if(productionMode) {
this._zmqConnectionString = `tcp://${config.ZMQ_HOST}:${config.ZMQ_DEV_PORT}`; // debug mode
if(this._inDocker) {
this._zmqConnectionString = config.ZMQ_CONNECTION_STRING;
} else if(this._productionMode && !this._inDocker) {
this._zmqConnectionString = config.ZMQ_CONNECTION_STRING;
if(this._zmqConnectionString === null) {
var createResult = await AnalyticsService.createIPCAddress();
@ -84,7 +90,7 @@ export class AnalyticsService {
this._requester.on("message", this._onAnalyticsMessage.bind(this));
console.log('Ok');
if(productionMode) {
if(this._productionMode && !this._inDocker) {
console.log('Creating analytics process...');
try {
var cp = await AnalyticsService._runAnalyticsProcess(this._zmqConnectionString);
@ -165,12 +171,13 @@ export class AnalyticsService {
private async _onAnalyticsDown() {
console.log('Analytics is down');
if(process.env.NODE_ENV !== 'development') {
if(this._productionMode && !this._inDocker) {
await AnalyticsService._runAnalyticsProcess(this._zmqConnectionString);
}
}
private _onAnalyticsMessage(data: any) {
let text = data.toString();
if(text === 'PONG') {
this._pingResponded = true;

Loading…
Cancel
Save