0

이전에는 교착 상태 문제에 대한 많은 경험이 없었지만 ActiveJob으로 작업하고 동시에 이러한 작업을 처리하려고하면이 문제가 발생합니다. 다음은 하나의 작업을 작성하는 예입니다. 그것이 작동하는 방식은 내가 ImportGameParticipationsJob을 시작하고 CreateOrUpdateGameParticipationJobs의 무리를 대기열에 넣습니다.Rails에서 ActiveJob을 사용할 때 데이터베이스에서 교착 상태를 피하는 방법은 무엇입니까?

SQL Server에서 많은 양의 교착 상태 오류가 발생하는 것을 막으려면 다음과 같은 원인이 발생할 수 있습니다. 객체를 채우기 위해 레코드를 선택하는 것에서 교착 상태가 발생할 수 있습니까? 또는 저장할 때 아래의 내 process_records 메서드 내에서 레코드를 저장/업데이트하려고 할 때 실제로 발생할 수 있습니까?

ImportGameParticipationsJob 그들은 최근에 업데이트하고 해당이 교착 상태에 도움이 설정할 수있는 추가 옵션을 추가

class CreateOrUpdateGameParticipationJob < ActiveJob::Base 
    queue_as :import_queue 

    def perform(*args) 
    if args.first.present? 
     game_key = args.first 

     # get all particpations for a given game 
     game_participations = GameRoster.where(game_key: game_key) 
     process_records(game_participations) 
    end 
    end 

    def process_records(participations) 
    # Loop through participations and build record for saving... 
    participations.each do |participation| 
     if participation.try(:player_id) 
     record = create_or_find(participation) 
     record = update_record(record, participation) 
     end 

     begin 
     if record.valid? 
      record.save 
     else 

     end 
     rescue Exception => e 

     end 
    end 
    end 

    def create_or_find(participation) 
    participation_record = GameParticipation.where(
     game_id: participation.game.try(:id), 
     player_id: participation.player.try(:id)) 
     .first_or_initialize do |record| 
     record.game = Game.find_by(game_key: participation.game_key) 
     record.player = Player.find_by(id: participation.player_id) 
     record.club  = Club.find_by(club_id: participation.club_id) 
     record.status = parse_status(participation.player_status) 
    end 
    return participation_record 
    end 

    def update_record(record, record) 
    old_status = record.status 
    new_status = parse_status(record.player_status) 
    if old_status != new_status 
     record.new_status = record.player_status 
     record.comment = "status was updated via participations import job" 
    end 
    return record 
    end 

end 

답변

0

class ImportGameParticipationsJob < ActiveJob::Base 
    queue_as :default 

    def perform(*args) 
    import_participations(args.first.presence) 
    end 

    def import_participations(*args) 
    games = Game.where(season: 2016) 
    games.each do |extract_record| 
     CreateOrUpdateGameParticipationJob.perform_later(extract_record.game_key) 
    end 
    end 
end 

CreateOrUpdateGameParticipationJob. 나는 같은 문제를 가지고 4.1에 있었고 4.1.1로 옮겨 가면서이 문제를 해결했다.

https://github.com/collectiveidea/delayed_job_active_record

https://rubygems.org/gems/delayed_job_active_record

문제

잠금 작업

당신은 코드를 잠금 유산을 사용해 볼 수 있습니다. 대개 느리지 만 특정 사람들에게는 더 잘 작동합니다.

지연 :: 백엔드 :: ActiveRecord.configuration.reserve_sql_strategy = default_sql

+0

내가 Sidekiq' – daveomcd

+0

아, 우리는의 무리에 지금 Sidekiq로 변환하는'사용'DelayedJob'을 사용하지 않는 그래서 나는이 똑같은 일에 빠지지 않기를 바란다. –