ajmath / serverless-offline-scheduler

MIT License
96 stars 40 forks source link

http request to localhost is hanging #42

Open maxdevp opened 4 years ago

maxdevp commented 4 years ago

Trying to send websocket messages to connected clients by making a call to http://localhost:3001/@connections/{connectionId} however all http calls to localhost:3000 or localhost:3001 are hanging. Works if the lambda function is invoked directly, without serverless-offline-scheduler.

pardini-net7 commented 4 years ago

I'm facing the same problem with s3 offline (but also with a bucket on a real s3). It hangs on the upload of a file after the cron is triggered. I think that this plugin doesn't work with http requests... could you help?

maxdevp commented 4 years ago

Noticed it only happens if scheduler is started with "sls offline". Requests work fine if started with sls schedule

ajmath commented 4 years ago

I don't really know what effect this plugin would have on http requests. At it's core, it's just a node process that is scheduling a call to process.execFile that's equivalent to sls invoke local --function functionName

On Tue, Jan 14, 2020 at 12:48 PM Max Gedikli notifications@github.com wrote:

Noticed it only happens if scheduler is started with "sls offline". Requests work fine if started with sls schedule

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ajmath/serverless-offline-scheduler/issues/42?email_source=notifications&email_token=AACBDNYDLMQMJ6VZ6HT6XL3Q5X3FDA5CNFSM4KFYVSIKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEI5QMGY#issuecomment-574293531, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACBDN46OMNDDOKYJJGCKRDQ5X3FDANCNFSM4KFYVSIA .

maxdevp commented 4 years ago

Not familiar with the code base.. wondering if it could be related to the order of scheduler and serverless start, http calls to outside domains work, it only hangs if we're calling lambda function (by making a request to localhost) within the scheduler that was started with sls offline start

ajmath commented 4 years ago

That could be it. If the scheduled lambda is fired at the same time as the offline http server, it's possible the server isn't listening when the scheduled lambda is invoked. If that was the case though, it should definitely work the next time the lambda is invoked.

On Tue, Jan 14, 2020 at 2:09 PM Max Gedikli notifications@github.com wrote:

Not familiar with the code base.. wondering if it could be related to the order of scheduler and serverless start, http calls to outside domains work, it only hangs if we're calling lambda function (by making a request to localhost) within the scheduler that was started with sls offline start

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ajmath/serverless-offline-scheduler/issues/42?email_source=notifications&email_token=AACBDN55E2BKIKN5BELFX7DQ5YEU3A5CNFSM4KFYVSIKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEI5YIZI#issuecomment-574325861, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACBDN5VHJZNTY63P456UPDQ5YEU3ANCNFSM4KFYVSIA .

ten-alk commented 4 years ago

@maxgedikli There is a workaround that helped me with a similar issue - sending HTTP requests to localhost in scheduled Lambda functions:

diff --git a/node_modules/serverless-offline-scheduler/lib/scheduler.js b/node_modules/serverless-offline-scheduler/lib/scheduler.js
index a03283e..96d2695 100644
--- a/node_modules/serverless-offline-scheduler/lib/scheduler.js
+++ b/node_modules/serverless-offline-scheduler/lib/scheduler.js
@@ -85,7 +85,7 @@ class Scheduler {
         args.push(`--${key}`, `${slsOption[key]}`);
       }
     }
-    return childProcess.execFileSync(process.argv[0], args, { cwd: "./", stdio: "inherit" });
+    return childProcess.spawn(process.argv[0], args, { cwd: "./", stdio: "inherit" });
   }

   _setEnvironmentVars(functionName) {

Not sure if if's the right approach or not - I don't have a capacity atm to debug further, but it works for my use cases.

rokso commented 4 years ago

I am having same issue and here is my observation. I have 2 lambda functions, one is just cron (no HTTP) and other accepts HTTP requests.

jkruse14 commented 4 years ago

The issue is with execFileSync, here's the documentation:

The child_process.execFileSync() method is generally identical to child_process.execFile() with the exception that the method will not return until the child process has fully closed. When a timeout has been encountered and killSignal is sent, the method won't return until the process has completely exited.

Note: the method will not return until the child process has fully closed.

If you do not close network connections, i.e. redis, mysql, the process will never return. If you set the lambda context callbackWaitsForEmptyEventLoop property to false, your lambda will return right when the handler/controller returns on AWS and not be concerned with those pesky connections (which will be reused). However, that is not taken into account with the plugin.

I have a potential fix and will open a PR.

jkruse14 commented 4 years ago

see #46

olegario96 commented 4 years ago

@rokso same issue here, but instead HTTP I'm using Kinesis events.