Saturday, June 22, 2019

Override env. variable and shell script

Override env. variable and shell script defined in Docker file in run command.

build an image

docker build -t busybox .

run container with env. variable & run CMD in Dockerfile

docker run busybox

override env. variable in Dockerfile
docker run --env ENV_TARGET=yahoo.com busybox

override CMD in Dockerfile
docker run busybox sh -c 'whoami && pwd'

Dockerfile

FROM busybox

WORKDIR /app
ENV ENV_TARGET google.com
COPY subs.sh subs.sh
RUN chmod +x subs.sh
CMD ["/app/subs.sh"]

subs.sh

#!/bin/sh

ping -c 2 ${ENV_TARGET}

axios concurrent calls

Axios can make multiple concurrent calls. In app.js calls to two end-points are started at the same time so total waiting time is 4 seconds. Server latency is 4 seconds for demo purposes.

app.js

const axios = require('axios')
 
try {
    console.log("start");
    function getUserName() {
        return axios.get('http://localhost:3000/heroes');
    }
 
    function getId() {
        return axios.get('http://localhost:3000/heroes');
    }
 
    // Performing multiple concurrent requests waiting time 4 seconds
    axios.all([getUserName(), getId()])
        .then(axios.spread(function (acct, perms) {
            const user = acct.data[0].param;
            const id = perms.data[1].param;
            console.log(`${user} ${id}`)
            const str = 'https://jsonplaceholder.typicode.com/' + user + '/' + id
            console.log(str)
            axios.get(str).then(response => console.log(response.data.name));
        }));
} catch (error) {
    console.error(error);
}

Monday, June 17, 2019

axios async await

Sometimes you need to wait for one (axios) call to finish to continue with next one. It's accomplished by using async/await. Last axios call in app.js downloads a file without waiting for getUser() to be finished.

set server delay to 4 seconds for demo purposes

{
  "name": "async-await",
  "scripts": {
    "serve-json": "json-server --delay 4000 --watch db.json",
    "start": "node app.js"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "json-server": "^0.15.0"
  }
}

db.json

{
    "heroes": [
        {
            "param": "users"
        },
        {
            "param": "1"
        }
    ]
}

app.js

const axios = require('axios')
const fs = require('fs')
 
async function getUser() {
    try {
        console.log("start");
        const response = await axios.get('http://localhost:3000/heroes');
        var1 = response.data[0].param; // users
        console.log("4 sec after start ", var1);
        const response2 = await axios.get('http://localhost:3000/heroes');
        var2 = response2.data[1].param; // 1
        console.log("8 sec after start", var2);
        const response3 = await axios.get('https://jsonplaceholder.typicode.com/' + var1 + '/' + var2);
        console.log(response3.data.name);
    } catch (error) {
        console.error(error);
    }
}
 
getUser()
 
// call below doesnot wait for getUser() to be finished 
axios({
    method: 'get',
    url: 'https://jsonplaceholder.typicode.com/users/2',
    responseType: 'stream'
  })
    .then(function (response) {
      response.data.pipe(fs.createWriteStream('full-user.json'))
});