UNPKG

9 kBMarkdownView Raw
1# wait-on - wait for files, ports, sockets, http(s) resources
2
3wait-on is a cross-platform command line utility which will wait for files, ports, sockets, and http(s) resources to become available (or not available using reverse mode). Functionality is also available via a Node.js API. Cross-platform - runs everywhere Node.js runs (linux, unix, mac OS X, windows)
4
5wait-on will wait for period of time for a file to stop growing before triggering availability which is good for monitoring files that are being built. Likewise wait-on will wait for period of time for other resources to remain available before triggering success.
6
7For http(s) resources wait-on will check that the requests are returning 2XX (success) to HEAD or GET requests (after following any redirects).
8
9wait-on can also be used in reverse mode which waits for resources to NOT be available. This is useful in waiting for services to shutdown before continuing. (Thanks @skarbovskiy for adding this feature)
10
11[![Build Status](https://secure.travis-ci.org/jeffbski/wait-on.png?branch=master)](http://travis-ci.org/jeffbski/wait-on)
12
13## Installation
14
15Requires node.js/iojs >= 4.0.0
16
17(For older Node.js engines, use wait-on@1.5.4)
18
19```bash
20npm install wait-on # local version
21OR
22npm install -g wait-on # global version
23```
24
25## Usage
26
27Use from command line or using Node.js programmatic API.
28
29### CLI Usage
30
31Assuming NEXT_CMD is the command to run when resources are available, then wait-on will wait and then exit with successfull exit code (0) once all resrouces are available causing NEXT_CMD to be run.
32
33wait-on can also be used in reverse mode which waits for resources to NOT be available. This is useful in waiting for services to shutdown before continuing. (Thanks @skarbovskiy for adding)
34
35If wait-on is interrupted before all resources are available, it will exit with non-zero exit code and thus NEXT_CMD will not be run.
36
37```bash
38wait-on file1 && NEXT_CMD # wait for file1, then exec NEXT_CMD
39wait-on f1 f2 && NEXT_CMD # wait for both f1 and f2, the exec NEXT_CMD
40wait-on http://localhost:8000/foo && NEXT_CMD # wait for http 2XX HEAD
41wait-on https://myserver/foo && NEXT_CMD # wait for https 2XX HEAD
42wait-on http-get://localhost:8000/foo && NEXT_CMD # wait for http 2XX GET
43wait-on https-get://myserver/foo && NEXT_CMD # wait for https 2XX GET
44wait-on tcp:4000 && NEXT_CMD # wait for service to listen on a TCP port
45wait-on socket:/path/mysock # wait for service to listen on domain socket
46wait-on http://unix:/var/SOCKPATH:/a/foo # wait for http HEAD on domain socket
47wait-on http-get://unix:/var/SOCKPATH:/a/foo # wait for http GET on domain socket
48```
49
50```
51Usage: wait-on {OPTIONS} resource [...resource]
52
53Description:
54
55 wait-on is a command line utility which will wait for files, ports,
56 sockets, and http(s) resources to become available (or not available
57 using reverse flag). Exits with success code (0) when all resources
58 are ready. Non-zero exit code if interrupted or timed out.
59
60 Options may also be specified in a config file (js or json). For
61 example --config configFile.js would result in configFile.js being
62 required and the resulting object will be merged with any
63 command line options before wait-on is called. See exampleConfig.js
64
65 In shell combine with && to conditionally run another command
66 once resources are available. ex: wait-on f1 && NEXT_CMD
67
68 resources types are defined by their prefix, if no prefix is
69 present, the resource is assumed to be of type 'file'
70
71 resource prefixes are:
72
73 file: - regular file (also default type). ex: file:/path/to/file
74 http: - HTTP HEAD returns 2XX response. ex: http://m.com:90/foo
75 https: - HTTPS HEAD returns 2XX response. ex: https://my/bar
76 http-get: - HTTP GET returns 2XX response. ex: http://m.com:90/foo
77 https-get: - HTTPS GET returns 2XX response. ex: https://my/bar
78 tcp: - TCP port is listening. ex: 1.2.3.4:9000 or foo.com:700
79 socket: - Domain Socket is listening. ex: socket:/path/to/sock
80 For http over socket, use http://unix:SOCK_PATH:URL_PATH
81 like http://unix:/path/to/sock:/foo/bar or
82 http-get://unix:/path/to/sock:/foo/bar
83
84Standard Options:
85
86 -c, --config
87
88 js or json config file, useful for http(s) options
89
90 -d, --delay
91
92 Initial delay before checking for resources in ms, default 0
93
94 -i, --interval
95
96 Interval to poll resources in ms, default 250ms
97
98 -l, --log
99
100 Log resources begin waited on and when complete or errored
101
102 -r, --reverse
103
104 Reverse operation, wait for resources to NOT be available
105
106 -t, --timeout
107
108 Maximum time in ms to wait before exiting with failure (1) code,
109 default Infinity
110
111 -v, --verbose
112
113 Enable debug output to stdout
114
115 -w, --window
116
117 Stability window, the time in ms defining the window of time that
118 resource needs to have not changed (file size or availability) before
119 signalling success, default 750ms. If less than interval, it will be
120 reset to the value of interval.
121
122 -h, --help
123
124 Show this message
125```
126
127### Node.js API usage
128
129```javascript
130var waitOn = require('wait-on');
131var opts = {
132 resources: [
133 'file1',
134 'http://foo.com:8000/bar',
135 'https://my.com/cat',
136 'http-get://foo.com:8000/bar',
137 'https-get://my.com/cat',
138 'tcp:foo.com:8000',
139 'socket:/my/sock',
140 'http://unix:/my/sock:/my/url',
141 'http-get://unix:/my/sock:/my/url'
142 ],
143 delay: 1000, // initial delay in ms, default 0
144 interval: 100, // poll interval in ms, default 250ms
145 timeout: 30000, // timeout in ms, default Infinity
146 window: 1000, // stabilization time in ms, default 750ms
147
148 // http options
149 ca: [ /* strings or binaries */ ],
150 cert: [ /* strings or binaries */ ],
151 key: [ /* strings or binaries */ ],
152 passphrase: 'yourpassphrase',
153 auth: {
154 user: 'theuser', // or username
155 pass: 'thepassword' // or password
156 },
157 httpSignature: {
158 keyId: 'yourKeyId',
159 key: 'yourKey'
160 },
161 strictSSL: false,
162 followAllRedirects: true,
163 followRedirect: true
164};
165waitOn(opts, function (err) {
166 if (err) { return handleError(err); }
167 // once here, all resources are available
168});
169```
170
171waitOn(opts, cb) - function which triggers resource checks
172
173 - opts.resources - array of string resources to wait for. prefix determines the type of resource with the default type of `file:`
174 - opts.delay - optional initial delay in ms, default 0
175 - opts.interval - optional poll resource interval in ms, default 250ms
176 - opts.log - optional flag which outputs to stdout, remaining resources waited on and when complete or errored
177 - opts.reverse - optional flag to reverse operation so checks are for resources being NOT available, default false
178 - opts.timeout - optional timeout in ms, default Infinity. Aborts with error.
179 - opts.verbose - optional flag which outputs debug output, default false
180 - opts.window - optional stabilization time in ms, default 750ms. Waits this amount of time for file sizes to stabilize or other resource availability to remain unchanged.
181 - http(s) specific options, see https://github.com/request/request#readme for specific details
182 - opts.ca: [ /* strings or binaries */ ],
183 - opts.cert: [ /* strings or binaries */ ],
184 - opts.key: [ /* strings or binaries */ ],
185 - opts.passphrase: 'yourpassphrase',
186 - opts.auth: { user, pass }
187 - opts.httpSignature: { keyId, key }
188 - opts.strictSSL: false,
189 - opts.followAllRedirects: true,
190 - opts.followRedirect: true
191
192 - cb(err) - if err is provided then, resource checks did not succeed
193
194
195## Goals
196
197 - simple command line utility and Node.js API for waiting for resources
198 - wait for files to stabilize
199 - wait for http(s) resources to return 2XX in response to HEAD request
200 - wait for http(s) resources to return 2XX in response to GET request
201 - wait for services to be listening on tcp ports
202 - wait for services to be listening on unix domain sockets
203 - configurable initial delay, poll interval, stabilization window, timeout
204 - command line utility returns success code (0) when resources are availble
205 - command line utility that can also wait for resources to not be available using reverse flag. This is useful for waiting for services to shutdown before continuing.
206 - cross platform - runs anywhere Node.js runs (linux, unix, mac OS X, windows)
207
208## Why
209
210I frequently need to wait on build tasks to complete or services to be available before starting next command, so this project makes that easier and is portable to everywhere Node.js runs.
211
212## Get involved
213
214If you have input or ideas or would like to get involved, you may:
215
216 - contact me via twitter @jeffbski - <http://twitter.com/jeffbski>
217 - open an issue on github to begin a discussion - <https://github.com/jeffbski/wait-on/issues>
218 - fork the repo and send a pull request (ideally with tests) - <https://github.com/jeffbski/wait-on>
219
220## License
221
222 - [MIT license](http://github.com/jeffbski/wait-on/raw/master/LICENSE)