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, #redis_pool

Instance Method Summary collapse

Methods inherited from Strategy

#replace?

Methods included from Timing

clock_stamp, now_f, time_source, timed

Methods included from Script::Caller

call_script, debug_lua, do_call, extract_args, max_history, normalize_argv, now_f, redis_version

Methods included from Logging

#build_message, included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context, #with_configured_loggers_context, #with_logging_context

Methods included from JSON

dump_json, load_json, safe_load_json

Constructor Details

#initialize(item, redis_pool = nil) ⇒ Replace

Initialize a new Replace strategy

Parameters:

  • item (Hash)

    sidekiq job hash



23
24
25
26
27
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 23

def initialize(item, redis_pool = nil)
  super(item, redis_pool)
  @queue       = item[QUEUE]
  @lock_digest = item[LOCK_DIGEST]
end

Instance Attribute Details

#lock_digestObject (readonly)

Returns the value of attribute lock_digest.



16
17
18
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 16

def lock_digest
  @lock_digest
end

#queueObject (readonly)

Returns the value of attribute queue.



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

def queue
  @queue
end

Instance Method Details

#call { ... } ⇒ void

This method returns an undefined value.

Replace the old job in the queue

Yields:

  • to retry the lock after deleting the old one



37
38
39
40
41
42
43
44
45
46
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 37

def call(&block)
  return unless (deleted_job = delete_job_by_digest)

  log_info("Deleted job: #{deleted_job}")
  if (del_count = delete_lock)
    log_info("Deleted `#{del_count}` keys for #{lock_digest}")
  end

  block&.call
end

#delete_job_by_digestString?

Delete the job from either schedule, retry or the queue

Returns:

  • (String)

    the deleted job hash

  • (nil)

    when deleting nothing



55
56
57
58
59
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 55

def delete_job_by_digest
  call_script(:delete_job_by_digest,
              keys: ["#{QUEUE}:#{queue}", SCHEDULE, RETRY],
              argv: [lock_digest])
end

#delete_lockInteger

Delete the keys belonging to the job

Returns:

  • (Integer)

    the number of keys deleted



67
68
69
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 67

def delete_lock
  digests.delete_by_digest(lock_digest, runtime: false)
end

#digestsDigests

Access to the Digests

Returns:

  • (Digests)

    and instance with digests



77
78
79
# File 'lib/sidekiq_unique_jobs/on_conflict/replace.rb', line 77

def digests
  @digests ||= SidekiqUniqueJobs::Digests.new
end