aws-lambda-runner 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/js/request.js +78 -38
  3. data/lib/lambda_runner.rb +1 -0
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c6974bc3e8c91e3625933ccb2e4937868ce9b6db
4
- data.tar.gz: 36f2002e3ee8d0d5aa57b3eddb70ce1e05f81158
3
+ metadata.gz: f00a420d05400ba0db54fe75fa8ec2feff66c821
4
+ data.tar.gz: 62fbcc48883724ea62d69687321cff22bd8b22f0
5
5
  SHA512:
6
- metadata.gz: 94fc927d30b90795b845934f1f8f49031c694b227a68f9b12032cd4812e72871e1234823a89159fd8b3b0fe7afd7be36472ebb7e8034d1df71e92d06e59827aa
7
- data.tar.gz: 1d852324b869731f44f2192ab9d42151ce31f1766ef5e9d27ae85b3c364805c86641e3c7f77acc0cc7a01d7d43ffdf3a31d8ef8ae310de3849343b0a69fbc326
6
+ metadata.gz: 2b89d4d941ccbf45904fae5a7cd5d06ee9bebf42ec975f4ae61fd887efe5eddb7995ac4cc3f53a1304f76aba2cea48770e6f08d929d10a1ec846aed21ad42923
7
+ data.tar.gz: b80855bc773be900831f97c0fae15c6b3055c4b901850e88fccfce46cec7d6dff13bba4701c5943b86607095d93c5d9056a424d28e5db48877b7e6dd6246a5a3
@@ -1,67 +1,107 @@
1
-
2
1
  var url = require('url');
3
2
 
4
3
  var next_id = 0;
5
- var done = {};
6
- var errors = {};
7
- var successes = {};
8
- var timeout = {};
4
+ // results[id] = { threw: error, completed: [ errorValue, successValue ], timedOut: bool };
5
+ var results = {};
9
6
 
10
7
  exports.request = function(req, res, opts, handler) {
8
+
11
9
  if (req.method === 'POST') {
12
- var id = next_id;
13
- var postOutput = '';
14
- next_id += 1;
10
+
11
+ var id = ++next_id;
12
+ results[id] = {};
13
+
14
+ var requestBody = '';
15
+
15
16
  req.on('data', function(chunk) {
17
+ requestBody += chunk.toString();
18
+ });
19
+
20
+ req.on('end', function(chunk) {
16
21
  setTimeout(function() {
17
- if (!done[id]) {
18
- timeout[id] = true;
22
+ if (!results[id].completed) {
23
+ results[id].timedOut = true;
19
24
  }
20
25
  }, opts.timeout);
21
- postOutput += chunk.toString();
22
- });
23
- req.on('end', function(chunk) {
24
- handler(JSON.parse(postOutput), {
25
- done: function(err, message) {
26
- if (err) {
27
- errors[id] = err;
28
- console.warn('Error:', err);
29
- } else {
30
- successes[id] = message;
26
+
27
+ var requestObject;
28
+ try {
29
+ requestObject = JSON.parse(requestBody);
30
+ } catch (e) {
31
+ console.log("POSTed bad json: " + e.toString());
32
+ res.writeHead(400, {'Content-Type': 'text/plain'});
33
+ res.end(e.toString());
34
+ return;
35
+ }
36
+
37
+ res.writeHead(200, {'Content-Type': 'text/plain'});
38
+ res.end(String(id));
39
+
40
+ try {
41
+ handler(requestObject, {
42
+ done: function(err, message) {
43
+ results[id].completed = [ err, message ];
44
+ if (err) {
45
+ console.warn('Error:', err);
46
+ }
47
+ console.info('Finished:', message);
31
48
  }
32
- console.info('Finished:', message);
33
- done[id] = true;
34
- }
35
- });
49
+ });
50
+ } catch (e) {
51
+ console.log("Handler crashed", e);
52
+ results[id].threw = e;
53
+ }
54
+
36
55
  });
37
- res.writeHead(200, {'Content-Type': 'text/plain'});
38
- res.end(String(id));
56
+
39
57
  } else if (req.method === 'DELETE') {
58
+
40
59
  // FIXME untested
41
60
  res.writeHead(202, {'Content-Type': 'text/plain'});
42
61
  var terminationMessage = 'Terminating server at http://[localhost]:' + opts.port + ' for ' + opts['module-path'] + ' / ' + opts.handler;
43
62
  res.end(terminationMessage + '\n');
44
63
  console.info(terminationMessage);
45
64
  server.close();
65
+
46
66
  } else if (req.method === 'GET') {
67
+
47
68
  var request_id = parseInt(url.parse(req.url, true).query.id);
48
- var status = 200;
49
- var getOutput = null;
50
-
51
- if (successes[request_id]) {
52
- status = 201;
53
- getOutput = successes[request_id];
54
- } else if (errors[request_id]) {
55
- status = 502;
56
- getOutput = errors[request_id];
57
- } else if (timeout[request_id]) {
58
- status = 504;
69
+ var result = results[request_id];
70
+
71
+ var status = null;
72
+ var responseBody = null;
73
+
74
+ if (result === undefined) {
75
+ status = 404;
76
+ } else {
77
+ if (result.completed) {
78
+ if (result.completed[0] !== null) {
79
+ status = 502;
80
+ responseBody = result.completed[0];
81
+ } else {
82
+ status = 201;
83
+ responseBody = result.completed[1];
84
+ }
85
+ } else if (result.threw) {
86
+ status = 500;
87
+ responseBody = result.threw.toString();
88
+ } else if (result.timedOut) {
89
+ status = 504;
90
+ } else {
91
+ // still in progress
92
+ status = 200;
93
+ }
59
94
  }
60
95
 
61
96
  res.writeHead(status, {'Content-Type': 'application/json'});
62
- res.end(JSON.stringify(getOutput) + '\n');
97
+ res.end(JSON.stringify(responseBody) + '\n');
98
+ console.log(request_id, status, result);
99
+
63
100
  } else {
101
+
64
102
  res.writeHead(500, {'Content-Type': 'text/plain'});
65
103
  res.end('Not implemented: ' + req.method + '\n');
104
+
66
105
  }
106
+
67
107
  };
@@ -59,6 +59,7 @@ module LambdaRunner
59
59
  case response.code
60
60
  when 200 then sleep(0.1)
61
61
  when 201 then return data
62
+ when 500 then fail data
62
63
  when 502 then fail data
63
64
  when 504 then fail 'timeout'
64
65
  else fail "unknown response #{response.code}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-lambda-runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - andrew wheat