0

I am in macos and scheduled a node script as a cronjob. I am saving the logs of the cronjob to a file. When the script is executed from shell manually with same command I've in cronjob, it executes successfully but it gives error from cronjob.

The script has used snoowrap to request to get top posts from the reddit. I think it's where the error comes from. The error is:

RangeError: Maximum call stack size exceeded
    at /Users/kiro/Documents/learns/myproj/node_modules/snoowrap/dist/objects/Listing.js:231:21
    at tryCatcher (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/promise.js:729:18)
    at _drainQueueStep (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/kiro/Documents/learns/myproj/node_modules/bluebird/js/release/async.js:15:14)
    at processImmediate (node:internal/timers:466:21)
node:internal/process/promises:265
            triggerUncaughtException(err, true /* fromPromise */);

The command i'm using in crontab is

~/Documents/learns/myproj/job.sh >> ~/Documents/learns/myproj/cron.log 2>&1where job.sh has

~/.nvm/versions/node/v16.14.0/bin/node ~/Documents/learns/myproj/build/index

This is the part of code that gives error

await this.redditClient
        .getSubreddit(subreddit)
        .getTop({ time: "day", limit: 400, count: 400 });

I also tried by passing --max-stack-size 32000 but still gives same error.

What can be the reason for this and how can I solve it?

Bhuwan Adhikari
  • 879
  • 1
  • 9
  • 21

1 Answers1

0

difficult to say without seeing your code. My best guess is you're firing too many functions without waiting for the previous function to finish. Try to make your code run synchronously or even better see Call async/await functions in parallel

Guy Hagemans
  • 496
  • 1
  • 4
  • 15
  • but why does it happen when only executed as cronjob? does executing from shell and cronjob have differences? [updated the question too with part of code that's giving the error] – Bhuwan Adhikari Oct 15 '22 at 12:58
  • no idea but you're probably calling this function too many times, there are too many subreddits. What should work is putting this function in a synchronous for loop so you're processing one subreddit at a time. – Guy Hagemans Oct 16 '22 at 09:48