2

I use the heroku scheduler add-on to do a daily database scan and it works like a charm, yay

But it never kills itself. When I run heroku ps -a mysuperapp I can see that the dyno is still up even though its job has been completed a few hours ago.

That is not too surprising since nothing in my code tells the dyno "I'm done". But I don't see any doc explaining how to say "I'm done".

This one answer suggests that

After finishing the work, dyno is killed

automagically, but that doesn't seem to happen in my case.


TLDR;

How can I notify the one-off dyno created by heroku scheduler that its job is done and it can kill itself?

Félix Paradis
  • 5,165
  • 6
  • 40
  • 49
  • What command is your one-off dyno running? Does it exit, ideally with a 0 status? – ChrisGPT was on strike Sep 27 '18 at 11:14
  • Hey Chris, the command I use to get the dyno running is `$ node workers/dbscan` (at the root of the project there is a `workers` folder, with a `dbscan.js` file inside) It does not exit with any status as far as I know. – Félix Paradis Sep 27 '18 at 15:07
  • If you run `node workers/dbscan` on your local machine, does the command finish, or does it run forvever? (If it exits it exits with an exit code.) – ChrisGPT was on strike Sep 27 '18 at 15:13
  • Well it does seem to exit in the sense that after the expected console.log()s are printed the terminal is ready for a new command. But I don't see no exit code anywhere. I tried adding `process.exit()` in order to see an exit code, but I must be looking at the wrong place... should it be printed in the terminal? – Félix Paradis Sep 27 '18 at 17:30
  • Exit codes aren't printed, but after the command finishes the code may be stored in a variable. For example, in bash you can run `echo $?` right after the command finishes to see the exit code. It sounds like the command _isn't_ finishing on Heroku. What does it do? Is there anything that might take a long time, go into an infinite loop, etc.? What happens if you run it interactively on Heroku, e.g. via `heroku run node workers/dbscan`? – ChrisGPT was on strike Sep 27 '18 at 17:55
  • Wow! Ok, not there yet, but you're being very helpful. The `echo $?` works great, it does end with exit code 0 locally. However, when running it on heroku, it just sort of hangs there (even after adding `process.exit()` in the final `.then()`) The db scan does take a long time in production (around 30 minutes), but it does not seem to get stuck in an infinite loop since it gets to the final `.then()`(and there is a `.catch()`) – Félix Paradis Sep 27 '18 at 18:39
  • Maybe something as small as the database connection remaining active keeps the dyno alive? I do use a cursor, so maybe that needs to be closed. I'll look into those. – Félix Paradis Sep 27 '18 at 18:48
  • It could be. The one-off dyno is still running because the command didn't finish. If we can figure out why the command isn't finishing we should be able to solve your problem. – ChrisGPT was on strike Sep 27 '18 at 20:21
  • So it was the database connection! Thanks for your insights Chris. – Félix Paradis Sep 28 '18 at 16:40

1 Answers1

1

If a one-off dyno never kills itself it's most probably because something is still active or never finishes or waits for a response.

In my case it was the database connection, adding mongoose.disconnect() solved the problem.



(Thanks to @Chris for getting me on the right track.)

Félix Paradis
  • 5,165
  • 6
  • 40
  • 49