Module: SidekiqUniqueJobs

Extended by:
JSON
Includes:
Connection
Defined in:
lib/sidekiq_unique_jobs/constants.rb,
lib/sidekiq_unique_jobs/cli.rb,
lib/sidekiq_unique_jobs/job.rb,
lib/sidekiq_unique_jobs/key.rb,
lib/sidekiq_unique_jobs/web.rb,
lib/sidekiq_unique_jobs/json.rb,
lib/sidekiq_unique_jobs/lock.rb,
lib/sidekiq_unique_jobs/redis.rb,
lib/sidekiq_unique_jobs/config.rb,
lib/sidekiq_unique_jobs/script.rb,
lib/sidekiq_unique_jobs/server.rb,
lib/sidekiq_unique_jobs/timing.rb,
lib/sidekiq_unique_jobs/digests.rb,
lib/sidekiq_unique_jobs/logging.rb,
lib/sidekiq_unique_jobs/version.rb,
lib/sidekiq_unique_jobs/lock_ttl.rb,
lib/sidekiq_unique_jobs/changelog.rb,
lib/sidekiq_unique_jobs/lock_args.rb,
lib/sidekiq_unique_jobs/lock_info.rb,
lib/sidekiq_unique_jobs/lock_type.rb,
lib/sidekiq_unique_jobs/locksmith.rb,
lib/sidekiq_unique_jobs/redis/set.rb,
lib/sidekiq_unique_jobs/connection.rb,
lib/sidekiq_unique_jobs/exceptions.rb,
lib/sidekiq_unique_jobs/middleware.rb,
lib/sidekiq_unique_jobs/normalizer.rb,
lib/sidekiq_unique_jobs/redis/hash.rb,
lib/sidekiq_unique_jobs/redis/list.rb,
lib/sidekiq_unique_jobs/script/dsl.rb,
lib/sidekiq_unique_jobs/timer_task.rb,
lib/sidekiq_unique_jobs/unlockable.rb,
lib/sidekiq_unique_jobs/deprecation.rb,
lib/sidekiq_unique_jobs/lock_config.rb,
lib/sidekiq_unique_jobs/lock_digest.rb,
lib/sidekiq_unique_jobs/on_conflict.rb,
lib/sidekiq_unique_jobs/reflectable.rb,
lib/sidekiq_unique_jobs/reflections.rb,
lib/sidekiq_unique_jobs/web/helpers.rb,
lib/sidekiq_unique_jobs/batch_delete.rb,
lib/sidekiq_unique_jobs/lock_timeout.rb,
lib/sidekiq_unique_jobs/redis/entity.rb,
lib/sidekiq_unique_jobs/redis/string.rb,
lib/sidekiq_unique_jobs/script/caller.rb,
lib/sidekiq_unique_jobs/script/client.rb,
lib/sidekiq_unique_jobs/script/config.rb,
lib/sidekiq_unique_jobs/script/script.rb,
lib/sidekiq_unique_jobs/script/timing.rb,
lib/sidekiq_unique_jobs/upgrade_locks.rb,
lib/sidekiq_unique_jobs/version_check.rb,
lib/sidekiq_unique_jobs/lock/base_lock.rb,
lib/sidekiq_unique_jobs/lock/validator.rb,
lib/sidekiq_unique_jobs/orphans/reaper.rb,
lib/sidekiq_unique_jobs/rspec/matchers.rb,
lib/sidekiq_unique_jobs/script/logging.rb,
lib/sidekiq_unique_jobs/script/scripts.rb,
lib/sidekiq_unique_jobs/update_version.rb,
lib/sidekiq_unique_jobs/on_conflict/log.rb,
lib/sidekiq_unique_jobs/orphans/manager.rb,
lib/sidekiq_unique_jobs/script/template.rb,
lib/sidekiq_unique_jobs/expiring_digests.rb,
lib/sidekiq_unique_jobs/orphans/observer.rb,
lib/sidekiq_unique_jobs/redis/sorted_set.rb,
lib/sidekiq_unique_jobs/script/lua_error.rb,
lib/sidekiq_unique_jobs/middleware/client.rb,
lib/sidekiq_unique_jobs/middleware/server.rb,
lib/sidekiq_unique_jobs/on_conflict/raise.rb,
lib/sidekiq_unique_jobs/lock/until_expired.rb,
lib/sidekiq_unique_jobs/on_conflict/reject.rb,
lib/sidekiq_unique_jobs/orphans/lua_reaper.rb,
lib/sidekiq_unique_jobs/lock/until_executed.rb,
lib/sidekiq_unique_jobs/on_conflict/replace.rb,
lib/sidekiq_unique_jobs/orphans/null_reaper.rb,
lib/sidekiq_unique_jobs/orphans/ruby_reaper.rb,
lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb,
lib/sidekiq_unique_jobs/lock/until_executing.rb,
lib/sidekiq_unique_jobs/lock/while_executing.rb,
lib/sidekiq_unique_jobs/on_conflict/strategy.rb,
lib/sidekiq_unique_jobs/lock/client_validator.rb,
lib/sidekiq_unique_jobs/lock/server_validator.rb,
lib/sidekiq_unique_jobs/options_with_fallback.rb,
lib/sidekiq_unique_jobs/on_conflict/reschedule.rb,
lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb,
lib/sidekiq_unique_jobs/on_conflict/null_strategy.rb,
lib/sidekiq_unique_jobs/logging/middleware_context.rb,
lib/sidekiq_unique_jobs/orphans/reaper_resurrector.rb,
lib/sidekiq_unique_jobs/lock/while_executing_reject.rb,
lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb,
lib/sidekiq_unique_jobs/rspec/matchers/have_valid_sidekiq_options.rb

Overview

Contains configuration and utility methods that belongs top level

Author:

Defined Under Namespace

Modules: Connection, JSON, Job, Logging, Middleware, Normalizer, OnConflict, OptionsWithFallback, Orphans, RSpec, Redis, Reflectable, Script, SidekiqWorkerMethods, Timing, Unlockable, Web Classes: BatchDelete, Changelog, Cli, Config, Conflict, Deprecation, Digests, DuplicateLock, DuplicateStrategy, ExpiringDigests, InvalidArgument, InvalidUniqueArguments, InvalidWorker, Key, Lock, LockArgs, LockConfig, LockDigest, LockInfo, LockTTL, LockTimeout, LockType, Locksmith, NoBlockGiven, NoSuchNotificationError, NotUniqueWorker, Reflections, Server, TimerTask, UniqueJobsError, UnknownLock, UpdateVersion, UpgradeLocks, VersionCheck

Constant Summary collapse

ThreadSafeConfig =

ThreadSafe config exists to be able to document the config class without errors

Concurrent::MutableStruct.new("ThreadSafeConfig",
:lock_timeout,
:lock_ttl,
:enabled,
:lock_prefix,
:logger,
:logger_enabled,
:locks,
:strategies,
:debug_lua,
:max_history,
:reaper,
:reaper_count,
:reaper_interval,
:reaper_timeout,
:reaper_resurrector_interval,
:reaper_resurrector_enabled,
:lock_info,
:raise_on_config_error,
:current_redis_version)
VERSION =

Returns the current SidekiqUniqueJobs version.

Returns:

  • (String)

    the current SidekiqUniqueJobs version

"8.0.7"
ARGS =
"args"
APARTMENT =
"apartment"
AT =
"at"
CHANGELOGS =
"uniquejobs:changelog"
CLASS =
"class"
CREATED_AT =
"created_at"
DEAD_VERSION =
"uniquejobs:dead"
DIGESTS =
"uniquejobs:digests"
EXPIRING_DIGESTS =
"uniquejobs:expiring_digests"
ERRORS =
"errors"
JID =
"jid"
LIMIT =
"limit"
LIVE_VERSION =
"uniquejobs:live"
LOCK =
"lock"
LOCK_ARGS =
"lock_args"
LOCK_ARGS_METHOD =
"lock_args_method"
LOCK_DIGEST =
"lock_digest"
LOCK_EXPIRATION =
"lock_expiration"
LOCK_INFO =
"lock_info"
LOCK_LIMIT =
"lock_limit"
LOCK_PREFIX =
"lock_prefix"
LOCK_TIMEOUT =
"lock_timeout"
LOCK_TTL =
"lock_ttl"
LOCK_TYPE =
"lock_type"
ON_CLIENT_CONFLICT =
"on_client_conflict"
ON_CONFLICT =
"on_conflict"
ON_SERVER_CONFLICT =
"on_server_conflict"
PAYLOAD =
"payload"
PROCESSES =
"processes"
QUEUE =
"queue"
RETRY =
"retry"
SCHEDULE =
"schedule"
TIME =
"time"
TIMEOUT =
"timeout"
TTL =
"ttl"
TYPE =
"type"
UNIQUE =
"unique"
UNIQUE_ACROSS_QUEUES =
"unique_across_queues"
UNIQUE_ACROSS_WORKERS =
"unique_across_workers"
UNIQUE_ARGS =
"unique_args"
UNIQUE_ARGS_METHOD =
"unique_args_method"
UNIQUE_DIGEST =
"unique_digest"
UNIQUE_PREFIX =
"unique_prefix"
UNIQUE_REAPER =
"uniquejobs:reaper"
WORKER =
"worker"

Class Method Summary collapse

Methods included from JSON

dump_json, load_json, safe_load_json

Methods included from Connection

included, #redis

Class Method Details

.configSidekiqUniqueJobs::Config

The current configuration (See: configure on how to configure)

Returns:



19
20
21
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 19

def config
  @config ||= reset! # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
end

.configure(options = {}) { ... } ⇒ Object

Configure the gem

This is usually called once at startup of an application

Parameters:

  • options (Hash) (defaults to: {})

    global gem options

Options Hash (options):

  • :lock_timeout (Integer) — default: default is 0
  • :lock_ttl (Integer) — default: default is 0
  • :enabled (true, false) — default: default is true
  • :lock_prefix (String) — default: default is 'uniquejobs'
  • :logger (Logger) — default: default is Sidekiq.logger

Yields:

  • control to the caller when given block



184
185
186
187
188
189
190
191
192
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 184

def configure(options = {})
  if block_given?
    yield config
  else
    options.each do |key, val|
      config.send(:"#{key}=", val)
    end
  end
end

.constantize(str) ⇒ Sidekiq::Worker

Attempt to constantize a string worker_class argument, always failing back to the original argument when the constant can’t be found

Returns:



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 254

def constantize(str)
  return str.class             if str.is_a?(Sidekiq::Worker) # sidekiq v6.x
  return str                   unless str.is_a?(String)
  return Object.const_get(str) unless str.include?("::")

  names = str.split("::")
  names.shift if names.empty? || names.first.empty?

  names.inject(Object) do |constant, name|
    # the false flag limits search for name to under the constant namespace
    #   which mimics Rails' behaviour
    constant.const_get(name, false)
  end
end

.disable!(&block) ⇒ false, true

Disable SidekiqUniuqeJobs either temporarily in a block or for good

Yield Returns:

  • (void)

    temporarily disable sidekiq unique jobs while executing a block of code

Returns:

  • (false)

    when not given a block

  • (true, false)

    the previous value of enable when given a block



134
135
136
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 134

def disable!(&block)
  toggle(false, &block)
end

.disabled?true, false

Checks if the gem has been disabled

Returns:

  • (true)

    when config.enabled is false

  • (false)

    when config.enabled is true



154
155
156
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 154

def disabled?
  !enabled?
end

.enable!(&block) ⇒ true, false

Enable SidekiqUniuqeJobs either temporarily in a block or for good

Yield Returns:

  • (void)

    temporarily enable sidekiq unique jobs while executing a block of code

Returns:

  • (true)

    when not given a block

  • (true, false)

    the previous value of enable when given a block



122
123
124
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 122

def enable!(&block)
  toggle(true, &block)
end

.enabled?true, false

Checks if the gem has been disabled

Returns:

  • (true)

    when config.enabled is true

  • (false)

    when config.enabled is false



144
145
146
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 144

def enabled?
  config.enabled
end

.fetch_redis_versionString

Returns the current redis version

Returns:

  • (String)

    a string like ‘5.0.2`



200
201
202
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 200

def fetch_redis_version
  Sidekiq.default_configuration.redis_info["redis_version"]
end

.locksHash<Symbol, SidekiqUniqueJobs::BaseLock>

The current locks

Returns:

  • (Hash<Symbol, SidekiqUniqueJobs::BaseLock>)

    the configured locks



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

def locks
  config.locks
end

.loggerLogger

The current logger

Returns:

  • (Logger)

    the configured logger



49
50
51
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 49

def logger
  config.logger
end

.logger=(other) ⇒ Logger

Set a new logger

Parameters:

  • other (Logger)

    another logger

Returns:

  • (Logger)

    the new logger



70
71
72
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 70

def logger=(other)
  config.logger = other
end

.logging?true, false

Check if logging is enabled

Returns:

  • (true, false)


80
81
82
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 80

def logging?
  config.logger_enabled
end

.nowTime

Current time

Returns:

  • (Time)


220
221
222
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 220

def now
  Time.now
end

.now_fFloat

Current time as float

Returns:

  • (Float)


210
211
212
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 210

def now_f
  now.to_f
end

.reflect {|x| ... } ⇒ void

This method returns an undefined value.

Yields notification stack for sidekiq unique jobs to configure notifications

Yield Parameters:



301
302
303
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 301

def reflect
  yield reflections if block_given?
end

.reflectionsReflections

Collection with notifications

Returns:



290
291
292
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 290

def reflections
  @reflections ||= Reflections.new # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
end

.reset!SidekiqUniqueJobs::Config

Resets configuration to deafult

Returns:



110
111
112
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 110

def reset!
  @config = SidekiqUniqueJobs::Config.default # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
end

.safe_constantize(str) ⇒ Sidekiq::Worker, String

Attempt to constantize a string worker_class argument, always failing back to the original argument when the constant can’t be found

Returns:



273
274
275
276
277
278
279
280
281
282
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 273

def safe_constantize(str)
  constantize(str)
rescue NameError => ex
  case ex.message
  when /uninitialized constant/
    str
  else
    raise
  end
end

.strategiesHash<Symbol, SidekiqUniqueJobs::Strategy>

The current strategies

Returns:

  • (Hash<Symbol, SidekiqUniqueJobs::Strategy>)

    the configured locks



29
30
31
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 29

def strategies
  config.strategies
end

.toggle(enabled) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Toggles enabled on or off

:nodoc:



163
164
165
166
167
168
169
170
171
172
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 163

def toggle(enabled)
  if block_given?
    enabled_was = config.enabled
    config.enabled = enabled
    yield
    config.enabled = enabled_was
  else
    config.enabled = enabled
  end
end

.use_config(tmp_config = {}) { ... } ⇒ void

This method returns an undefined value.

Temporarily use another configuration and reset to the old config after yielding

Parameters:

  • tmp_config (Hash) (defaults to: {})

    the temporary configuration to use

Yields:

  • control to the caller



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 92

def use_config(tmp_config = {})
  raise ::ArgumentError, "#{name}.#{__method__} needs a block" unless block_given?

  old_config = config.to_h
  reset!
  configure(tmp_config)
  yield
ensure
  reset!
  configure(old_config.to_h)
end

.validate_worker(options) ⇒ Boolean

Checks that the worker is valid with the given options

Parameters:

  • options (Hash)

    the ‘sidekiq_options` to validate

Returns:

  • (Boolean)

Raises:



231
232
233
234
235
236
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 231

def validate_worker(options)
  raise NotUniqueWorker, options unless (lock_type = options[LOCK])

  lock_class = locks[lock_type]
  lock_class.validate_options(options)
end

.validate_worker!(options) ⇒ Object

Checks that the worker is valid with the given options

Parameters:

  • options (Hash)

    the ‘sidekiq_options` to validate

Raises:



245
246
247
248
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 245

def validate_worker!(options)
  lock_config = validate_worker(options)
  raise InvalidWorker, lock_config unless lock_config.errors.empty?
end

.versionString

The current gem version

Returns:

  • (String)

    the current gem version



59
60
61
# File 'lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb', line 59

def version
  VERSION
end