medusa: Cron jobs randomly registered and never gets executed

I have testet with different setups on local machine and also asked in Discord help sections for anyone els with this issue. But without luck. So I will try to describe the issue here.

Bug report

Cron jobs not registered or running as expected

Cron jobs with both the new jobSchedulerService and the old eventBusService are often not registered when Medusa server starts. But common for them all is that none of the cron jobs get triggered to run. I have tried with both the "* * * * *" execute every minute or "0 0 * * *" every hour. But never see any job execution in the terminal log.

Also if you have more the on loader in the loader folder most of the time only one of them is registered.

(edit) Today i noticed that it only the last file in the loaders folder thats not being registred. If you add a new file lets say file1.js file2.js Then its only the first file that gets registred. If you add another one like file1.js file2.js file3.js Then file1 and 2 gets registred but not file3. etc.

Eks job

const myJob = async (container, options) => {
  const jobSchedulerService: JobSchedulerService = container.resolve(
    "jobSchedulerService"
  );

  jobSchedulerService.create(
    "my-console-job",
    {},
    "* * * * *",
    async () => {
      console.log("my-console-job executed");
    },
    { keepExisting: false }
  );
};

export default myJob;

System information

Medusa version (including plugins): 1.6.0 and up to 1.7.8 Node.js version: 18.14.2 lts and v19.4.0 Database: Both postgres 12 / 14 and Sqlite Operating system: mac / linux docker for redis and db Browser (if relevant):

Steps to reproduce the behavior

  1. npm install @medusajs/medusa-cli -g
  2. update deps to lates version or stay on current
  3. medusa new my-medusa-store --seed
  4. docker-compose up -d to start a medusa redis and backend (can remove medusa build from docker-compose)
  5. create a loader folder in src and create a new cron file as the example above.
  6. start medusa sever with yarn start

Screenshot

3 jobs image

2 registred image

Expected behavior

Expect to see all registered job in Medusa server logs when running and current jobs being executed

Additional context

I have also noticed that with medusa develop sometimes the files in src is not created before Medusa server start. So to be sure you have to run yarn / npm build first.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

So is there a way to manage all added cron jobs, or how can you see whats been added etc

Not a solution to your problem, but I recommend RedisInsight if you want a GUI for Redis. For some cases, I find it easier to use than the CLI.

Also when you test are you running redis in a docker container

Unfortunately, I don’t have experience running Redis in a container, so I can’t be of much help in this regard. I’ll play around with our JobScheduler early next week and try to reproduce your issue. Will get back to you as soon as possible.

When you have time can you share your tsc config for when it works for you

@adrien2p will you share your tsc config?

here is my ts config in my local project

{
  "compilerOptions": {
    "lib": [
      "es5",
      "es6",
      "es2019"
    ],
    "target": "es5",
    "outDir": "./dist",
    "esModuleInterop": true,
    "declaration": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "noImplicitReturns": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "allowJs": true,
    "skipLibCheck": true,
    "downlevelIteration": true,
    "rootDir": "src"
  },
  "include": [
    "./src",
    "src/index.d.ts"
  ],
  "exclude": [
    "./dist",
    "./src/**/__tests__",
    "./src/**/__mocks__",
    "node_modules"
  ]
}

@bentrynning, will you test the latest changes when time allows for it? I am closing the issue in the meantime. Feel free to reopen if your issues persist.

@olivermrbl there has been an update to the job scheduler service a few weeks back which introduced the issue. I’ve made a PR with the small fix: https://github.com/medusajs/medusa/pull/3335

hey, i also have this problem, but i fixed that by adding empty file-> z.js to the folder) #3056