serverless-offline icon indicating copy to clipboard operation
serverless-offline copied to clipboard

Computer freezes after wake up with scheduled events

Open jer-sen opened this issue 3 years ago • 3 comments

Bug Report

Current Behavior

When the computer wakes up from a long standby, scheduled event are all fired at the same time executing a lot of lambdas, the computer freezes.

Sample Code

  • file: serverless.yml
service: my-service

plugins:
  - serverless-offline

provider:
  runtime: nodejs16.x
  stage: dev

functions:
  myLambda:
    handler: src/lambdas/myLambda.handler
    events:
      - schedule: rate(1 minutes)
  • file: handler.js

Not relevant

Expected behavior/code

Events triggered too late should not be fired (or at least it should be configurable, ideally per function with a "too late" time limit)

Environment

  • serverless version: v3.21.0
  • serverless-offline version: v8.8.81
  • node.js version: v16.15.0
  • OS: Windows 10

Possible Solution

Add these line here https://github.com/dherault/serverless-offline/blob/e907f894087db77f7aa86b40a40679a99c5c1965/src/events/schedule/Schedule.js#L47

      nodeSchedule.scheduleJob(cron, async (fireDate) => {
        const delay = Date.now() - fireDate.valueOf();
        if (delay > 5 * 60 * 1000) {
          log.error(
            `Scheduled function fired too late: [${functionKey}] ${delay}ms late`,
          )
          return;
        }

5 * 60 * 1000 should be replace by a configurable max delay

Additional context/Screenshots

None

jer-sen avatar Aug 05 '22 17:08 jer-sen

thank you for filing the issue @jer-sen

haven't tried to repro it yet, but would you mind putting up a PR with the suggested fix, possibly with some comments (I assume it's probably not easy to write a test)?

dnalborczyk avatar Aug 08 '22 00:08 dnalborczyk

@dnalborczyk sorry I realy don't have time for that. Also the best would be to make it configurable but it would take me a lot of time to dig into documentation and configuration system. The fix works well on my project. For the test, maybe you can extract scheduleJob callback and call it with new Date(Date.now() - 24 * 60 * 60 * 1000)) and new Date() to check that there is an error only in the first case.

jer-sen avatar Aug 08 '22 16:08 jer-sen

sorry I realy don't have time for that

that's understandable, no problem. it just might take a while that someone will get to it, that's all.

dnalborczyk avatar Aug 10 '22 16:08 dnalborczyk