Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- checkAppPr: function (port) {
- var result = Promise.defer();
- var checkStatus = function (next, result, times) {
- portscanner.checkPortStatus(port, '127.0.0.1', function (error, status) {
- if (error) {
- result.reject(error);
- } else {
- if (status === 'open') {
- console.log("Application status: open");
- result.resolve();
- } else {
- times--;
- if (times > 0) {
- setTimeout(function () {
- next(next, result, times);
- }, 1000);
- } else {
- result.reject("start timeout");
- }
- }
- }
- })
- }
- checkStatus(checkStatus, result, 20);
- return result.promise;
- }
- // Returns a promise that resolves when the port is open
- checkPortStatus: function(port, host){
- return new Promise((resolve, reject) => {
- portscanner.checkPortStatus(port, host, function(error, status) {
- if(error)
- reject(error);
- else if(status === 'open')
- resolve(status);
- else
- reject(new Error('Port is not open'));
- });
- });
- },
- // Your API function
- checkAppPort: function(port, retriesLeft) {
- const TIME_BETWEEN_CHECKS = 1000;
- const HOST = '127.0.0.1';
- const RETRIES = 20;
- // Setting a default to retriesLeft
- retriesLeft = retriesLeft === void 0 ? RETRIES : retriesLeft;
- if(!port) throw new Error('Port is required');
- if(retriesLeft === 0) return Promise.reject('Timed Out');
- return new Promise((resolve, reject) => {
- // We call our checker. When it resolves, it calls this promise's resolve.
- // If it rejects, we do added work.
- this.checkPortStatus(port, host).then(resolve, error => {
- setTimeout(() => {
- // Call this function again, with one less retry. However, we hook our
- // resolve and reject to the promise of the new call effectively making
- // a chain when it keeps failing.
- this.checkAppPort(port, retriesLeft - 1).then(resolve, reject);
- }, TIME_BETWEEN_CHECKS);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement