Skip to content

Commit

Permalink
Merge pull request #3 from industrial-edge/BugFix-InfluxDB-error
Browse files Browse the repository at this point in the history
Integration of wait function in data collector to wait for influxdb to be ready
  • Loading branch information
Boehmi-TB authored May 10, 2021
2 parents fd4018d + ec81aad commit a82407f
Show file tree
Hide file tree
Showing 7 changed files with 681 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/data-collector/node_modules/*
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
datacollector:
build:
context: ./src/data-collector
image: data-collector_edge:0.0.1
image: data-collector_edge:0.0.2
restart: always
networks:
- proxy-redirect
Expand Down
1 change: 1 addition & 0 deletions src/data-collector/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
14 changes: 9 additions & 5 deletions src/data-collector/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#start with a nodejs base-image
# start with a nodejs base-image
FROM node:12.18.3-alpine

# Create app directory
WORKDIR /usr/src/app

#install required nodejs packages
RUN npm install influx
RUN npm install mqtt
# Install app dependencies
COPY package*.json ./

#copy the nodejs script into the container
RUN npm install --production

# Bundle app source
COPY . .

EXPOSE 3000

#set start command for container
CMD [ "node", "app.js" ]
24 changes: 24 additions & 0 deletions src/data-collector/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ const influx = new Influx.InfluxDB({
]
})


const delay = ms => new Promise(res => setTimeout(res, ms));

createDatabase = async (influx, influxdbDatabase) => {
i = 0
do {
await delay(5000);
hosts = await influx.ping(5000)
console.log(hosts)
i++;
console.log(`Try to connect to InfluxDB: try number ${i}`)
console.log(`influxdb Status: ${hosts[0].online}`)
} while (!hosts[0].online)
console.log("Influx DB online");
influx.createDatabase(influxdbDatabase)
}

createDatabase(influx, INFLUXDB_DATABASE).then(res => {
console.log("Database created");
})


/*
//create database
function createDatabase() {
influx.createDatabase(INFLUXDB_DATABASE)
Expand All @@ -72,6 +95,7 @@ function createDatabase() {
//wait 12 seconds before creating database (influx container needs a while to initialize)
setTimeout(createDatabase, 12000);
*/

/* Publish response after recieved message*/
client.on('message', function (topic, message) {
Expand Down
Loading

0 comments on commit a82407f

Please sign in to comment.