0

ActiveJobs의 백엔드로 SQS를 사용하고 있습니다. 작업은 SQS로 전송레일 ActiveJob SQS 및 레코드가 여전히 존재하지 않습니다.

UpdateStuffAfterCreateJob.perform_later(self.id) 

: 이것은 ActiveJob를 초기화

after_create :update_stuff_after_create 

:

내 모델의 after_create 콜백 큐에 전송되는 아주 간단한 일이 큐가 제대로 대기 중입니다. 문제는 큐에서 읽는 담당하는 다른 예를을 때하는 것은 내가 보는 작업을 실행을 시도하는 인 ActiveRecord::RecordNotFound :

레코드가 있지만, 작업을 보내 인스턴스에 존재처럼
class UpdateStuffAfterCreateJob < ActiveJob::Base 
    def perform(id) 
    publisher_offer_click = MyModel.find(id) # ActiveRecord::RecordNotFound 
    my_model.update_stuff 
    end 
end 

이 보이는 작업을 사용하는 인스턴스에 레코드가 없습니다.

enter image description here


나는 깊은 디버깅되었고, 나는이 작업 만들 수 있습니다

class TestingEntityExistsJob < ActiveJob::Base 
    def perform(id, exists_outside, time_outside_string) 
    Rails.logger.info "[TestingEntityExistsJob] #{id}, #{MyModel.last.id}, #{Time.now.strftime("%H:%M:%S.%L")}, #{time_outside_string}, exists: #{MyModel.where(:id => publisher_offer_click_id).exists?}, exists_outside: #{exists_outside}" 
    end 
end 

를 내가 내 모델의 after_create에서 다음과 같이 호출 :

TestingEntityExistsJob.perform_later(self.id, MyModel.where(:id => self.id).exists?, Time.now.strftime('%H:%M:%S.%L')) 

다음과 같은 이상한 결과가 있습니다.

[2017-05-05 16:13:12.773] [TestingEntityExistsJob] 30645284, 30645284, 16:13:12.781, 16:13:12.720, exists: true, exists_outside: true 
[2017-05-05 16:13:12.773] [TestingEntityExistsJob] 30645281, 30645284, 16:13:12.781, 16:13:12.659, exists: true, exists_outside: true 
[2017-05-05 16:13:12.913] [TestingEntityExistsJob] 30645283, 30645284, 16:13:12.929, 16:13:12.838, exists: true, exists_outside: true 
[2017-05-05 16:13:12.964] [TestingEntityExistsJob] 30645285, 30645285, 16:13:12.970, 16:13:12.927, exists: true, exists_outside: true 
[2017-05-05 16:13:13.368] [TestingEntityExistsJob] 30645286, 30645285, 16:13:13.391, 16:13:13.309, exists: false, exists_outside: true 
[2017-05-05 16:13:13.374] [TestingEntityExistsJob] 30645287, 30645285, 16:13:13.391, 16:13:13.339, exists: false, exists_outside: true 
[2017-05-05 16:13:13.447] [TestingEntityExistsJob] 30645288, 30645288, 16:13:13.453, 16:13:13.411, exists: true, exists_outside: true 
[2017-05-05 16:13:13.718] [TestingEntityExistsJob] 30645289, 30645289, 16:13:13.723, 16:13:13.658, exists: true, exists_outside: true 
[2017-05-05 16:13:14.702] [TestingEntityExistsJob] 30645290, 30645290, 16:13:14.722, 16:13:14.642, exists: true, exists_outside: true 
[2017-05-05 16:13:14.842] [TestingEntityExistsJob] 30645291, 30645291, 16:13:14.850, 16:13:14.789, exists: true, exists_outside: true 
[2017-05-05 16:13:17.658] [TestingEntityExistsJob] 30645292, 30645292, 16:13:17.663, 16:13:17.599, exists: true, exists_outside: true 
[2017-05-05 16:13:18.558] [TestingEntityExistsJob] 30645294, 30645294, 16:13:18.565, 16:13:18.513, exists: true, exists_outside: true 
[2017-05-05 16:13:18.648] [TestingEntityExistsJob] 30645293, 30645294, 16:13:18.652, 16:13:18.592, exists: false, exists_outside: true 
[2017-05-05 16:13:19.565] [TestingEntityExistsJob] 30645295, 30645295, 16:13:19.570, 16:13:19.514, exists: true, exists_outside: true 
[2017-05-05 16:13:21.899] [TestingEntityExistsJob] 30645296, 30645295, 16:13:21.918, 16:13:21.771, exists: false, exists_outside: true 
[2017-05-05 16:13:21.916] [TestingEntityExistsJob] 30645297, 30645295, 16:13:21.923, 16:13:21.832, exists: false, exists_outside: true 
[2017-05-05 16:13:22.541] [TestingEntityExistsJob] 30645298, 30645298, 16:13:22.547, 16:13:22.484, exists: true, exists_outside: true 
[2017-05-05 16:13:23.223] [TestingEntityExistsJob] 30645299, 30645299, 16:13:23.228, 16:13:23.171, exists: true, exists_outside: true 

외부에서 항상 exists이라는 레코드가 표시 될 수 있지만 항상 실행되지는 않습니다. 때로는 MyModel.last.id이로드하려는 레코드의 id보다 작습니다.

왜 이런 일이 벌어 질 수 있는지 제안 해주세요. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

내 실제 해결 방법을 사용하는 것이 좋습니다는'UpdateStuffAfterCreateJob.set입니다 self.id)'나는 못 생겼다고 생각 했으므로 필요하지 않아야한다. / – fguillen

답변

1

이 동작의 이유는 save의 트랜잭션이 커밋되는 것처럼 after_create이 실행된다는 것입니다. 이로 인해 모델 데이터베이스 연결이 아직 커밋하지 않았으므로 작업의 데이터베이스 연결에 레코드에 대한 액세스 권한이없는 짧은 기간이 발생합니다. (=> 2.seconds 대기) .perform_later을 (

당신은

after_commit :some_method, on: [:create] 

대신

after_create :some_method 

https://apidock.com/rails/ActiveRecord/Transactions/ClassMethods/after_commit