具有更新的变量的文件,以及其他文件看到的更改
File with variable that gets updated, and other files see that change?
我正在尝试构建一个简单的工具,它ping一堆url来监控它们的状态,并根据每个应用程序的状态更新一个变量。
我还有另一个文件,我希望能够随时执行它,从该变量中获取每个应用程序的当前状态。
这是我的主文件,您可以看到有两个导出-start和getStatues。
index.js
'use strict';
const rest = require('restler');
const time = require('simple-time');
const seconds = time.SECOND;
// The list of apps to check if are running
var apps = {
myApp: {
url: 'http://myUrl.com',
status: null,
lastUpdatedAt: new Date()
}
};
/**
* Loop through and check the status of every app
*/
function checkAllStatuses() {
for (var name in apps) {
if (apps.hasOwnProperty(name)) {
var app = apps[name];
console.log('app = ', app);
checkAppStatus(name, app);
}
}
}
/**
* Checks the status of an app
*
* @param name - The name of the app
* @param app - The app that we're checking the status of
*/
function checkAppStatus(name, app) {
var req = rest.get(app.url);
req.on('complete', function(result, response) {
if(response.statusCode !== app.status) {
updateStatus(name, response.statusCode);
}
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});
req.on('timeout', function(data, response) {
console.log('Request timed out');
});
}
/**
* Updates the status of an app
*
* @param app - The app to update the status of
* @param status - The status to update the app to
*/
function updateStatus(name, status) {
apps[name].status = status;
apps[name].lastUpdatedAt = new Date();
}
function getStatuses() {
return apps;
}
function start() {
// Check every 5 seconds
setInterval(checkAllStatuses, 5*seconds);
}
module.exports.start = start;
module.exports.getStatuses = getStatuses;
然后我有一个文件,它开始了这个过程:
start.js
'use strict';
const status = require('./index');
status.start();
然后我有一个文件,我想执行以获得应用程序的当前状态:
consumer.js
'use strict';
const status = require('./index');
console.log(status.getStatuses());
问题是consumer.js只显示index.js中初始app
变量中的内容,即:
{
myApp: {
url: 'http://myUrl.com',
status: null,
lastUpdatedAt: new Date()
}
};
而运行CCD_ 2命令的进程正在显示非空的更新状态。
如何使consumer.js能够看到start.js正在更新的变量的值?
如果可能的话,我希望不必使用数据存储。最糟糕的情况是,我写入一个文件,运行redis、mongo或其他数据存储,但我试图避免这种情况,使这个应用程序尽可能简单。
在start.js
和consume.js
中使用相同的代码index.js
,但在运行每个文件时创建两个独立的实例。也就是说,apps
变量在start.js
创建的实例中发生变化,但consume.js
中没有任何内容告诉您的代码更改apps
变量。
如果您没有保存状态的历史记录,或者没有将数据保存到数据存储,那么启动例程有什么意义?您可以调用checkAllStatuses
,然后在希望使用数据时返回结果。
编辑以下是将两个文件(start.js
和consume.js
)组合为一个文件的示例。它还添加了一个示例socket.io实现,因为您已经说过,通过websocket向客户端提供状态是最终目标。
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
//// Your status library
var status = require('./index');
//// Start getting statuses
status.start();
app.listen(80);
//
// This is just the default handler
// in the socket.io example
//
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
// Someone wants the list of statuses
// This uses socket.io acknowledgements
// to return the data. You may prefer to use
// `socket.emit` instead or an altogether different socket library.
socket.on('status_fetch', function (data, callback_fn) {
callback_fn( status.getStatuses() );
});
});
- 生成pdf或其他非html文件时的错误处理
- 为其他JS生成定义“;编译器”;来自TypeScript定义文件
- 如何将JS文件从其他文件夹链接到页面
- 输入类型=文件验证停止其他输入类型验证
- 将不同上传的文件显示为缩略图或任何其他方式
- 使用metro-uiJS对话框调用其他文件html
- 节点:'最佳实践'使用其他js文件的js文件
- 使用CloudFront和AmazonS3,或者其他CDN,我可以将所有类似的请求发送到一个静态文件中
- 先在Angular中加载配置文件,然后再加载其他文件
- 如何忽略node.js中其他文件或组件所需的文件
- Firefox扩展开发:如何在内容脚本中创建一个全局函数,以便其他加载的脚本文件可以访问它
- Matrix3d 属性值及其他
- 将文件和其他一些字段上传到不同的服务器
- 包含多个文件和其他输入字段的表单验证
- JQuery Mobile中的JavaScript文件在其他页面上不起作用
- js中的$.get()方法获取文件的其他目录
- Javascript文件停止其他Javascript的工作
- 如何检查图像文件从其他网站是可见的
- jQuery gallery .js可以加载非图像文件类型,如.html文件或其他内容类型,如HTML5视频
- 如何将文件与其他数据一起发送到webmethod ?不使用任何插头