Mountable engine with cron (whenever gem)
Asked Answered
P

2

8

Is it possible to use cron (via the whenever gem) to run tasks directly on mountable engine models. The cron I think would not be able to start from the main app since mountable engines are supposed to be isolated.

I am able to use whenever within a normal rails app and it works great but I have a need to run tasks in an engine style way.

Thanks

Phthalocyanine answered 25/8, 2012 at 10:10 Comment(0)
P
3

You cannot take a Rails Engine on its own and directly execute tasks, such as a Rake task or calling a Model. Think of a Rails Engine as a mini Rails App. It provides functionality and features to the mounting parent Rails App. On its own, the Engine is incomplete. With the Engine mounted to a Rails App, it has access to all the config and initializers required to start up and operate correctly.

The Rake tasks for a mounted Engine are inherited into the the parent Rails App. Executing the follow should include the list of Rake Tasks from the Engine

rake -T

From the Rails runner, you can execute the mounted Rails Engine's Models, libs, etc. as well. When accessing an Engine's features, you have to use the Engine's namespace. Here is an example from the Rails Guide for accessing a Model from an Engine:

Blorgh::Post.find(1)
Pomology answered 30/8, 2012 at 1:52 Comment(1)
I am still relatively new to rails engines and mountable engines so this is a learning curve. So from what you mention it clears a little up for me in showing me that I can run cron directly on Engine models but the execution would need to start from the host application. Being isolated I thought mountable engines were just that, black box but from what you mentioned I gave it a try and it works really well for me. I will mark this question correct as it has properly solved my rails engine problem and given me a little more insight into engines.Phthalocyanine
B
0

The designers of Rails definitely didn't intend it to be possible. Whenever relies on rails' runner script, which isn't supported for mountable plugins (for good reason--there's no application to run with runner). Maybe it's possible to find some other way to execute the code by configuring whenever to use a different ruby executer (rails console, IRB, etc.) but this would probably have unintended results since it's not in the context of an application, so you have no configuration or environment.

However, you state: The cron I think would not be able to start from the main app since mountable engines are supposed to be isolated, but I tested it and that's not the case. As long as you namespace the runner task appropriately, your application's whenever config file will locate the appropriate task in the plugin code and will be able to execute it. Please let me know if for some reason that won't work for your particular needs.

Basilisk answered 29/8, 2012 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.