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 collapse

DEFAULT_COUNT =

Returns the number of matches to return by default.

Returns:

  • (Integer)

    the number of matches to return by default

1_000
SCAN_PATTERN =

Returns the default pattern to use for matching.

Returns:

  • (String)

    the default pattern to use for matching

"*"

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, 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.



17
18
19
# File 'lib/sidekiq_unique_jobs/changelog.rb', line 17

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



31
32
33
34
# File 'lib/sidekiq_unique_jobs/changelog.rb', line 31

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:



44
45
46
47
48
49
50
51
52
# File 'lib/sidekiq_unique_jobs/changelog.rb', line 44

def entries(pattern: SCAN_PATTERN, count: DEFAULT_COUNT)
  options = {}
  options[:match] = pattern
  options[:count] = count

  redis do |conn|
    conn.zscan_each(key, **options).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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sidekiq_unique_jobs/changelog.rb', line 63

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

    [
      total_size.to_i,
      result[0].to_i, # next_cursor
      result[1].map { |entry| load_json(entry[0]) }, # entries
    ]
  end
end