serverless / serverless-local-schedule

⚡️🗺️⏰ Schedule AWS CloudWatch Event based invocations in local time(with DST support!)
MIT License
72 stars 11 forks source link

TypeError: schedule.rate.match is not a function #20

Closed gabrielecuccu closed 2 years ago

gabrielecuccu commented 3 years ago

Hi, this error started today on my nightly deploy task:

Serverless: Converting local crontabs to UTC crontabs...

 Type Error ----------------------------------------------

  TypeError: schedule.rate.match is not a function
      at ServerlessLocalCrontabs.convertCrontabs (/root/project/build/node_modules/serverless-local-schedule/index.js:73:37)
      at PluginManager.invoke (/root/project/build/node_modules/serverless/lib/classes/PluginManager.js:580:20)
      at async PluginManager.spawn (/root/project/build/node_modules/serverless/lib/classes/PluginManager.js:602:5)
      at async Object.before:deploy:deploy [as hook] (/root/project/build/node_modules/serverless/lib/plugins/deploy.js:61:11)
      at async PluginManager.invoke (/root/project/build/node_modules/serverless/lib/classes/PluginManager.js:580:9)
      at async PluginManager.run (/root/project/build/node_modules/serverless/lib/classes/PluginManager.js:640:7)
      at async Serverless.run (/root/project/build/node_modules/serverless/lib/Serverless.js:457:5)
      at async /root/project/build/node_modules/serverless/scripts/serverless.js:803:9

I believe it relates to this bit of configuration in serverless.yml:

  subscriptions-f:
    environment: ${file(.env.json)}
    handler: dist/subscriptions-f-handler.handler
    timeout: 900
    events:
      - sns:
          arn: ${file(.env.json):USER_CHAIN_TOPIC}
      - schedule:
          rate: cron(0 * * * ? *)
          timezone: Europe/London
          input:
            topic: 'main'

I was able to deploy the same code to UAT and to Production 2 days ago - this issue started on nightly provisioning only.

I don't know if it matters, I have been using serverless-local-schedule for about 1 year but with frameworkVersion < 2. Now I'm using frameworkVersion: "2"

Any idea? Thank you!

gabrielecuccu commented 3 years ago

this happens with the latest serverless 2.59.0, it works with 2.54.0

jp928 commented 3 years ago
ejoncas-rivalbet commented 3 years ago

Same problem here. I re-installed with sudo npm i -g serverless@2.54.0 and then re-deploy and it works.

gabrielecuccu commented 3 years ago

I can't create a PR because I don't have write access to this repo, but this seems to solve the problem with latest serverless:

diff --git a/index.js b/index.js
index 41dbca7..b628203 100644
--- a/index.js
+++ b/index.js
@@ -70,7 +70,8 @@ function convertCrontabs() {
         event.schedule.hasOwnProperty("timezone")
       ) {
         const schedule = event.schedule;
-        const match = schedule.rate.match(/^cron\((.*)\)$/);
+        const rate = Array.isArray(schedule.rate) ? schedule.rate[0] : schedule.rate;
+        const match = rate.match(/^cron\((.*)\)$/);
         if (!match)
           // skip rate() schedules
           continue;

Basically after serverless 2.48 scheduler.rate is an array, so I'm wondering if using the first element of the array would be correct. This solves the problem for me. I'm facing other problems though, but it is probably another story.

ejoncas-rivalbet commented 3 years ago

@gabrielecuccu i created a fork and applied that patch: https://github.com/UnitedIncome/serverless-local-schedule/pull/21

Let's see if someone can approve/merge. Thanks for the fix.

jp928 commented 3 years ago

@ejoncas-rivalbet I found this approach has bug. it breaks my scheduled lambda into huge amount of sub resources.

ramblingenzyme commented 2 years ago

@pgrzesik I've opened another PR that should close out this issue, could you give it a look when you can?

pgrzesik commented 2 years ago

Thanks @ramblingenzyme - I was unavailable for a few days but I've reviewed your proposal, thanks for patience