How can I run a rake task from a delayed_job - ruby-on-rails

I'd like to run a rake task (apn:notifications:deliver from the apn_on_rails gem) from a delayed_job. In other words, I'd like enqueue a delayed job which will call the apn:notifications:deliver rake task.
I found this code http://pastie.org/157390 from http://geminstallthat.wordpress.com/2008/02/25/run-rake-tasks-with-delayedjob-dj/.
I added this code as DelayedRake.rb to my lib directory:
require 'rake'
require 'fileutils'
class DelayedRake
def initialize(task, options = {})
#task = task
#options = options
end
##
# Called by Delayed::Job.
def perform
FileUtils.cd RAILS_ROOT
#rake = Rake::Application.new
Rake.application = #rake
### Load all the Rake Tasks.
Dir[ "./lib/tasks/**/*.rake" ].each { |ext| load ext }
#options.stringify_keys!.each do |key, value|
ENV[key] = value
end
begin
#rake[#task].invoke
rescue => e
RAILS_DEFAULT_LOGGER.error "[ERROR]: task \"#{#task}\" failed. #{e}"
end
end
end
Everything runs fine until the delayed_job runs and it complains:
[ERROR]: task "apn:notifications:deliver" failed. Don't know how to build task 'apn:notifications:deliver'
How do I let it know about apn_on_rails? I'd tried require 'apn_on_rails_tasks' at the top of DelayedRake which didn't do anything. I also tried changing the directory of rake tasks to ./lib/tasks/*.rake
I'm somewhat new to Ruby/Rails. This is running on 2.3.5 on heroku.

Why don't do just a system call ?
system "rake apn:notifications:deliver"

I believe it's easier if you call it as a separate process. See 5 ways to run commands from Ruby.
def perform
`rake -f #{Rails.root.join("Rakefile")} #{#task}`
end
If you want to capture any errors, you should capture STDERR as shown in the article.

Related

How can I get around-hook-like behavior for rake tasks?

I have a number of rake tasks for which I would like to implement around-hook-like behavior. Specifically, I'm looking for a way to ensure that all of my Rake tasks execute in a particular (complicated, derived) Time.use_zone block.
For analogy, I have this in my ApplicationController:
around_filter :use_time_zone
def use_time_zone
time_zone = non_trivial_derivation
Time.use_zone(time_zone) { yield }
end
And now all of my controller actions will appropriately execute in the specified time zone. I would like some mechanism like this for Rake. I'd be willing to change or modify the dependency chain for my rake tasks, but I don't want to insert the actual time zone derivation code at the top of each rake task, out of concerns that that would lead to maintenance fragility. I'm pretty sure that Rake dependencies hold the solution--after all, Rake dependencies allow me to execute code in the context of my Rails application. But I can't figure out how to get that done for this use case.
I came up with a simple solution that doesn't require any external dependencies or gems such as rake-hooks:
desc "rake around hook"
task :use_timezone, [:subtask] => :environment do |name, args|
puts "using timezone"
Rake::Task[args[:subtask]].invoke
puts "end using timezone"
end
task :testing do
puts "testing"
end
The idea is that you execute the main use_timezone task and pass in your actual task as an argument:
$ rake use_timezone[testing]
That outputs:
> using timezone
> testing
> end using timezone
For your case you can write it like this:
task :use_timezone, [:subtask] => :environment do |name, args|
time_zone = non_trivial_derivation
Time.use_zone(time_zone) { Rake::Task[args[:subtask]].invoke }
end
And use it like this:
$ rake use_timezone[your_task]
Hope that helps.

Determining whether gem was called as Rake task

I'm writing a gem that includes some rake tasks. I have some code in the before_configuration method that I want to run when my gem is loaded by the app at runtime, but NOT when a task is run with rake. How can I determine that?
lib/mygem/tasks.rake:
namespace :mygem do
task :dosomething do
puts "DONE"
end
end
lib/mygem/railtie.rb:
require "rails"
module Mygem
class Railtie < ::Rails::Railtie
config.before_configuration do
#is_rake_task = ?
if !is_rake_task
# Do something
end
end
end
end
Rake is only defined in rake context.
So, you could simply go something like that :
if defined? Rake
# rake specific stuff
else
# non rake stuff
end
Edit :
While this will work perfectly with rails s, there's a problem with zeus on development environment : zeus will require rake.
If this is a problem, if think you can take advantage of Rake.application, which sets an instance variable when a rake task is executed.
I've tested this in a zeus s context :
> Rake.instance_variable_defined? :#application
false
And in a rake <task> context :
> Rake.instance_variable_defined? :#application
true
So, a complete solution would be :
if defined?( Rake ) and Rake.instance_variable_defined?( :#application )
# rake specific stuff
else
# non rake stuff
end

Rake task not running from within worker

I don't understand why my rake task is not running from within a resque worker. Running
rake :send_this_email
from the console works fine, I just want to run it as a cron job (as follows) but something is not working proplerly while invoking the rake task from within the worker.
My rescue_schedule.yml
send_this_email:
cron: "*/2 * * * *"
class: SendThisEmailWorker
args:
description: "Send email when condition defined in rake task is met"
My send_this_email_worker.rb in workers directory, where the problem must be if I can manually call the rake task myself from the console?
require 'rake'
module SendThisEmailWorker
#queue = :send_this_email
def self.perform
Rake::Task["send_this_email"].invoke
end
end
When I start my dev server this send_this_email rake task should run every 2 minutes correct? It's not and the resque admin panel shows it as a job in the queue. What am I missing here?
Thanks for your attention.
UPDATED from gerep comment
require 'rake'
module SendThisEmailWorker
#queue = :send_this_email
def self.perform
puts "Hi from the console, I'm started"
Rake::Task["send_this_email"].invoke
end
end
Only require 'rake' is not enough. For example if you do
Rake::Task.tasks #list down all task
You will get []
You need to tell your worker class to load tasks.
Try this
require 'rake'
Rake::Task.clear # necessary to avoid tasks being loaded several times in dev mode
YOUR_APP_NAME::Application.load_tasks
module SendThisEmailWorker
#queue = :send_this_email
def self.perform
puts "Hi from the console, I'm started"
Rake::Task["send_this_email"].invoke
end
end
YOUR_APP_NAME is the name of your app and can be found at config/application.rb

Create my own rake test:foo task

I have a bunch of tests that aren't unit or functional tests, they're of the format test/foo/special_test.rb
I want to create a rake task like rake test:units that will run all the tests in the foo folder. How do I do this?
Edit: I'd actually like rake test:foo to be a little different from rake test:units, in that I do not want it to run when I do simply rake test.
I don't remember where this is from, so unfortunately I can't give proper acknowledgement, but this should work. I say "should" because I've stopped using it, but grabbed it from my git history.
# First, 'reopen' the default :test namespace and create your custom task.
namespace :test do
Rake::TestTask.new(:foo_tests => ["test:prepare", "other_dependent_rake_tasks"] ) do |t|
DatabaseCleaner.strategy = :transaction # If using this.
t.libs << "test"
# Will also get subfolders within test/foo
t.test_files = FileList['test/foo/**/*_test.rb', 'test/foo/*_test.rb']
end
end
You can remove the default "test" task and redefine it so that when you run rake test it will automatically also run rake test:foo_tests.
remove_task "test"
desc 'Adding onto Rails regular tests'
task :test do
# Add all the names of tests you want run here.
errors = %w(test:units test:functionals test:integration test:foo_tests).collect do |task|
begin
puts "Running: #{task}"
Rake::Task[task].invoke
nil
rescue => e
task
end
end.compact
abort "Errors running #{errors * ', '}!" if errors.any?
end

rufus-scheduler fails with rake tasks (Don't know how to build task)

I'm using Rails3 (Windows, Ruby 1.8.7) with rufus-scheduler gem. Gem works fine, but if I'm trying to run some standard rake task, error occurs:
Don't know how to build task 'db:version' # ofc, db:version is just example
Terminal command
rake -T
works
If I'm trying to define own simple rake commands, they works fine too:
# /lib/my_scheduler.rb
require 'rubygems'
require 'rake'
require 'rufus/scheduler'
load File.join( Rails.root, 'lib', 'tasks', 'my_own_tasks.rake')
scheduler = Rufus::Scheduler.start_new
scheduler.every '5s' do
Rake::Task["my_own_namespace:test"].invoke
end
end
# /lib/tasks/my_own_tasks.rb
namespace :my_own_namespace do
task :test do
puts "Some scheduler task"
end
end
... but using standard rake tasks *in my_own_tasks* throws the same error.
Some help would be appreciated
PS. I'm newbie, so sorry, if that was dumb question
Maybe someone will need solution:
system("rake namespace:task")
f.e:
system("rake db:version")

Resources