简体   繁体   English

在Heroku Scheduler中使用已安装的Rails Engine中的模型

[英]Using models from a mounted Rails Engine in a Heroku Scheduler

I'm writing a rake task to use as a Heroku Scheduler to read and modify some models from a mounted Rail engine. 我正在编写一个rake任务,用作Heroku Scheduler来从已安装的Rail引擎读取和修改某些模型。 The engine is mounted and operating fine within the Rails app itself (the engine itself is in an isolated namespace) 引擎已在Rails应用程序本身中安装并运行良好(引擎本身位于独立的命名空间中)

I've created a rake task that references a model from the engine, and when Heroku Scheduler runs the task, I get an 'uninitialized constant' error. 我创建了一个rake任务,引用了引擎中的模型,当Heroku Scheduler运行该任务时,出现了“未初始化的常量”错误。 It loads the app models (ie models that I have defined in app/models/) fine. 它可以很好地加载应用程序模型(即我在app / models /中定义的模型)。

I appreciate that the Heroku Scheduler might not add models from mounted engines to the load path when it starts up, but I'm not sure what the correct way for me to explicitly tell the Scheduler to load these models is. 我很欣赏Heroku Scheduler在启动时可能不会将安装的引擎中的模型添加到加载路径,但是我不确定明确告诉Scheduler加载这些模型的正确方法是什么。

I have a basic understanding of Rails and mounting Rails engines, but obviously not enough to understand how to solve this problem. 我对Rails和安装Rails引擎有基本的了解,但显然不足以了解如何解决此问题。

How can I include the mounted engine models along with the app models in a rake task run by Heroku Scheduler? 如何在Heroku Scheduler运行的rake任务中包括已安装的引擎模型和应用程序模型?

EDIT added Rakefile 编辑添加了Rakefile

Here is my Rakefile, although it isn't going to reveal too much. 这是我的Rakefile,尽管不会透露太多。 It's all just application-specific models: 都是针对特定应用的模型:

require 'payments/worker' # in the Rails lib/ dir

task :handle_notifications do
  # set the loggers to stdout for scheduled tasks
  ActiveRecord::Base.logger = Logger.new(STDOUT)
  Rails.logger = Logger.new(STDOUT)

  worker = Worker.new

  # Notification is the model defined in the engine
  Notification.not_handled.each do |notification|
    worker.handle! notification
  end
end

So I get an uninitialized constant Notification from the Heroku Scheduler logging 所以我从Heroku Scheduler日志记录中获得了一个uninitialized constant Notification

IIRC,您必须通过指定在Rake任务中明确设置Rails环境

task :handle_notifications => :environment do

I think it is a simple as doing a 我认为这很简单

require 'your_engine_gem_name'

in your Rakefile or task that requires the Engine 在您的Rakefile或需要引擎的任务中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM