Esa Mäkinen
Esa Mäkinen

Reputation: 160

Why Heroku worker has 100% dyno load?

I have this worker process in Heroku, which does some cleaning. It runs every two hours and listens to Heroku terminate signals. It works fine, but I'm seeing 100% dyno load all the time.

My question is: How to run this kind of worker process in Heroku without 100% dyno load? The loop causes the dyno load, but what to use instead of the infinite loop?

# Scheduler here
cleanup = Rufus::Scheduler.new
cleanup.cron '* */2 * * *' do
  do_some_cleaning
end

# Signal trapping
Signal.trap("TERM") {
  terminate = true
  shut_down
  exit 0
}

# Infinite loop

while terminate == false
end

Upvotes: 1

Views: 353

Answers (2)

jmettraux
jmettraux

Reputation: 3551

I should have thought about it sooner. You can actually simply join rufus-scheduler's loop:

cleanup_scheduler = Rufus::Scheduler.new
cleanup_scheduler.cron '* */2 * * *' do
  do_some_cleaning
end

Signal.trap('TERM') do
  shut_down
  exit 0
end

cleanup_scheduler.join

That joins rufus-scheduler scheduling thread and is pseudo equivalent to:

while !terminated
  sleep 0.3
  trigger_schedules_if_any
end

Upvotes: 1

rdegges
rdegges

Reputation: 33854

It's because you're doing an infinite loop with no sleep cycles. This means you're basically telling the CPU that every single cycle you should be immediately executing a loop condition.

This will quickly use up your CPU.

Instead, try throwing a sleep statement into your infinite loop -- this will pause execution and bring your usage down to 0% =)

while terminate == false
  sleep 1
end

Upvotes: 2

Related Questions