Search code examples
ruby-on-railsrubyrakerufus-scheduler

Using Rake with Rufus


I'm trying to user rake and rufus, both of which I am new to. I want to have Rufus call my rake task but I am getting the following error. Don't know how to build task 'inbox:process_inbox'

lib/tasks/inbox_tasks.rb

namespace :inbox do
  task :process_inbox do
    logger = Logger.new(Rails.root.to_s + "/log/scheduler.log")
    logger.info "Rufus Here!"
  end
end

rufus_scheduler.rb

require 'rufus-scheduler'
require 'rake'

scheduler = Rufus::Scheduler.new

scheduler.every '10s', :first_at => Time.now + 3 do
  Rake::Task["inbox:process_inbox"]
end

Solution

  • As @jmettraux (the creator of rufus-scheduler!) has already answered, the problem is that the rake task is defined in a .rb file instead of .rake file.

    Adding some more details to help in the future.

    While creating a new rake task, you could get the rails generator to automatically create the file with appropriate structure.

    Example: Running

    > rails g task inbox process_inbox
    create  lib/tasks/inbox.rake
    

    will create a file named lib/tasks/inbox.rake with content:

    namespace :inbox do
     desc "TODO"
     task process_inbox: :environment do
     end 
    
    end
    

    Having a DESC in the task definition is important; that allows for verifying that the rake task is defined and available, by running either rake -T inbox or rake -T | grep inbox

    > rake -T inbox
    rake inbox:process_inbox  # TODO