-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
81 lines (65 loc) · 2.42 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
var http = require('http');
const util = require('./util.js');
//Lambda環境変数の読み込み
const IpAddress = process.env.IpAddress;
const RemoteWalletAddress = process.env.RemoteWalletAddress;
const URL = 'http://' + IpAddress + ':7890/account/status?address=' + RemoteWalletAddress;
const topicARN = process.env.topicARN;
const region = process.env.region;
var AWS = require('aws-sdk');
var sns = new AWS.SNS();
module.exports.checkHarvestStatus = (event, context, callback) => {
var promise = new Promise(function(resolve, reject) {
//リモートノードへアクセス、必要な情報を抽出
resolve(module.exports.checkRemoteStatus(URL));
}).then(function(result) {
//SNS用メール・サブジェクトを生成
result.subject = module.exports.createSubject();
return result;
}).then(function(result) {
console.log("result " + JSON.stringify(result));
console.log("topicARN " + topicARN);
if (result.message.status != "UNLOCKED") {
//UNLOCKED以外のステータスの場合、デリゲートハーベスティング(委任)が停止しているため通知
return util.publishToTopic(JSON.stringify(result.message), result.subject, topicARN);
} else {
console.log("Harvest status is UNLOCKED. (OK)");
return true;
}
}).then(function(result) {
callback(null, result);
}).catch(function(err) {
console.log("err# " + JSON.stringify(err));
callback(err);
});
};
module.exports.checkRemoteStatus = (URL) => {
var message = {};
return new Promise(function(resolve, reject) {
console.log("URL:" + URL);
//リモートステータスの確認
http.get(URL, function(res) {
message.statusCode = res.statusCode;
res.setEncoding('utf8');
res.on('data', function(str) {
console.log(str);
message.message = JSON.parse(str);
});
resolve(message);
});
});
};
module.exports.publishToTopic = (sns, message, subject, targetArn) => {
var params = {
Message: message,
Subject: subject,
TargetArn: targetArn
};
return sns.publish(params).promise();
};
//subject作成
module.exports.createSubject = () => {
var subject = "[XEM][info] Please Check Harvest Status";
return subject;
}