ec2-spot-notification
Advanced tools
Comparing version 2.0.3 to 2.1.0
/// <reference types="node" /> | ||
/// <reference types="request" /> | ||
/// <reference types="bluebird" /> | ||
import { EventEmitter } from "events"; | ||
@@ -10,6 +8,9 @@ import Promise = require("bluebird"); | ||
private beater; | ||
constructor(); | ||
static readonly requestOpts: CoreOptions; | ||
private shouldCheckInstanceStatus; | ||
constructor(_shouldCheckInstanceStatus?: boolean); | ||
static get requestOpts(): CoreOptions; | ||
protected heartbeat(): void; | ||
protected checkStatus(): Promise<any>; | ||
protected checkSpotStatus(): Promise<any>; | ||
protected checkInstanceStatus(): Promise<any>; | ||
instanceId(): Promise<any>; | ||
@@ -16,0 +17,0 @@ start(): void; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
@@ -13,2 +18,3 @@ function __() { this.constructor = d; } | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SpotNotifier = exports.SpotNotification = void 0; | ||
var events_1 = require("events"); | ||
@@ -20,6 +26,9 @@ var Promise = require("bluebird"); | ||
var timers_1 = require("timers"); | ||
var SpotNotification = (function (_super) { | ||
var SpotNotification = /** @class */ (function (_super) { | ||
__extends(SpotNotification, _super); | ||
function SpotNotification() { | ||
return _super.call(this) || this; | ||
function SpotNotification(_shouldCheckInstanceStatus) { | ||
if (_shouldCheckInstanceStatus === void 0) { _shouldCheckInstanceStatus = false; } | ||
var _this = _super.call(this) || this; | ||
_this.shouldCheckInstanceStatus = _shouldCheckInstanceStatus; | ||
return _this; | ||
} | ||
@@ -29,3 +38,3 @@ Object.defineProperty(SpotNotification, "requestOpts", { | ||
return { | ||
baseUrl: url_1.format({ | ||
baseUrl: (0, url_1.format)({ | ||
protocol: "http", | ||
@@ -39,3 +48,3 @@ slashes: true, | ||
}, | ||
enumerable: true, | ||
enumerable: false, | ||
configurable: true | ||
@@ -47,11 +56,15 @@ }); | ||
this.checkStatus() | ||
.then(function (terminationTime) { | ||
if (terminationTime.isValid() && terminationTime.isSameOrAfter(now)) { | ||
.then(function (_a) { | ||
var terminationTime = _a[0], instanceStatus = _a[1]; | ||
if (terminationTime && terminationTime.isValid() && terminationTime.isSameOrAfter(now)) { | ||
// Termination scheduled | ||
_this.emit("termination", terminationTime); | ||
} | ||
else { | ||
else if (terminationTime) { | ||
// Termination cancelled | ||
_this.emit("termination-cancelled", terminationTime); | ||
} | ||
if (instanceStatus === "Terminated") { | ||
_this.emit("instance-termination", instanceStatus); | ||
} | ||
}) | ||
@@ -64,2 +77,11 @@ .catch(function (error) { | ||
SpotNotification.prototype.checkStatus = function () { | ||
var jobs = [ | ||
this.checkSpotStatus() | ||
]; | ||
if (this.shouldCheckInstanceStatus) { | ||
jobs.push(this.checkInstanceStatus()); | ||
} | ||
return Promise.all(jobs); | ||
}; | ||
SpotNotification.prototype.checkSpotStatus = function () { | ||
return new Promise(function (resolve, reject) { | ||
@@ -71,2 +93,5 @@ request.get("spot/termination-time", SpotNotification.requestOpts, function (error, response, body) { | ||
} | ||
else if (response.statusCode === 404) { | ||
resolve(null); | ||
} | ||
else { | ||
@@ -78,2 +103,14 @@ reject(error || response.statusCode); | ||
}; | ||
SpotNotification.prototype.checkInstanceStatus = function () { | ||
return new Promise(function (resolve, reject) { | ||
request.get("autoscaling/target-lifecycle-state", SpotNotification.requestOpts, function (error, response, body) { | ||
if (!error && response.statusCode === 200) { | ||
resolve(String(body)); | ||
} | ||
else { | ||
reject(error || response.statusCode); | ||
} | ||
}); | ||
}); | ||
}; | ||
SpotNotification.prototype.instanceId = function () { | ||
@@ -99,3 +136,3 @@ return new Promise(function (resolve, reject) { | ||
if (this.beater) { | ||
timers_1.clearInterval(this.beater); | ||
(0, timers_1.clearInterval)(this.beater); | ||
} | ||
@@ -102,0 +139,0 @@ }; |
@@ -13,5 +13,7 @@ import {IncomingMessage} from "http"; | ||
private beater: any; | ||
private shouldCheckInstanceStatus: boolean; | ||
constructor() { | ||
constructor(_shouldCheckInstanceStatus:boolean = false) { | ||
super(); | ||
this.shouldCheckInstanceStatus = _shouldCheckInstanceStatus; | ||
} | ||
@@ -34,11 +36,13 @@ | ||
this.checkStatus() | ||
.then((terminationTime: Moment) => { | ||
if (terminationTime.isValid() && terminationTime.isSameOrAfter(now)) { | ||
.then(([terminationTime, instanceStatus]: [Moment, string]) => { | ||
if (terminationTime && terminationTime.isValid() && terminationTime.isSameOrAfter(now)) { | ||
// Termination scheduled | ||
this.emit("termination", terminationTime); | ||
} | ||
else { | ||
} else if (terminationTime) { | ||
// Termination cancelled | ||
this.emit("termination-cancelled", terminationTime); | ||
} | ||
if (instanceStatus === "Terminated") { | ||
this.emit("instance-termination", instanceStatus); | ||
} | ||
}) | ||
@@ -52,2 +56,12 @@ .catch((error: Error) => { | ||
protected checkStatus(): Promise<any> { | ||
const jobs = [ | ||
this.checkSpotStatus() | ||
]; | ||
if (this.shouldCheckInstanceStatus) { | ||
jobs.push(this.checkInstanceStatus()) | ||
} | ||
return Promise.all(jobs); | ||
} | ||
protected checkSpotStatus(): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
@@ -58,2 +72,4 @@ request.get("spot/termination-time", SpotNotification.requestOpts, (error: null|Error, response: IncomingMessage, body: string) => { | ||
resolve(moment(body)); | ||
} else if (response.statusCode === 404) { | ||
resolve(null); | ||
} | ||
@@ -67,2 +83,15 @@ else { | ||
protected checkInstanceStatus(): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
request.get("autoscaling/target-lifecycle-state", SpotNotification.requestOpts, (error: null|Error, response: IncomingMessage, body: string) => { | ||
if (!error && response.statusCode === 200) { | ||
resolve(String(body)); | ||
} | ||
else { | ||
reject(error || response.statusCode); | ||
} | ||
}); | ||
}); | ||
} | ||
public instanceId(): Promise<any> { | ||
@@ -69,0 +98,0 @@ return new Promise((resolve, reject) => { |
{ | ||
"name": "ec2-spot-notification", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "AWS EC2 Spot Instance Termination Notices", | ||
@@ -36,8 +36,9 @@ "main": "./lib/index.js", | ||
"@types/bluebird": "^3.0.37", | ||
"@types/node": "^7.0.5", | ||
"@types/request": "0.0.39" | ||
"@types/node": "18.6.1", | ||
"@types/request": "2.48.8", | ||
"tslint": "6.1.3", | ||
"typescript": "4.7.4" | ||
}, | ||
"dependencies": { | ||
"@types/bluebird": "^3.5.4", | ||
"@types/node": "^7.0.22", | ||
"bluebird": "^3.5.0", | ||
@@ -44,0 +45,0 @@ "moment": "^2.18.1", |
# EC2 Spot Instance Termination Notifier | ||
AWS EC2 Spot Instance Termination Notices for NodeJs | ||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Dependency Status][dependency-image]][dependency-url] | ||
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] | ||
@@ -25,2 +25,5 @@ ### Installation | ||
}); | ||
spot.on("instance-termination", error => { | ||
console.log("instance-termination", error); | ||
}); | ||
@@ -105,3 +108,3 @@ spot.instanceId() | ||
We welcome contributions of all kinds from anyone. | ||
* Author: [Tümay Çeber](https://github.com/brendtumi) [![](https://img.shields.io/gratipay/user/brendtumi.svg)](https://gratipay.com/brendtumi/) | ||
* Author: [Tümay Çeber](https://github.com/brendtumi) | ||
@@ -111,6 +114,4 @@ ### My boss wants a license. So where is it? | ||
[dependency-image]: https://david-dm.org/brendtumi/ec2-spot-notification.svg?style=flat-square | ||
[downloads-image]: http://img.shields.io/npm/dm/ec2-spot-notification.svg?style=flat-square | ||
[npm-image]: https://img.shields.io/npm/v/ec2-spot-notification.svg?style=flat-square | ||
[dependency-url]: https://david-dm.org/brendtumi/ec2-spot-notification | ||
[npm-url]: https://npmjs.org/package/ec2-spot-notification |
19206
4
406
114
5
10
- Removed@types/node@^7.0.22
- Removed@types/node@7.10.14(transitive)