Class: SidekiqUniqueJobs::OnConflict::Replace

Inherits:
Strategy
  • Object
show all
Defined in:
lib/sidekiq_unique_jobs/on_conflict/replace.rb

Overview

Strategy to replace the job on conflict

Author:

Instance Attribute Summary collapse

Attributes inherited from Strategy

#item

Instance Method Summary collapse

Methods inherited from Strategy

#replace?

Methods included from Logging

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context

Constructor Details

#initialize(item) ⇒ Replace

Returns a new instance of Replace.

Parameters:

  • item (Hash)

    sidekiq job hash



12
13
14
15
16
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 12

def initialize(item)
  super
  @queue         = item[QUEUE_KEY]
  @unique_digest = item[UNIQUE_DIGEST_KEY]
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



9
10
11
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 9

def queue
  @queue
end

#unique_digestObject (readonly)

Returns the value of attribute unique_digest.



9
10
11
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 9

def unique_digest
  @unique_digest
end

Instance Method Details

#call { ... } ⇒ Object

Replace the old job in the queue

Yields:

  • to retry the lock after deleting the old one



20
21
22
23
24
25
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 20

def call(&block)
  return unless delete_job_by_digest

  delete_lock
  block&.call
end

#delete_job_by_digestObject

Delete the job from either schedule, retry or the queue



28
29
30
31
32
33
34
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 28

def delete_job_by_digest
  Scripts.call(
    :delete_job_by_digest,
    nil,
    keys: ["#{QUEUE_KEY}:#{queue}", SCHEDULE_SET, RETRY_SET], argv: [unique_digest],
  )
end

#delete_lockObject

Delete the keys belonging to the job



37
38
39
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 37

def delete_lock
  SidekiqUniqueJobs::Digests.delete_by_digest(unique_digest)
end