我有一个在本地运行的简单工作(在开发和生产以及暂存中)但在heroku(生产和暂存)上失败。
命令是DoctorLocation.reindex!
,这是一种用于algolia的方法。
引发的错误是NameError: uninitialized constant DoctorProfile::Recommendation
:
app[worker.1]: [Worker(host:c304ffd7-6c21-4eeb-b423-d42fae5d2adb pid:4)] Job DoctorLocation.reindex! (id=70) FAILED (3 prior attempts) with NameError: uninitialized constant DoctorProfile::Recommendation
奇怪的是DoctorLocation.reindex!
能够在控制台中成功运行,只有在使用延迟的作业时它才会失败。
另请注意,DoctorProfile
和Recommendation
是两个独立的模型,这使得此错误更加混乱。
更新:医生位置模型的代码:
class DoctorLocation < ApplicationRecord
include AlgoliaSearch
include AlgoliaDelayedJob
belongs_to :doctor_profile
belongs_to :location
delegate :first_name, :last_name, :name, :photo_url, :gender, :practice, to: :doctor_profile
unless Rails.env.test?
algoliasearch :per_environment => true, if: :approved?,
id: :uuid, enqueue: :trigger_delayed_job do
attribute :id, :uuid, :first_name, :last_name, :name, :photo_url, :gender,
:practice, :location
attribute :doctor_id do
doctor_profile.id
end
attribute :patients do
doctor_profile.patients.map{|x| {
uuid: x.uuid, photo_url: x.photo_url, first_name: x.first_name,
last_name: x.last_name, name: x.name, id: x.id
}}
end
attribute :reviews do
doctor_profile.reviews.map{|x| {
body: x.body,
patient_profile_photo_url: x.patient_profile.photo_url,
patient_profile_uuid: x.patient_profile.uuid,
patient_profile_id: x.patient_profile.id,
patient_profile_name: x.patient_profile.name
}}
end
attribute :patient_count do
doctor_profile.patients.length
end
attribute :patient_ids do
doctor_profile.patients.map(&:uuid)
end
attribute :insurances do
doctor_profile.insurance_providers.map{|x| "#{x.category}:#{x.name}"}
end
attribute :specialties do
doctor_profile.specialties.map(&:name)
end
geoloc :lat, :lon
tags do
doctor_profile.specialties.map(&:name)
end
attribute :group_ids do
doctor_profile.patients.map{|x| x.group_ids }.flatten.compact.uniq
end
attribute :liked_by_friends_of_user_id do
doctor_profile.patients.map{ |x|x.friend_ids }.flatten.compact.uniq
end
attributesToIndex ['first_name', 'last_name', 'name']
attributesForFaceting [
:insurances, :gender, :patient_ids, :liked_by_friends_of_user_id,
:group_ids
]
customRanking ['desc(patient_count)']
end
end
private
def approved?
doctor_profile.approved
end
def lat
location.latitude
end
def lon
location.longitude
end
end
module AlgoliaDelayedJob
extend ActiveSupport::Concern
module ClassMethods
def trigger_delayed_job(record, remove)
if remove
# record not saved, so delete from index by id
# (can't instantiate the model and call .delay.remove_from_index!)
index = Algolia::Index.new("#{self.name}_#{Rails.env}")
index.delete_object(record.uuid)
else
obj = self.find(record.id)
obj.index!
end
end
end
end