# Any support for loading rake tasks defined in plugins?

**URL:** https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479
**Category:** Development
**Created:** [February 24, 2020, 2:40pm UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479 "2020-02-24T14:40:07Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [February 24, 2020, 2:40pm UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479/1 "2020-02-24T14:40:07Z")

</div>

I need to be able to run custom tasks against a Discourse forum. Is there any way of loading rake tasks defined in a plugin so that these can be used in production?

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [February 24, 2020, 3:42pm UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479/2 "2020-02-24T15:42:49Z")

</div>

I think this should work. Just put them in the same directory in the plugin as where they live in core.

I’ve done this in the past with creating a rake task that runs after another but haven’t created a standalone task.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [March 16, 2020, 10:35am UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479/3 "2020-03-16T10:35:59Z")

</div>

Yep this works. It didn’t appear to work for me at first hence me asking the question, but I’ve tried this again and works first time.

I added my rake task in `lib/tasks` in my plugin. Thought I’d confirm this works for anyone else that stumbles upon this.

---

<div class="post-metadata">

### Author: ![kimardenmiller](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kimardenmiller/32/119631_2.png) [@kimardenmiller](https://meta.discourse.org/u/kimardenmiller)
#### Post date: [November 23, 2020, 8:16pm UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479/4 "2020-11-23T20:16:34Z")

</div>

Anyone experience `lib/tasks` rake tasks executing twice? Ours seems to execute twice from `rake subscriptions:procourse_convert`.

[https://github.com/ryan-hyer/discourse-subscriptions/blob/master/lib/tasks/procourse\_memberships.rake](https://github.com/ryan-hyer/discourse-subscriptions/blob/master/lib/tasks/procourse_memberships.rake)

---

<div class="post-metadata">

### Author: ![frank.manuel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/frank.manuel/32/203348_2.png) [@frank.manuel](https://meta.discourse.org/u/frank.manuel)
#### Post date: [February 15, 2021, 9:49pm UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479/5 "2021-02-15T21:49:42Z")

</div>

Hi @kimardenmiller , did you ever find an answer to why your rake task executes twice?

---

<div class="post-metadata">

### Author: ![kimardenmiller](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kimardenmiller/32/119631_2.png) [@kimardenmiller](https://meta.discourse.org/u/kimardenmiller)
#### Post date: [February 16, 2021, 1:03am UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479/6 "2021-02-16T01:03:47Z")

</div>

I think it might have had something to do with declaring the same method in two places, which I think caused it to run twice. Not verified, but check for that.

---

<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: [January 14, 2023, 11:36am UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479/7 "2023-01-14T11:36:41Z")

</div>

> [@kimardenmiller](#):
>
> I think it might have had something to do with declaring the same method in two places

EDIT: I think I worked out how this happens.

It’s not really following convention, or rather perhaps too much.

The issue is _ **if you reference the rake file in plugin.rb it seems to run twice** _, but the rake task is included even if you don’t reference it, so no need to load it during initialisation.

@frank.manuel

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [January 16, 2023, 10:27am UTC](https://meta.discourse.org/t/any-support-for-loading-rake-tasks-defined-in-plugins/142479/9 "2023-01-16T10:27:07Z")

</div>

> [@merefield](#):
>
> the rake task is included even if you don’t reference it

AFAIK there isn’t any discourse-specific logic here. My guess would be that the task files are autoloaded if you defined a `::Rails::Engine` for your plugin?
