Skip to content

Commit a398be0

Browse files
author
Victor Duarte da Silva
committed
Use the sls invoke local to add suport to python
Please note that with this method your function - must not 'print' anything becouse the output is used as http response. - will not receive relevant context becouse 'invoke local' not accept it as parameter
1 parent 4d38b5e commit a398be0

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/functionHelper.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ const debugLog = require('./debugLog');
55
function runPythonHandler(funOptions, options){
66
var spawn = require("child_process").spawn;
77
return function(event,context){
8-
var process = spawn('python',["run.py"],
9-
{stdio: ['pipe', 'pipe', 'pipe'], shell: true,cwd:funOptions.servicePath});
10-
process.stdin.write(JSON.stringify({event,context,options,funOptions})+"\n");
8+
var process = spawn('sls',["invoke", "local", "-f", funOptions.funName],
9+
{stdio: ['pipe', 'pipe', 'pipe'], shell: true, cwd:funOptions.servicePath});
10+
process.stdin.write(JSON.stringify(event)+"\n");
1111
process.stdin.end();
1212
let results = ''
1313
process.stdout.on('data', (data) => {
14-
console.log(`handler out: ${data}`);
1514
results = results + data;
16-
console.log(results)
17-
});
15+
});
1816
process.stderr.on('data', (data) => {
1917
context.fail(data);
2018
});
@@ -23,8 +21,7 @@ function runPythonHandler(funOptions, options){
2321
context.succeed( JSON.parse(results) );
2422
} else {
2523
context.succeed( code ,results);
26-
}
27-
24+
}
2825
});
2926
}
3027
}
@@ -63,7 +60,7 @@ module.exports = {
6360
}
6461
let user_python = true
6562
let handler = null;
66-
if (funOptions['serviceRuntime'] == 'python2.7'){
63+
if (['python2.7', 'python3.6'].indexOf(funOptions['serviceRuntime']) !== -1){
6764
handler = runPythonHandler(funOptions, options)
6865
} else {
6966
debugLog(`Loading handler... (${funOptions.handlerPath})`);

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class Offline {
303303
const apiKeys = this.service.provider.apiKeys;
304304
const protectedRoutes = [];
305305

306-
if (['nodejs', 'nodejs4.3', 'nodejs6.10', 'babel','python2.7'].indexOf(serviceRuntime) === -1) {
306+
if (['nodejs', 'nodejs4.3', 'nodejs6.10', 'babel', 'python2.7', 'python3.6'].indexOf(serviceRuntime) === -1) {
307307
this.printBlankLine();
308308
this.serverlessLog(`Warning: found unsupported runtime '${serviceRuntime}'`);
309309

0 commit comments

Comments
 (0)