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

**URL:** https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077
**Category:** Development
**Created:** [November 24, 2021, 5:33pm UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077 "2021-11-24T17:33:16Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)
#### Post date: [November 24, 2021, 5:33pm UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/1 "2021-11-24T17:33:16Z")

</div>

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:

```plaintext
# frozen_string_literal: true

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

```

With this result:

```plaintext
/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:

```plaintext
/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`.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [August 23, 2023, 10:46am UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/2 "2023-08-23T10:46:29Z")

</div>

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

```plaintext
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:

```plaintext
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.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [August 23, 2023, 10:58am UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/3 "2023-08-23T10:58:11Z")

</div>

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!) 😅

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

```

This just repeats the experience of the OP.

---

<div class="post-metadata">

### Author: ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)
#### Post date: [August 23, 2023, 12:20pm UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/4 "2023-08-23T12:20:16Z")

</div>

> [@merefield](#):
>
> Did you solve this?

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.

---

<div class="post-metadata">

### Author: ![manut](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/manut/32/485119_2.png) [@manut](https://meta.discourse.org/u/manut)
#### Post date: [February 24, 2025, 10:31am UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/5 "2025-02-24T10:31:38Z")

</div>

I may be a bit late to the party 🙂

I’m hitting the same issue in a new plugin, and searched a bit. It seems this issue hits other plugins too (e.g.: the official chat plugin), but I found a working solution to avoid the tasks being registered multiple times:

```rb
# Clear any previous registration
task('import_json:preview').clear 

# Task definition
desc 'Previews changes which would be applied by import_json'
task 'import_json:preview', [:file_path] => :environment do |_task, args|
  puts "Hello world!"
end

```

Clearing _after_ the task description will prevent the task to be listed with `rake --tasks`, so it must be cleared before.

---

<div class="post-metadata">

### Author: ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)
#### Post date: [February 26, 2025, 12:16pm UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/6 "2025-02-26T12:16:04Z")

</div>

> [@manut](#):
>
> It seems this issue hits other plugins too (e.g.: the official chat plugin)

Does this mean you get the same result adding a task to it or that an already existing task in the plugin has this behaviour?

Thanks for the information! It feels a little hacky to have to clear it but it’s certainly a useful workaround.

I’ll mark your post as the solution for others’ convenience. Hopefully the broader cause can still be identified, I guess the question becoming: why are tasks registered twice in some plugins and not others?

---

<div class="post-metadata">

### Author: ![manut](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/manut/32/485119_2.png) [@manut](https://meta.discourse.org/u/manut)
#### Post date: [February 26, 2025, 2:46pm UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/7 "2025-02-26T14:46:34Z")

</div>

> Does this mean you get the same result adding a task to it or that an already existing task in the plugin has this behaviour?

While trying to understand why the tasks I developped in my own plugin was executed twice, I tested an existing task in the Chat plugin and it executed twice too.

I tested some tasks from other plugins too but can’t remember which ones. Some where executing once but there was nothing obvious about the difference of implementation.

Maybe there is something to do with the autoloading?

On a fresh plugin, the engine contains this line:

```rb
config.autoload_paths << File.join(config.root, "lib")

```

If I don’t autoload `lib` but `lib/some_path` to avoid autoloading `lib/tasks`, it seems better, but the downside is that it breaks autoloading (I stopped here). Which may lead to have a bunch of `require_relative` statements to compensate…

---

<div class="post-metadata">

### Author: ![Simon\_Manning](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon_manning/32/198596_2.png) [@Simon\_Manning](https://meta.discourse.org/u/Simon_Manning)
#### Post date: [February 26, 2025, 6:34pm UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/9 "2025-02-26T18:34:55Z")

</div>

> [@manut](#):
>
> Maybe there is something to do with the autoloading?
> 
> On a fresh plugin, the engine contains this line:
> 
> ```plaintext
> config.autoload_paths << File.join(config.root, "lib")
> 
> ```

It certainly seems plausible that if there is already a mechanism for loading the tasks, this might cause them to be loaded a second time.

> [@manut](#):
>
> If I don’t autoload `lib` but `lib/some_path` to avoid autoloading `lib/tasks`, it seems better, but the downside is that it breaks autoloading (I stopped here).

I wonder if using either of the following would deal with this (I think I would lean towards the latter since I don’t know what side effects there could be to autoloading once):

```ruby
config.autoload_once_paths << File.join(config.root, "lib")

```

```ruby
config.autoload_lib(ignore: %w( tasks ))

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [March 28, 2025, 6:35pm UTC](https://meta.discourse.org/t/plugin-rake-task-running-multiple-times-what-are-we-missing/210077/10 "2025-03-28T18:35:38Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
