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?
AI-generated summary
The discussion revolves around loading rake tasks defined in Discourse plugins. jamesmarkcook inquired about running custom tasks against a Discourse forum, and justin suggested placing them in the lib/tasks directory in the plugin. jamesmarkcook confirmed this approach works.
However, kimardenmiller encountered an issue where their rake task executed twice. kimardenmiller suspected it was due to declaring the same method in two places. merefield later discovered that referencing the rake file in plugin.rb causes the task to run twice. They noted that the rake task is included even without referencing it, making it unnecessary to load during initialization.
david suggested that the task files are autoloaded if a ::Rails::Engine is defined for the plugin, but noted that this is not specific to Discourse.
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.
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.
Anyone experience lib/tasks rake tasks executing twice? Ours seems to execute twice from rake subscriptions:procourse_convert.
Hi @kimardenmiller , did you ever find an answer to why your rake task executes twice?
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.
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.
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?