Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mountable engine with cron (whenever gem)

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

like image 245
pieterk Avatar asked Aug 25 '12 10:08

pieterk


1 Answers

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)
like image 171
mguymon Avatar answered Sep 21 '22 23:09

mguymon