run rake task inside rails application

梦想的初衷 提交于 2019-12-06 13:53:42

You can just setup an environment variable for , and then issue #invoke method from a controller. So, prepare the files:

gem 'rake'

config/initializers/rake.rb:

Rake.load_rakefile Rails.root.join( 'Rakefile' )

app/controllers/your_controller:

ENV["Template"] = template.name
ENV["Theme"] = theme.name
Rake::Task[ 'assets:precompile' ].invoke

Issue bundle install, then run console rails c, and type:

Rake::Task.tasks.map(&:name).grep 'assets:precompile'
# => ["assets:precompile"]

As you can see, the task assets:precompile is loaded successfully. Then just issue the action for the controller.

To run the task for an other app you shell run also the other instance, similar to as you had done:

system( "other_app_run.sh '#{template.name}' '${theme.name}'" )

other_app_run.sh:

#!/bin/bash

source "$HOME/.rvm/scripts/rvm"
cd /other/app/path
export Template="$1"
export Theme="$2"
rake assets:precompile
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!