Any support for loading rake tasks defined in plugins?

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?

1 个赞

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.

4 个赞

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.

4 个赞

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

2 个赞

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

1 个赞

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.

1 个赞

编辑:我认为我已经弄清楚这是如何发生的。

这并不真正遵循约定,或者说也许是太遵循了。

问题在于如果您在 plugin.rb 中引用 rake 文件,它似乎会运行两次,但即使您不引用它,rake 任务也会被包含,因此无需在初始化期间加载它。

@frank.manuel

1 个赞

据我所知,这里没有特定于 discourse 的逻辑。我猜想,如果你为你的插件定义了一个 ::Rails::Engine,那么任务文件就会被自动加载?

1 个赞