Class: SidekiqUniqueJobs::Changelog

Inherits:
Redis::SortedSet show all
Defined in:
lib/sidekiq_unique_jobs/changelog.rb

Overview

Class Changelogs provides access to the changelog entries

Author:

Constant Summary

Constants inherited from Redis::SortedSet

Redis::SortedSet::DEFAULT_COUNT, Redis::SortedSet::SCAN_PATTERN

Instance Attribute Summary

Attributes inherited from Redis::Entity

#key

Instance Method Summary collapse

Methods inherited from Redis::SortedSet

#clear, #count, #rank, #score

Methods inherited from Redis::Entity

#count, #exist?, #expires?, #pttl, #ttl

Methods included from Timing

clock_stamp, now_f, time_source, timed

Methods included from JSON

dump_json, load_json, safe_load_json

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

Constructor Details

#initializeChangelog

Returns a new instance of Changelog.



10
11
12
# File 'lib/sidekiq_unique_jobs/changelog.rb', line 10

def initialize
  super(CHANGELOGS)
end

Instance Method Details

#add(message:, digest:, job_id:, script:) ⇒ void

This method returns an undefined value.

Adds a new changelog entry

Parameters:

  • message (String)

    a descriptive message about the entry

  • digest (String)

    a unique digest

  • job_id (String)

    a Sidekiq JID

  • script (String)

    the name of the script adding the entry



24
25
26
27
# File 'lib/sidekiq_unique_jobs/changelog.rb', line 24

def add(message:, digest:, job_id:, script:)
  message = dump_json(message: message, digest: digest, job_id: job_id, script: script)
  redis { |conn| conn.zadd(key, now_f, message) }
end

#entries(pattern: SCAN_PATTERN, count: DEFAULT_COUNT) ⇒ Array<Hash>

The change log entries

Parameters:

  • pattern (String) (defaults to: SCAN_PATTERN)

    the pattern to match

  • count (Integer) (defaults to: DEFAULT_COUNT)

    the number of matches to return

Returns:



37
38
39
40
41
# File 'lib/sidekiq_unique_jobs/changelog.rb', line 37

def entries(pattern: SCAN_PATTERN, count: DEFAULT_COUNT)
  redis do |conn|
    conn.zscan(key, match: pattern, count: count).to_a.map { |entry| load_json(entry[0]) }
  end
end

#page(cursor: 0, pattern: "*", page_size: 100) ⇒ Array<Integer, Integer, Array<Hash>] the total size, next cursor and changelog entries

Paginate the changelog entries

Parameters:

  • cursor (Integer) (defaults to: 0)

    the cursor for this iteration

  • pattern (String) (defaults to: "*")

    “*” the pattern to match

  • page_size (Integer) (defaults to: 100)

    100 the number of matches to return

Returns:

  • (Array<Integer, Integer, Array<Hash>] the total size, next cursor and changelog entries)

    Array<Integer, Integer, Array<Hash>] the total size, next cursor and changelog entries



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sidekiq_unique_jobs/changelog.rb', line 52

def page(cursor: 0, pattern: "*", page_size: 100)
  redis do |conn|
    total_size, result = conn.multi do |pipeline|
      pipeline.zcard(key)
      pipeline.zscan(key, cursor, match: pattern, count: page_size)
    end

    # NOTE: When debugging, check the last item in the returned array.
    [
      total_size.to_i,
      result[0].to_i, # next_cursor
      result[1].map { |entry| load_json(entry) }.select { |entry| entry.is_a?(Hash) },
    ]
  end
end