callbackAWS API中的callback
有没有人知道一种方式,或有人devise了一个聪明的解决方法,把一个callback/挂钩到亚马逊API(http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/query-apis.html),使行动像创build实例,当实例处于运行状态时,可以简单地通过callback通知一个实例吗?
我在想,我可以在node.js中编写一个循环来简单地检查所需的状态,并在某些请求后最终超时,但是我希望听到更好的编程方法:)
除非AWS API支持某种通知端点(我对API不是很熟悉),否则您可能会停留在轮询状态。 但是,您可以使用EventEmitter
将其隐藏在公开callback的EventEmitter
API后面。 伪代码:
// aws_server.js var EventEmitter = require('events').EventEmitter; var util = require('util'); function AwsServer(some_data) { this.data = some_data; EventEmitter.call(this); }; util.inherits(AwsServer, EventEmitter); AwsServer.prototype.createInstance = function() { // Do something with an API to create an EC2 instance console.log("Creating instance, data:", this.data); // Here, you would begin polling for state changes, etc. waiting for // the server to change state. We will simulate this with a setTimeout call. setTimeout(function() { this.emit('running'); }.bind(this), 3000); }; module.exports = AwsServer; // somewhere_else.js var AwsServer = require('./aws_server') var newServer = new AwsServer('some_data'); newServer.on('running', function() { console.log('New instance is running'); }); newServer.createInstance();
最好的办法是在服务器init.d上添加一个shell脚本,该脚本将在服务器停止或启动时运行。
可能对这个问题没有用处,但通过使用Amazon的EC2 shell工具以编程方式检测实例是否是其他方式:
ec2-describe-instance-status <ec2 instance id>
如此处所述。 如果机器没有运行,它将返回空白,如果是则返回数据。
- 任何人在Amazon SNS和Apple推送通知中使用Node.js?
- 在Elastic Beanstalk上通过.ebextensions安装git
- 如何使用AWS IoT向/从Web浏览器发送/接收消息
- 如何在AWS Lambda中访问标题?
- Azure服务总线相当于AWS
- Amazon S3 – 编辑上传的图像
- AWS Elastic Beanstalk:我是否必须使用完整的“/ node_modules”和“/ bower_components”来部署我的应用程序,并编译sass和其他依赖项?
- Cognito开发人员使用节点validation身份
- phantomjs在访问aws-sdk时找不到节点模块“encryption”