Coder Social home page Coder Social logo

opportunitylivetv / areyoubeingserved Goto Github PK

View Code? Open in Web Editor NEW

This project forked from samgiles/areyoubeingserved

1.0 3.0 1.0 9 KB

Are you being served? Wait for runtime dependencies when using docker-compose

License: Mozilla Public License 2.0

JavaScript 100.00%

areyoubeingserved's Introduction

Are you being served?

Is your service accepting requests yet?

bin/waitfortcp --host localhost --port 3000 --timeout 20

An extensible library for waiting on things, and managing runtime dependencies. Useful when using something like docker-compose to create your environment, you can order things based on the readiness of other services.

Command line wait for a TCP connection

This can be applied to most things: an HTTP(S) server, a database server, etc.

This command will wait for localhost to listen on port 3000 for up to 20 seconds.

bin/waitfortcp --host localhost --port 3000 --timeout 20

Example with docker-compose, a database, and a Node.JS app

./Dockerfile

Simple Dockerfile for a Node app, notably the entry point is npm start.

FROM mhart/alpine-node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install

COPY . /usr/src/app

EXPOSE 3000
CMD [ "npm", "start"]

./docker-compose.yml

Our app depends on a PostGIS database db.

version: "2"
services:
  db:
    image: mdillon/postgis
    environment:
      POSTGRES_DB: appdb
    ports:
      - "5432:5432"
  myapp:
    build:
      context: ./
    depends_on:
      - db
    links:
      - db
    ports:
      - "3000:3000"

./package.json

In prestart we wait for the database port to accept a connection before running our database migrations.

{
  "name": "myapp",
  "main": "index.js",
  "scripts": {
    "prestart": "waitfortcp --host db --port 5432 && db-migrate up",
    "start": "bin/service",
  },
  "author": "Awesome Developer <[email protected]>",
  "license": "MPL-2.0",
  "dependencies": {
    "areyoubeingserved": "^1.0.0",
	"db-migrate": "*"
  }
}

Extending

You can resuse the retry logic, the library exports a function createRetry.

createRetry(timeoutInSeconds, function)

The function should return true, if the test succeeded, or false if not, and can return Promise equivalents.

Example

const { createRetry } = require('waitforit');

createRetry(10, function() {
  return new Promise((resolve, reject) => {
    doThing((err) => {
      if (err) {
        // Not ready :(
        resolve(false);
      } else {
        // Ready!
        resolve(true);
      }
    });
  });
});

License

MPL-2.0

areyoubeingserved's People

Contributors

samgiles avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

martina6hall

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.