I have defined a rake task and a worker job using sidekiq as below. The rake task is not calling the worker job. Please help me find my problem.
Office.rake
namespace :office do
desc "send reminder emails"
task send_reminder: :environment do
Office.all.each do |office|
(office.issues.includes(:billings).where("issues.api_accounts_receivable > 0").select { |issue| (issue.billings.last.date < Message.last.date) if issue.billings.present? }).each do |issue|
puts "Issue ID is #{issue["id"]}"
ReminderWorker.perform_async("#{issue["id"]}")
end
end
end
end
reminder_worker.rb
class ReminderWorker
include Sidekiq::Worker
def perform(issue_id)
puts issue_id
end
end
puts don't show output for issue_id in the terminal for the worker but does show output for "Issue ID is #{issue["id"]}" mentioned in the rake task when I call the rake office:send_reminder from the terminal.
I found my mistake. I didn't turn on the sidekiq server.
Related
In my rails project (Rails 3.1, Ruby 1.9.3) there are around 40 rake tasks defined. The requirement is that I should be able to create an entry (the rake details) in a database table right when we start each rake. The details I need are the rake name, arguments, start time and end time. For this purpose, I don't want rake files to be updated with the code. Is it possible to do this outside the scope of rake files.
Any help would be greatly appreciated!!
Try this
https://github.com/guillermo/rake-hooks
For example in your Rakefile
require 'rake/hooks'
task :say_hello do
puts "Good Morning !"
end
before :say_hello do
puts "Hi !"
end
#For multiple tasks
namespace :greetings do
task :hola do puts "Hola!" end ;
task :bonjour do puts "Bonjour!" end ;
task :gday do puts "G'day!" end ;
end
before "greetings:hola", "greetings:bonjour", "greetings:gday" do
puts "Hello!"
end
rake greetings:hola # => "Hello! Hola!"
This seems to be a bit awkward, But it may help others.
Rake.application.top_level_tasks
will return an array of information including Rake name and its arguments.
Reference attached below.
pry(main)> a = Rake.application.top_level_tasks
=> ["import_data[client1,", "data.txt]"]
When you create rake task, you can pass a parent task which will run before your task:
task my_task: :my_parent_task do
# ...
end
If your task depends from more than 1 task, you can pass an array of parent tasks
task my_task: [:my_prev_task, :my_another_prev_task] do
# ...
end
Have just installed whenever gem https://github.com/javan/whenever to run my rake tasks, which are nokogiri / feedzilla dependent scraping tasks.
eg my tasks are called grab_bbc, grab_guardian etc
My question - as I update my site, I keep add more tasks to scheduler.rake.
What should I write in my config/schedule.rb to make all rake tasks run, no matter what they are called?
Would something like this work?
every 12.hours do
rake:task.each do |task|
runner task
end
end
Am new to Cron, using RoR 4.
namespace :sc do
desc 'All'
task all: [:create_categories, :create_subcategories]
desc 'Create categories'
task create_categories: :environment do
# your code
end
desc 'Create subcategories'
task create_subcategories: :environment do
# your code
end
end
in console write $ rake sc:all
write separate rake tasks for each scraping tasks. then write a aggregated task to run all those scraping rake tasks.
desc "scrape nytimes"
task :scrape_nytimes do
# scraping method
end
desc "scrape guardian"
task :scrape_guardian do
# scraping method
end
desc "perform all scraping"
task :scrape do
Rake::Task[:scrape_nytimes].execute
Rake::Task[:scrape_guardian].execute
end
then call the rake task as
rake scrape
Make sure you have a unique namespace with all the tasks in it, like:
namespace :scrapers do
desc "Scraper Number 1"
task :scrape_me do
# Your code here
end
desc "Scraper Number 2"
task :scrape_it do
# Your code here
end
end
You could then run all tasks of that namespace with a task outside of that namespace:
task :run_all_scrapers do
Rake.application.tasks.each do |task|
task.invoke if task.name.starts_with?("scrapers:")
end
end
That said, I'm pretty sure that this is not how you should run a set of scrapers. If for any reason the if part should return true you might unintenionally run tasks like rake db:drop
Either "manually" maintaining schedule.rb or a master task seems like a better option to me.
The aggregated task can be concise:
namespace :scrape do
desc "scrape nytimes"
task :nytimes do
# scraping method
end
desc "scrape guardian"
task :guardian do
# scraping method
end
end
desc "perform all scraping"
task scrape: ['scrape:nytimes', 'scrape:guardian']
Namespaces are also a good practice.
Use namespace and in_namespace to run all tasks dynamically.
I prefer this method because it keeps things clean and precludes you from having to remember to update your "parent" task if any of our namespace tasks change.
Note, the example was borrowed from Dmitry Shvetsov's excellent answer.
namespace :scrape do
desc "scrape nytimes"
task :nytimes do
# scraping method
end
desc "scrape guardian"
task :guardian do
# scraping method
end
end
desc "perform all scraping"
task :scrape do
Rake.application.in_namespace( :scrape ){ |namespace| namespace.tasks.each( &:invoke ) }
end
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
I'm trying to create a Rake task that invokes two other rake tasks. I've found people with related questions here and here, but it hasn't been very useful. This is what I've cobbled together so far. Any idea what I'm doing wrong?
task :cron => :environment do
#if Time.now.hour % 2 == 0
Rake::Task["robots:update_robots"].reenable
Rake::Task["robots:update_robots"].invoke
#end
end
As you can see, it's a cron job that's meant for Heroku to do. But I've commented out what I don't need so I can test that it's working.
I keep getting this error:
Don't know how to build task 'robots:update_robots'
But I have no idea why.
UPDATE: So I it turns out I wasn't able to run the original task that was being called by my cron rake task. I had it running ok for a while, buy somewhere along the line, I deleted the "d" in "update". So this command
Rake::Task["robots:upate_robots"].execute
didn't because the robots rake task was "upate", not "update".
Tl;dr: typos.
In general, your solution should work:
require 'rake'
task :environment do
puts 'task environment'
end
namespace :robots do
task :update_robots do
puts "task robots:update_robots"
end
end
task cron: :environment do
puts 'task cron'
Rake::Task['robots:update_robots'].reenable
Rake::Task['robots:update_robots'].invoke
end
Rake::Task['robots:update_robots'].invoke
Rake::Task[:cron].invoke
# >> robots:update_robots was invoked
# >> task robots:update_robots
# >> task environment
# >> task cron
# >> task robots:update_robots
My first thought is that you must have the rake task wrong (are you sure it's "robots:update_robots" ?)
It's unusual to me that you need to reenable it, this implies that what you want is not Rake, but just plain old Ruby. Move the contents of the update_robots task out to a method which you can then invoke directly instead of trying to treat tasks like methods (tasks are for handling dependencies, they only invoke once on purpose, and your need to bend them around that implies you're using the wrong tool for the job). Then, both your code and the robots:update_robots can just call the same method:
require 'rake'
def update_robots
puts "method update_robots"
end
task :environment do
puts 'task environment'
end
namespace :robots do
task :update_robots do
update_robots
puts "task robots:update_robots"
end
end
task cron: :environment do
puts 'task cron'
update_robots
end
Rake::Task['robots:update_robots'].invoke
Rake::Task[:cron].invoke
# >> method update_robots
# >> task robots:update_robots
# >> task environment
# >> task cron
# >> method update_robots
Is there any way in rails to call a method, such as before, automatically when running a rake task I've built?
Let's say we have
namespace :migrate do
def before
# do this before all tasks
end
desc 'migrate authors from legacy database'
task :authors => :environment do
# some code here
end
end
I want to the before method to run everytime a task runs.
See if this helps: http://www.rubyflow.com/items/4104