kibertoad / toad-scheduler

In-memory Node.js and browser job scheduler
MIT License
564 stars 25 forks source link

CronJob on aws ec2 #190

Closed itsManeka closed 1 year ago

itsManeka commented 1 year ago

Is there any particularity when running a Cron Job in an AWS instance? I ask because my Cron Job works locally, but it doesn't seem to run on my aws instance.

I identified that it is not running through logs, because there is no error either.

Note: It's a more lay question, because I don't know where to start looking for the problem. I googled a few things, but nothing I could identify as a solution or explanation.

A bit of code to show you how I'm doing it.

require("dotenv").config();

const {CronJob, ToadScheduler, SimpleIntervalJob, Task} = require('toad-scheduler');

module.exports={
    createNormalJob(name, callback, conf) {
        const task = new Task(name, callback);
        const job = new SimpleIntervalJob(conf, task);
        return job;
    },

    createCronJob(name, callback, conf) {
        const task = new Task(name, callback);
        const job = new CronJob(conf, task, {preventOverrun: true});
        return job;
    },

    createJobs() {
        const scheduler = new ToadScheduler()
        scheduler.addCronJob(this.createCronJob('sometext', someCallbackFunction, {cronExpression: '0 15 0,12 * * ?'}));
    }
}

Note: Simple Interval Jobs works fine in aws and locally, the problem is about Cron Jobs.

kibertoad commented 1 year ago

@itsManeka What's AWS in this context, EC2, lambdas, something else?

@Hexagon Any thoughts on what may cause croner to misbehave in cloud?

Hexagon commented 1 year ago

@itsManeka As a first test, can you try using Croner barebones, something like

import { Cron } from "croner";

new Cron("*/5 * * * * *", () => {
    console.log('This will run every fifth second');
});

?

Croner use setTimeout internally, so if simple interval jobs work, cron-jobs should work 😅

Btw, ? has a special meaning in croner/toad scheduler, it is replaced by current value. Just use * for wildcards, even in dayofmonth and dayofweek

And another question, is it one of these aws lambda thingies? https://ed4becky.org/clearing-up-settimeout-misconceptions-in-aws-lambdas/

itsManeka commented 1 year ago

@itsManeka What's AWS in this context, EC2, lambdas, something else?

@Hexagon Any thoughts on what may cause croner to misbehave in cloud?

I wrote a program in node.js, create an ec2 instance in aws and I'm running this program inside that instance. Just an app running on a server.

itsManeka commented 1 year ago

@itsManeka As a first test, can you try using Croner barebones, something like

import { Cron } from "croner";

new Cron("*/5 * * * * *", () => {
  console.log('This will run every fifth second');
});

?

Croner use setTimeout internally, so if simple interval jobs work, cron-jobs should work 😅

Btw, ? has a special meaning in croner/toad scheduler, it is replaced by current value. Just use * for wildcards, even in dayofmonth and dayofweek

And another question, is it one of these aws lambda thingies? https://ed4becky.org/clearing-up-settimeout-misconceptions-in-aws-lambdas/

@Hexagon thanks for the tips. I'll try to use the log tip in the app later and as soon as I find something I'll come back here to let you know what happened.

Over the ?, I used this site to help me generate the expression. I had the following results:

Using ?: image

Using *: image

My idea is to run the task every day at 12:15 and 00:15.

I don't know if this could be a bad implementation, but what I can say is that locally it's working. The problem is only occurring on the server.

About aws, it's nothing related to lambda, I'm just using an ec2 instance as a server for my app.

Hexagon commented 1 year ago

@Hexagon thanks for the tips. I'll try to use the log tip in the app later and as soon as I find something I'll come back here to let you know what happened.

Great!

Over the ?, I used this site to help me generate the expression. I had the following results:

Yeah, ? is a non-standard character that can mean different things in different implementation. See non-standard characters section of https://en.wikipedia.org/wiki/Cron

Croner uses the latter implementation.

My idea is to run the task every day at 12:15 and 00:15.

Yeah, 0 15 0,12 * * * would do that... But oh, time zone problems maybe? Do your AWS server run a different time zone from local?

@kibertoad, is it possible to pass time zone through toad-scheduler?

itsManeka commented 1 year ago

Just bringing news: The problem was solved, I updated the packages on the server and applied the correction in the cron expression as per your tips.

After that it started running ok. I apologize for the inconvenience. This conversation, however, was very useful due to the tips on the cron expression.

About the time zone, I use a server in a region that uses the same time zone as mine, so I believe it won't be a problem.

Hexagon commented 1 year ago

Good to hear! 🎉