rails admin for rake tasks - ruby-on-rails-3

I have a rails app with dozens of rake tasks defined for admin purpose for now, I want these rake tasks to be reflected on a kind of admin interface, so is there any plug-n-play solution for this or any quick ideas around?

You can get output of rake -T and then parse it with regex. i.e. :
all_tasks = %x(rake -T)
tasks_array = all_tasks.scan(/(rake\s[\w|:]*)/)
rake -T is quite long operation, so you may want to cache it

Related

How to expire cache part using rake task in rails3?

I am creating a application, rails3 with ruby 1.9.2. I have a left menu with some blog posts. This is displaying in every page in my application, so I am using cache concept in views. That blog posts update in database every day using rake task.
In rake task database update everyday first hour, after update database I want to clear that cache part from rake task. Any one help how to write rake task for expiring cache.
use a cronjob for your rake tast: https://github.com/javan/whenever
expire fragments with a sweeper: http://guides.rubyonrails.org/caching_with_rails.html#sweepers

How do I create a rake task for a Rails engine which is not exposed to the host application?

# lib/tasks/test.rake
task :hello do
puts 'hello'
end
$ rake app:hello
To run the task I need to prefix it with "app:" and it runs in the context of the dummy app. It is also exposed to the host application (i.e when used as a plugin in a parent Rails app) as rake hello.
I want to run a rake task which does not require a Rails environment and runs some command, but it is run from the engine root, not the dummy app root.
I know that's a bit late, but for others here searching for the correct answer, do the following :
Create your task :
# lib/tasks/your_engine_tasks.rake
desc "Explaining what the task does"
task :your_task do
# Task goes here
end
Then go to your engine ./Rakefile and add
load 'lib/tasks/your_engine_tasks.rake'
Here we go, now:
$ rake -T
gives you your task.
Hope I helped.
I want a better answer to this question however I did figure out that you can add tasks in the Rakefile for the engine (so ./Rakefile not in the spec/dummy application) like so:
task :my_task do
puts "hi!"
end
task :default => [:spec, :my_task]
I would prefer to have my task in another file but at least this provides a way to go forward. In my case, I want to run Konacha javascript tests in the dummy application so my Rakefile looks like this:
task :spec_javascript do
exec 'cd spec/dummy && rake konacha:run' # exec passes command return value up stack!
end
task :default => [:spec, :spec_javascript]

delayed job starts but does not process any jobs in production

I have a rails app using delayed job, it works fine in development.
However, when I pushed to production it doesn't do anything.
Delayed job starts up fine using either one of these commands:
RAILS_ENV=production rake jobs:work
or
RAILS_ENV=production script/delayed_job start
However, once the process has started it just sits there and does not process any of the jobs in the delayed_backend_mongoid table.
I checked the delayed_job.log and it shows that the process starts up fine but does not throw any errors.
And for the heck of it I checked the production.log and there are no errors there as well.
Also if I run
RAILS_ENV=production rake jobs:clear
It will delete the jobs that are in the que, so I don't think it has an issue communicating with mongodb.
I'm using
Rails 3.1.1
gem 'delayed_job'
gem 'delayed_job_mongoid'
Any insight will be greatly appreciated.
Ok so I finally got it to work, it wasn't related to delayed job at all. It had to do with the ruby gem Anemone. Anemone could not get proper authorization to the mongodb. Fixed, thanks anyways.

does Delayed_job daemon not run in development?

I'm using delayed_job and I am able to run jobs using rake jobs:work but using the daemonized version, it does nothing although I see it in the process list.
I'm using:
rails (3.0.9)
delayed_job (2.1.4)
daemons (1.0.10)
I'm running delayed_job using:
unix>RAILS_ENV=development script/delayed_job start
It could be a problem loading a custom job class file. To test that, try this:
Enter the rails console rails console --sandbox.
Make sure you have a job in the table job = Delayed::Job.first.
Try YAML.load(job.handler). If you get an error that looks like this: ArgumentError: undefined class/module MyCustomClass, it's probably a problem loading your custom job
Still in the rails console, run require 'My_Custom_Class. Then run the YAML.load(job.handler) command again. If this returns the appropriate object it's definitely a class loading problem.
To fix the problem create the file config/initializers/custom.rb and in it put require 'My_Custom_Class'.
You should then be able to run rake jobs::workoff and get something that looks like this:
[Worker(host:my.host pid:5085)] Starting job worker
[Worker(host:my.host pid:5085)] MyCustomJob completed after 0.0774
[Worker(host:my.host pid:5085)] 1 jobs processed at 9.1935 j/s, 0 failed ...
[Worker(host:my.host pid:5085)] No more jobs available. Exiting
To answer your question we may need more information.
Are jobs added to database? Are there any errors in jobs?
What's the result of RAILS_ENV=development script/delayed_job status as I already mentioned?
Second, did you went through the most common problems Wiki page?
https://github.com/collectiveidea/delayed_job/wiki/Common-problems

Automatically run gem tasks in test environment

I have a Rails 3 gem which has some rake tasks that should only be run in the test environment. Running in other environments doesn't really make sense.
My problem is Rake loads the Rails system in order to find my tasks in my gem. So by the time it gets to my tasks Rails is already loaded in the "development" environment (or whatever environment the user specified). This means in order to run my rake tasks properly the user must do:
RAILS_ENV=test rake mytask
Since my task only make sense in the "test" environment this is annoying as I would much rather the user be able to just type:
rake mytask
This is similar to how test:units and test:functionals automatically assume the test environment and the user doesn't need to specify RAILS_ENV=test at the command line. So the question is how do I modify my test so that Rails switches to the test environment?
My current workaround is:
Rails.env = 'test'
ActionMailer::Base.delivery_method = :test
require Rails.root.join('test/test_helper')
This seems to somewhat work but it is still logging to log/development.log and I think it is still actually running the "development" config. Anybody have any ideas? Looking at how the test tasks are defined in Rails itself doesn't reveal how to do it that I can see.
https://github.com/rails/rails/blob/master/railties/lib/rails/test_unit/testing.rake
UPDATE: I've updated my code after taking inputs from Eric's implementation at https://github.com/eric1234/test_inline/commit/fe3da7efa3a2cdb7824c23cfa41697b0ceb9e8e2.
For original code see - https://stackoverflow.com/posts/4600524/revisions
desc "Do something in Test environment"
task :example => :environment do
if not Rails.env.test?
Dir.chdir(Rails.root) do
system "rake example RAILS_ENV=test"
end
else
#.... stuff ....
end
end
I didn't check for the correctness of code, but you get the idea, right?