ajmath / serverless-offline-scheduler

MIT License
96 stars 40 forks source link

Can not see cron running #12

Closed eladhaz05 closed 5 years ago

eladhaz05 commented 6 years ago

Hi,

I have a service with Cron event. I try to add the plugin to the service. When I run serverless offline I see the log Serverless: scheduler: scheduling ScheduleTasksExecute/ScheduleTasksExecute with 0/1 * ? But I do not see any logs from the function. How can I know that the function is running?

Thank you

MartinCerny-awin commented 6 years ago

@eladhaz05 You have to lunch the serverless offline after you have scheduled the cron event.

eladhaz05 commented 6 years ago

I add to serverless the scheduled event and still nothing happened example is serverless: plugins: ... serverless-offline-scheduler

TEST: handler: Router.route tags:

MartinCerny-awin commented 6 years ago

I have just found that there is not need to run scheduler manually using sls schedule or sls offline start Scheduler would be run automatically if you debug the script.

I am using vs code and I have setup the debuging using these two tutorials https://hackernoon.com/running-and-debugging-aws-lambda-functions-locally-with-the-serverless-framework-and-vs-code-a254e2011010 and https://code.visualstudio.com/docs/editor/debugging but my launch.json looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Serverless Offline",
            "program": "${workspaceRoot}/node_modules/serverless/bin/serverless",
            "args": [
                "offline",
                "start"
            ],
            "cwd": "${workspaceRoot}",
        }
    ]
}

The setup would look differently if you are using another IDE.

When I debug the debug console in VS Code outputs

Serverless: scheduler: scheduling cron/cron with */1 * * * *
Serverless: Starting Offline: dev/eu-west-2.
Serverless: Routes for cron:
Serverless: (none)
Serverless: Offline listening on http://localhost:3000

My serverless.yml

service: cron-fetch-earliest-timeslot-clicrdv

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: eu-west-2

plugins:
  - serverless-offline
  - serverless-offline-scheduler

functions:
  cron:
    handler: handler.run
    events:
      - schedule: rate(1 minute)
mik356ua commented 6 years ago

@eladhaz05 it looks like the problem is with how you define your cronjob schedule. This works for me (runs every minute):

    events:
      - schedule: cron(* * * * * *)

This runs every hour:

    events:
      - schedule: cron(0/1 * * * * *)
ajmath commented 5 years ago

@eladhaz05 I agree with @mik356ua. If you have an hourly crontab, it's not going to get executed until the next hour hits, so it might seem like nothing is happening for a while if you started the script shortly after the hour.

andidev commented 5 years ago

Well there is some issue here. I had problems running crons locally but I finally managed to do it (not sure what did the trick). But now a month later when I try again it does not work, it simply does not get triggered (or at least shows no logs).

- schedule: cron(0/1 * * * * *) should work by the way cause setting it to rate(1 minute) does exactly that according to logs when serverless start Serverless: scheduler: scheduling cron/cron with */1 * * * *. I think there is another issue here.

ovirta commented 5 years ago

Was also struggling to get debugging scheduled functions working with VS Code. This is the setup that did the trick on Windows machine. Very similar that @MartinCerny-awin described above, thanks!

serverless.yml:

plugins:
  - serverless-offline-scheduler
:
            - schedule:
                rate: rate(1 minute)

.vscode/launch.json:

        {
            "name": "Debug-project-schedule",
            "type": "node",
            "request": "launch",
            "cwd": "${workspaceRoot}/project-folder", 
            "windows": { "runtimeExecutable": "npm.cmd"},
            "runtimeArgs": [
                "run-script",
                "schedule",
            ],
            "port": 9229
        }

package.json:

  "scripts": {
    "schedule": "SET SLS_DEBUG=* && node --inspect-brk ./node_modules/serverless/bin/serverless offline start -s local",

Install serverless-offline and serverless-offline-scheduler normally.

npm install serverless-offline --save-dev
npm i --save-dev serverless-offline-scheduler

Choose and run "Debug-project-schedule" session and you should be able to break into both normal and scheduled functions. Hope this helps someone :).