require lib in rake task

南楼画角 提交于 2020-01-01 10:47:10

问题


I have a file alert_import in lib/models/alert_import', I would like to use in my task sth like this:

task :send_automate_alerts => :environment do
 # STDERR.puts "Path is #{$:}"
  Rake.application.rake_require '../../lib/models/alert_import'
  ai = AlertImport::Alert.new(2)
  ai.send_email_with_notifcations
end

In this code I get error:

Can't find ../../lib/models/alert_import

in AlertImport I have:

module AlertImport

  class Alert

    def initialize(number_days)
      @number_days = number_days
    end

    def get_all_alerts
      alerts = { }
      Organization.automate_import.each do |o|
        last_import = o.import_histories.where(import_type: "automate").last
        last_successful_import = ImportHistory.last_automate_successful_import(o)
        if last_import
          if last_import.created_at + @number_days.days >= Time.now
            alerts[o.id] ="Error during last automate import Last successful import was #{ last_successful_import ? last_successful_import.created_at : "never"}" if last_import.status == "failure"
            alerts[o.id] ="Error during last automate import - status pending Last successful import was #{ last_successful_import ? last_successful_import.created_at : "never"}" if last_import.status == "pending"
          else
            alerts[o.id] = "There were no new files uploaded within #{@number_days} days"
          end
        else
          alerts[o.id] = "The import was never triggered at all"
        end
      end
      alerts
    end

    def send_email_with_notifcations
      alerts =get_all_alerts
      unless alerts.empty?
        AlertMailer.email_notifications(alerts).deliver
      end
    end

  end

end

The correct solution is:

desc "Send alerts about automate imports"

task :send_automate_alerts => :environment do
  require "#{Rails.root}/lib/models/alert_import"
  ai = AlertImport::Alert.new(2)
  ai.send_email_with_notifcations
end

回答1:


In Rails 3.x, I've had success by first importing the file using require and then including the module to the namespace. Here's how it would look:

require 'models/alert_import'

namespace :alerts

  include AlertImport

  desc 'Send alerts about automate imports'
  task send_automate_alerts: :environment do
    ai = AlertImport::Alert.new(2)
    ai.send_email_with_notifcations
  end

end



回答2:


Most probably your path wrong, you can do as follow

task :send_automate_alerts => :environment do
 # STDERR.puts "Path is #{$:}"
  Rake.application.rake_require "#{Rails.root}/lib/models/alert_import"
  ai = AlertImport::Alert.new(2)
  ai.send_email_with_notifcations
end

"#{Rails.root}" this will give you the current path of your project




回答3:


I tried a few options, most notably trying rake require, but it looks like the documentation for rake_require is incorrect. It specifically will not include files that don't end in .rake

So in the end, I did it "from scratch" - something like this: ```

namespace :my_namespace do
  task :my_task do
    require File.join(Rails.root, 'app', 'services', 'my_module.rb')

    class Wrapper
      include MyModule
    end
    Wrapper.new.the_method_I_need(args)
  end
end

Done.




回答4:


Your path is wrong, you can try:

task :send_automate_alerts => :environment do
 # STDERR.puts "Path is #{$:}"
  Rake.application.rake_require "#{Rails.root}/lib/models/alert_import"
  ai = AlertImport::Alert.new(2)
  ai.send_email_with_notifcations
end

Regards!




回答5:


check out there http://rake.rubyforge.org/classes/Rake/Application.html#M000099 define correct path



来源:https://stackoverflow.com/questions/14455884/require-lib-in-rake-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!