Plugin rake task running multiple times - what are we missing?

Did you solve this?

I’m getting a similar issue:

If I create a rake take in a plugin, say: lib/tasks/test.rake

with contents

desc "count posts"
task "test_rake:test" => :environment do
  puts "Number of posts on this forum is #{Post.count}"
end

and run: rake test_rake:test

The output is surprising to me:

Number of posts on this forum is 366
Number of posts on this forum is 366
Number of posts on this forum is 366

I’m making some kind of error here, I’m sure, but not sure what?

Why is this running more than once and how to stop it?

I get a similar issue in Production which is, of course, more of a serious concern.