보석은 다음 전략으로 직업 고유성을 보호 할 수 있습니다.
전략 | 직업이 잠겨 있습니다 | 작업이 잠금 해제됩니다 |
---|---|---|
until_executing | 대기열로 밀 때 | 처리가 시작될 때 |
until_executed | 대기열로 밀 때 | 작업이 성공적으로 처리 되면 |
until_expired | 대기열로 밀 때 | 자물쇠가 만료 되면 |
until_and_while_executing | 대기열로 밀면 | 처리가 시작될 때 동시 작업을 방지하기 위해 런타임 잠금이 취소됩니다 추가 옵션이 있습니다 : runtime_lock_ttl , on_runtime_conflict |
while_executing | 처리가 시작될 때 | 작업이 처리 될 때 오류를 포함하여 모든 결과로 |
SidekiquniqueJobs에서 영감을 얻은 후드 아래의 Redlock을 사용합니다.
gemfile에 activejob-uniqueness
보석을 추가하십시오.
gem 'activejob-uniqueness'
Sidekiq Web UI의 작업 잠금 해제를 원한다면 패치를 명시 적으로 요구하십시오. 대기열 정리가 느려집니다!
gem 'activejob-uniqueness' , require : 'active_job/uniqueness/sidekiq_patch'
bundle install
명령을 실행하십시오.
ActiveJob :: 독창성은 구성없이 작동 할 준비가되었습니다. REDIS_URL
사용하여 Redis 인스턴스에 연결됩니다. 기본값을 무시하려면 다음 명령을 사용하여 이니셜 config/initializers/active_job_uniqueness.rb
만듭니다.
rails generate active_job:uniqueness:install
class MyJob < ActiveJob :: Base
# new jobs with the same args will raise error until existing one is executed
unique :until_executed
def perform ( args )
# work
end
end
class MyJob < ActiveJob :: Base
# new jobs with the same args will be logged within 3 hours or until existing one is being executing
unique :until_executing , lock_ttl : 3 . hours , on_conflict : :log
def perform ( args )
# work
end
end
구성으로 전역으로 기본값을 설정할 수 있습니다
class MyJob < ActiveJob :: Base
# Proc gets the job instance including its arguments
unique :until_executing , on_conflict : -> ( job ) { job . logger . info "Oops: #{ job . arguments } " }
def perform ( args )
# work
end
end
class MyJob < ActiveJob :: Base
unique :until_executed
def perform ( foo , bar , baz )
# work
end
def lock_key_arguments
arguments . first ( 2 ) # baz is ignored
end
end
class MyJob < ActiveJob :: Base
unique :until_executed
def perform ( foo , bar , baz )
# work
end
def lock_key
'qux' # completely custom lock key
end
def runtime_lock_key
'quux' # completely custom runtime lock key for :until_and_while_executing
end
end
선택된 전략은 자동으로 작업을 잠금 해제하지만 경우에 따라 (예 : 대기열이 정화 됨) 수동으로 작업을 잠금 해제하는 것이 편리합니다.
# Remove the lock for particular arguments:
MyJob . unlock! ( foo : 'bar' )
# or
ActiveJob :: Uniqueness . unlock! ( job_class_name : 'MyJob' , arguments : [ { foo : 'bar' } ] )
# Remove all locks of MyJob
MyJob . unlock!
# or
ActiveJob :: Uniqueness . unlock! ( job_class_name : 'MyJob' )
# Remove all locks
ActiveJob :: Uniqueness . unlock!
아마도 아마도 당신은 일자리가 시험에 잠겨 있기를 원하지 않을 것입니다. 이 라인을 테스트 스위트 ( rails_helper.rb
)에 추가하십시오.
ActiveJob :: Uniqueness . test_mode!
ActiveJob :: Oliqueness Instruments ActiveSupport::Notifications
:
lock.active_job_uniqueness
runtime_lock.active_job_uniqueness
unlock.active_job_uniqueness
runtime_unlock.active_job_uniqueness
conflict.active_job_uniqueness
runtime_conflict.active_job_uniqueness
그런 다음 ActiveJob::Base.logger
에 씁니다.
ActiveJob 버전 6.1
이전에는 콜백 체인이 중단 되더라도 항상 Enqueued MyJob (Job ID) ...
기록합니다. 세부
Redis Server 실행 (별도의 콘솔) :
docker run --rm -p 6379:6379 redis
다음과 함께 테스트 실행 :
bundle
rake
ActiveJob :: Oliqueness는 Sidekiq API를 지원하여 대기열 정리 (예 : Sidekiq Web UI를 통해)에서 작업 잠금 장치를 지원하지 않습니다. Sidekiq 5.1 일자리 사망을 시작합니다. 또한 잠금을 트리거합니다. 각 작업이 개별적으로 잠금 해제되므로 큰 대기열 정리가 훨씬 느려집니다 . SideKiq API 패치를 활성화하려면 Gemfile에서 명시 적으로 필요합니다.
gem 'activejob-uniqueness' , require : 'active_job/uniqueness/sidekiq_patch'
https://github.com/veeqo/activejob-uniqueness (https://github.com/veeqo.come)에서 버그 보고서 및 풀 요청은 환영합니다.
보석은 MIT 라이센스의 조건에 따라 오픈 소스로 제공됩니다.
Veeqo에서 우리 엔지니어 팀은 최상의 코딩 관행에서 최고 수준으로 구축 된 세계적 수준의 인벤토리 및 배송 플랫폼을 만들기위한 임무를 수행하고 있습니다. 우리는 성장하는 팀으로 여행에 참여할 다른 열정적 인 개발자를 찾고 있습니다. 전자 상거래에서 가장 흥미 진진한 기술 회사 중 하나를 위해 일하는 경력을 찾고 있다면, 우리는 귀하의 의견을 듣고 싶습니다.
Veeqo 개발자 블로그