chillin
v1.0.4
Published
NPM package API and Bash/CLI tool to implement waiting on an external resource's availability.
Downloads
4
Readme
Introduction
This tool is intended to be used either in bash or a node.js project. It waits for a resource (such as a database) to become available, then it starts another process. It just chills until the timeout expires or the wait condition is satisfied.
Let's illustrate with an example. Say you have a Docker Compose environment that starts containers S and D (for database). S depends on D, but D can be slow to start up. S can use chillin in either of two ways.
- The NodeJS startup script can be modified to use the chillin NPM package.
- A startup bash script can utilize the chillin CLI.
Please Note Chillin uses EcmaScript 6. It will not run on older versions of NodeJS!
NodeJS Example
const chillin = require('chillin')
, promise = chillin.loadWaiterModule('port')
.configure('host', 'www.google.com')
.configure('port', 80)
.start()
promise.then(function(){
console.log('OK!')
}, function(x){
console.error(`Failed: ${x}`)
})
To Run Chillin Inside Your Project
> npm install chillin --save-dev
> export PATH=$PATH:$(npm bin)
> chillin port www.google.com 80
BASH Example
Please reference the example above if you are installing Chillin in your project rather than as a global package. The line that changes the PATH variable can be added to the example below to make it run with a locally installed package.
#!/usr/bin/env bash
if [ $# -lt 1 ]; then
echo "Name/IP of Postgres server required." 1>&2;
echo "> $0 PGSERVER" 1>&2;
exit 1
fi
#If you want to suppress error output on failure, you can redirect
#stderr to null on the next line.
chillin port $1 5432
#Get the exit code
RESULT=$?
if [ $RESULT == 0 ]; then
echo "Starting the program that depends on Postgres..."
else
echo "Failed! Not starting the program." 1>&2
exit $RESULT
fi
Types of Waits
There are three built-in waiter modules: port, exec, and mock. The port waiter is shown in the examples. The exec module waits for a process to start and complete or time out. The mock module is for testing purposes. Other modules may be loaded using a path consistent with require() in NodeJS.
The port Waiter
chillin www.github.com 80 --timeout 2000
The positional arguments are ADDRESS PORT
The exec Waiter
chillin --timeout 2000 echo "Hello"
The positional arguments correspond to COMMAND arg1 arg2 ... argN