-
Notifications
You must be signed in to change notification settings - Fork 235
/
main.js
32 lines (31 loc) · 927 Bytes
/
main.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
/*global exports, require */
var aws = require('aws-sdk'),
got = require('got');
exports.handler = function (event, context) {
'use strict';
var SES = new aws.SES(),
emailConfig = function (err) {
return {
Source: event.email.from,
Destination: {
ToAddresses: [event.email.to]
},
Message: {
Subject: { Data: '[monitoring] ' + event.url},
Body: { Text: { Data: 'Problem: ' + JSON.stringify(err)} }
}
};
},
start = Date.now();
if (!event.email || !event.email.from || !event.email.to) {
return context.fail('email not set up');
}
if (!event.url) {
return context.fail('url not set up');
}
got(event.url, {retries: 0, timeout: (event.timeout || 2000)}).then(function () {
context.succeed({success: event.url, time: (Date.now() - start)});
}, function (e) {
SES.sendEmail(emailConfig({code: e.statusCode || e.code, message: e.statusMessage}), context.done);
});
};