7

我在 lib/models/alert_import' 中有一个文件 alert_import,我想在我的任务中使用这样的东西:

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

在这段代码中我得到错误:

找不到 ../../lib/models/alert_import

在 AlertImport 我有:

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

正确的解决方案是:

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
4

5 回答 5

9

在 Rails 3.x 中,我首先使用导入文件require,然后将模块包含到命名空间中,从而取得了成功。下面是它的外观:

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
于 2014-06-24T08:10:55.113 回答
3

很可能您的路径错误,您可以执行以下操作

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}"这将为您提供项目的当前路径

于 2013-01-22T10:32:42.853 回答
3

我尝试了一些选项,最显着的是尝试rake require,但看起来文档rake_require不正确。它特别不包括不以结尾的文件.rake

所以最后,我是“从头开始”做的——像这样:```

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

完毕。

于 2017-05-23T02:50:31.330 回答
1

你的路径不对,你可以试试:

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

问候!

于 2013-01-22T10:15:16.963 回答
0

检查那里http://rake.rubyforge.org/classes/Rake/Application.html#M000099 定义正确的路径

于 2013-01-22T11:16:21.460 回答