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

I’m adding a rake task to the docker_manager plugin but I’m finding that the task is running twice when I invoke it. In its simplest form, I can add testing.rake to lib/tasks with the following contents:

# frozen_string_literal: true

desc "A test"
task "docker_manager:test do
  puts "test"
end

With this result:

/var/www/discourse# rake docker_manager:test --trace
** Invoke docker_manager:test (first_time)
** Execute docker_manager:test
test
test

I’ve confirmed that this is not a general problem with tasks in plugins by adding the same rake file to the poll plugin with this result:

/var/www/discourse# mv plugins/docker_manager/lib/tasks/testing.rake plugins/poll/lib/tasks/
/var/www/discourse# rake docker_manager:test --trace
** Invoke docker_manager:test (first_time)
** Execute docker_manager:test
test

I haven’t yet been able to find anything obvious in the two plugins that might be preventing/causing this so I’m hoping someone might be able to offer suggestions for what could be preventing this in poll or causing it in docker_manager.

2 Likes

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.

1 Like

OK I managed to reduce the task to only running twice by removing the task from being loaded in the initialise block. Apparently it’s enough just to put it in /lib/tasks without having to load it.

That has brought me down to 1 repetition (now to a total of 2!) :sweat_smile:

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

This just repeats the experience of the OP.

2 Likes

Sadly not. Initially I had assumed it was something in core causing it to be loaded twice but seeing it only run once when added to the poll plugin seemed to refute that.

I’ve kind of forgotten now but I think using --trace might also be showing it only loading once. I might be wrong but if I recall correctly, the invoke and/or execute lines will appear twice if it’s loaded twice.

I failed to find any differences between docker_manager and poll or anything in core that would explain this difference in behaviour, and had basically given up when I made the post.

1 Like