wurk 1.19.0 → 1.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: baff6a3f75bca45d6dbcbed8c3363b306ef0f37f8c48f9de0fac68f320ab9a79
4
- data.tar.gz: 3e1725ade18090f1d9eca95d0d3b016db9a15747f63bed75b4ef6cc95555982e
3
+ metadata.gz: 68607081f66bca6632668d2f456e66dfe87906ec13fa0582fac43868c5c11793
4
+ data.tar.gz: cb235411d7e504bccfdc83766540a6d5b6e9df3b2e41447efbc8928565be35d4
5
5
  SHA512:
6
- metadata.gz: 78be5c5f73decd87bda36782bde1ab6d3698b6fc05ad659d6b167c2fba7b7e63cb3250dc3c3ed07016addf03376bceecf6ffb67072ac6df0fbd6b88d975d1824
7
- data.tar.gz: 83e105bb40f2d95b89564cae52cd7fc85347bd6f131f822d91eac80b74d4ff45ee3c425e4a89fc87c8144717f0020b9a4de56584bfc1f03a76b72be074ae979e
6
+ metadata.gz: 386b8055b1fcd9de03d0451a0393df9af87f4746f6846c869ffae59fa6307eab8d3120bca67e2b9e85b98d68202fe2e8bf50833d086c0eaade01ce0090edf3d1
7
+ data.tar.gz: 927b92fb1a5fbbc69a4eea1945aeb659adc10eb9b1755678f6d214d356ac8b8a9f40893d6163482bbe50f7912dbbe46b0fe805e9781e59ad978005ba90ce653a
data/README.md CHANGED
@@ -308,7 +308,7 @@ Wurk is MIT and stays that way. If what you need is a commercial support contrac
308
308
 
309
309
  `bundle install && restart`. The worker command stays as it was: Wurk ships `sidekiq` and `sidekiqswarm` executables, so `bundle exec sidekiq -C config/sidekiq.yml` in a Procfile or manifest keeps working. Wurk reads and writes the same Redis schema, so there is no data migration: Wurk picks up the queues, schedules, retries, batches and periodic loops Sidekiq left behind. Running Sidekiq and Wurk workers against one Redis *at the same time* is tested for stock Sidekiq 8 only, which may roll from one to the other under the conditions the guide lists; for a Sidekiq 7.x, Pro or Enterprise app a live production move is a **drain cutover** (stop Sidekiq's workers, confirm nothing is in flight, start Wurk), with a matching rollback. The [production cutover](https://github.com/developerz-ai/wurk/blob/main/docs/migrate-from-sidekiq.md#9-production-cutover) section has every step and command. The upstream suites of sidekiq-cron, sidekiq-unique-jobs, sidekiq-failures and sentry-sidekiq run against Wurk in the [`ecosystem` CI job](https://github.com/developerz-ai/wurk/blob/main/.github/workflows/ecosystem.yml) (see [`test/ecosystem/`](https://github.com/developerz-ai/wurk/tree/main/test/ecosystem)). sidekiq-scheduler, sidekiq-status and sidekiq-throttled are target additions tracked in [docs/idea/14-ecosystem-compat.md](https://github.com/developerz-ai/wurk/blob/main/docs/idea/14-ecosystem-compat.md).
310
310
 
311
- **Keeping a `sidekiq-*` add-on gem?** Those gems declare `add_dependency "sidekiq"`, which would reinstall real Sidekiq next to Wurk. Satisfy it with Wurk's git-only [`sidekiq` shim gem](https://github.com/developerz-ai/wurk/tree/main/ecosystem/sidekiq-shim) instead, pinned to the tag of the Wurk release you run: `gem "sidekiq", github: "developerz-ai/wurk", tag: "v1.19.0", glob: "ecosystem/sidekiq-shim/*.gemspec"`. Only sidekiq-cron, sidekiq-unique-jobs, sidekiq-failures and sentry-sidekiq are exercised that way, by the `ecosystem` workflow; treat the others as untested.
311
+ **Keeping a `sidekiq-*` add-on gem?** Those gems declare `add_dependency "sidekiq"`, which would reinstall real Sidekiq next to Wurk. Satisfy it with Wurk's git-only [`sidekiq` shim gem](https://github.com/developerz-ai/wurk/tree/main/ecosystem/sidekiq-shim) instead, pinned to the tag of the Wurk release you run: `gem "sidekiq", github: "developerz-ai/wurk", tag: "v1.20.0", glob: "ecosystem/sidekiq-shim/*.gemspec"`. Only sidekiq-cron, sidekiq-unique-jobs, sidekiq-failures and sentry-sidekiq are exercised that way, by the `ecosystem` workflow; treat the others as untested.
312
312
 
313
313
  Full walkthrough — config side-by-side, the Redis key/`sidekiq_options` mapping, known incompatibilities, moving sidekiq-cron schedules over (`rake wurk:import:cron`), and the production cutover and rollback playbook: **[docs/migrate-from-sidekiq.md](https://github.com/developerz-ai/wurk/blob/main/docs/migrate-from-sidekiq.md)**. On the night, keep the **[incident runbook](https://github.com/developerz-ai/wurk/blob/main/docs/runbook.md)** open.
314
314
 
data/lib/wurk/dead_set.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'job_set'
4
+ require_relative 'metrics/deaths'
4
5
 
5
6
  module Wurk
6
7
  # Capped ZSET of jobs that exhausted retries (the "morgue"). Bounded by
7
8
  # `dead_max_jobs` and `dead_timeout_in_seconds` config knobs — every
8
- # `kill` trims both axes. Death handlers fire by default — including on
9
+ # `kill` trims both axes, and counts the job into Metrics::Deaths, which
10
+ # the trim never touches. Death handlers fire by default — including on
9
11
  # API/UI kills, matching Sidekiq — unless `notify_failure: false`.
10
12
  #
11
13
  # Spec: docs/target/sidekiq-free.md §19.5, §17.2, §31.8.
@@ -52,8 +54,7 @@ module Wurk
52
54
  # malformed path has no parseable job to hand a handler. `max_jobs:` /
53
55
  # `timeout:` propagate to #trim (see there for the override rationale).
54
56
  def kill_raw(payload, max_jobs: nil, timeout: nil) # rubocop:disable Naming/PredicateMethod
55
- now = ::Process.clock_gettime(::Process::CLOCK_REALTIME)
56
- Wurk.redis { |conn| conn.call('ZADD', @name, now.to_s, payload) }
57
+ add(payload)
57
58
  trim(max_jobs: max_jobs, timeout: timeout)
58
59
  true
59
60
  end
@@ -65,8 +66,7 @@ module Wurk
65
66
  # receive `(job, ex)`. `max_jobs:` / `timeout:` propagate to the auto-trim;
66
67
  # see `#trim` for the rationale.
67
68
  def kill(message, opts = {})
68
- now = ::Process.clock_gettime(::Process::CLOCK_REALTIME)
69
- Wurk.redis { |conn| conn.call('ZADD', @name, now.to_s, message) }
69
+ add(message)
70
70
  trim(max_jobs: opts[:max_jobs], timeout: opts[:timeout]) if opts.fetch(:trim, true)
71
71
  fire_death_handlers(message, opts[:ex] || api_kill_error) if opts.fetch(:notify_failure, true)
72
72
  true
@@ -74,6 +74,14 @@ module Wurk
74
74
 
75
75
  private
76
76
 
77
+ def add(message)
78
+ now = ::Process.clock_gettime(::Process::CLOCK_REALTIME)
79
+ Wurk.redis do |conn|
80
+ conn.call('ZADD', @name, now.to_s, message)
81
+ Metrics::Deaths.record(conn, message)
82
+ end
83
+ end
84
+
77
85
  # Only the death handlers see it, so a kill that notifies none (a UI kill,
78
86
  # `kill_all` over a whole set) never pays for the caller's backtrace.
79
87
  def api_kill_error
data/lib/wurk/keys.rb CHANGED
@@ -47,6 +47,12 @@ module Wurk
47
47
  # elapsed. Per-day variants append `:YYYY-MM-DD`. Spec: sidekiq-pro.md §7.
48
48
  STAT_EXPIRED = 'stat:expired'
49
49
 
50
+ # Jobs moved to the dead set, per queue and class (Metrics::Deaths): HASH,
51
+ # field = JSON `[queue, class]`, value = count. A Wurk extra like
52
+ # `status:` — Sidekiq keeps no death counter — and, unlike the capped
53
+ # `dead` set it counts into, it only ever grows.
54
+ STAT_DEATHS = 'stat:deaths'
55
+
50
56
  # TTL applied to per-day `stat:processed:*` / `stat:failed:*` /
51
57
  # `stat:expired:*` strings. 5 years, in seconds. Matches
52
58
  # Sidekiq::Launcher::STATS_TTL.
@@ -0,0 +1,12 @@
1
+ -- Counts one job into the death counter (Metrics::Deaths). A field already in
2
+ -- the hash always counts; a new one counts only while the hash holds fewer than
3
+ -- the cap, and into the overflow field past it, so junk payloads cannot grow
4
+ -- the hash, or a scrape of it, without bound.
5
+ -- KEYS = [deaths hash]
6
+ -- ARGV = [field, max fields, overflow field]
7
+ -- Returns the field's new count.
8
+ local field = ARGV[1]
9
+ if redis.call("hexists", KEYS[1], field) == 0 and redis.call("hlen", KEYS[1]) >= tonumber(ARGV[2]) then
10
+ field = ARGV[3]
11
+ end
12
+ return redis.call("hincrby", KEYS[1], field, 1)
data/lib/wurk/lua.rb CHANGED
@@ -63,8 +63,10 @@ module Wurk
63
63
  # retry would starve the whole cluster's scheduled jobs. It is moved to the
64
64
  # dead set (scored `now`, like a kill) so it stays inspectable, and the
65
65
  # sweep continues. pcall keeps a failed decode from raising out of the script.
66
- # KEYS = [sorted_set, queues_set, dead_set]
67
- # ARGV = [now, queue_prefix, now_ms, batch]
66
+ # Each one moved is counted into the death counter the way
67
+ # `lua/count_death.lua` counts (Metrics::Deaths), under queue `unknown`.
68
+ # KEYS = [sorted_set, queues_set, dead_set, deaths_hash]
69
+ # ARGV = [now, queue_prefix, now_ms, batch, deaths_max_fields, deaths_overflow_field]
68
70
  # Returns the number of members handled (promoted or moved to dead).
69
71
  # Order matters: decode + push BEFORE zrem. Redis Lua has no rollback,
70
72
  # so a failed cjson.decode after a zrem would lose the job. Decode first;
@@ -126,6 +128,18 @@ module Wurk
126
128
  if not last then return job end
127
129
  return string.sub(job, 1, e) .. colon .. ARGV[3] .. string.sub(job, last + 1)
128
130
  end
131
+ local function label(v) if type(v) == "string" and v ~= "" then return v end end
132
+ -- Best effort, pcall only: a refused count (a wrong-typed key, maxmemory)
133
+ -- must not stop the ZREM below, or the member is re-buried every sweep.
134
+ local function count_death(decoded)
135
+ local job = type(decoded) == "table" and decoded or {}
136
+ local field = cjson.encode({"unknown", label(job["wrapped"]) or label(job["class"]) or "unknown"})
137
+ local size = redis.pcall("hlen", KEYS[4])
138
+ if redis.pcall("hexists", KEYS[4], field) == 0 and type(size) == "number" and size >= tonumber(ARGV[5]) then
139
+ field = ARGV[6]
140
+ end
141
+ redis.pcall("hincrby", KEYS[4], field, 1)
142
+ end
129
143
  local jobs = redis.call("zrangebyscore", KEYS[1], "-inf", ARGV[1], "LIMIT", 0, tonumber(ARGV[4]))
130
144
  for i = 1, #jobs do
131
145
  local job = jobs[i]
@@ -138,6 +152,7 @@ module Wurk
138
152
  redis.call("zadd", KEYS[3], ARGV[1], job)
139
153
  end
140
154
  redis.call("zrem", KEYS[1], job)
155
+ if type(q) ~= "string" then count_death(ok and decoded) end
141
156
  end
142
157
  return #jobs
143
158
  LUA
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require_relative '../keys'
5
+ require_relative '../lua'
6
+
7
+ module Wurk
8
+ module Metrics
9
+ # Cluster-wide, monotonic count of jobs moved to the dead set, per queue and
10
+ # class, in `stat:deaths` (Keys::STAT_DEATHS). The dead set is capped at
11
+ # `dead_max_jobs`: once full, every kill trims the oldest entry, so its size
12
+ # stops moving and cannot say how many jobs died. This count can.
13
+ #
14
+ # Labels come from the payload's `queue` and its `wrapped` class (an
15
+ # ActiveJob's own class) or `class`, never the jid or args. A field the
16
+ # payload cannot supply is `unknown`. Distinct pairs past MAX_SERIES fold
17
+ # into OVERFLOW (`lua/count_death.lua`).
18
+ #
19
+ # Best effort, and only Wurk writes it (a stock Sidekiq process in a mixed
20
+ # fleet does not). #record reports a failed count, which may or may not have
21
+ # landed (a lost reply); the schedule-promote script cannot report, and drops it.
22
+ #
23
+ # Fields are encoded `script_safe`, escaping `/` as Redis's cjson does, so a
24
+ # pair the schedule-promote script counts lands on the same field.
25
+ module Deaths
26
+ MAX_SERIES = 500
27
+ UNKNOWN = 'unknown'
28
+ OTHER = 'other'
29
+ OVERFLOW = ::JSON.generate([OTHER, OTHER]).freeze
30
+ # Name, help and type of the `/metrics` family (Metrics::Prometheus), whose
31
+ # samples are #samples. Read from shared Redis, so every scrape target
32
+ # reports the same value: aggregate with `max`.
33
+ FAMILY = ['wurk_jobs_dead_total', 'Jobs moved to the dead set, by queue and class (stat:deaths).',
34
+ 'counter'].freeze
35
+
36
+ class << self
37
+ # Counts one death. Runs once, never replayed (`eval_once`), so a dropped
38
+ # socket can lose a count but never add one. Reported, not raised: the
39
+ # job is already in the dead set, and a caller that saw a raise would
40
+ # treat the kill as failed and kill it again.
41
+ def record(conn, payload, config: Wurk.configuration)
42
+ Lua::Loader.eval_once(conn, :count_death, keys: [Keys::STAT_DEATHS],
43
+ argv: [field(payload), MAX_SERIES, OVERFLOW])
44
+ rescue RedisClient::Error => e
45
+ config.handle_exception(e, context: 'dead-set death counter')
46
+ end
47
+
48
+ # The hash field for one payload: JSON `[queue, class]`.
49
+ def field(payload)
50
+ job = payload.is_a?(::String) ? parse(payload) : payload
51
+ job = {} unless job.is_a?(::Hash)
52
+ ::JSON.generate([label(job['queue']) || UNKNOWN, label(job['wrapped']) || label(job['class']) || UNKNOWN],
53
+ script_safe: true)
54
+ end
55
+
56
+ # `[[{ 'queue' => q, 'class' => c }, count], …]` from an HGETALL reply
57
+ # (a Hash under RESP3, a flat Array under RESP2), sorted by labels. Two
58
+ # fields that decode to the same labels (escaped differently by another
59
+ # writer) are summed.
60
+ def samples(reply)
61
+ pairs = reply.is_a?(::Hash) ? reply : Array(reply).each_slice(2)
62
+ totals = ::Hash.new(0)
63
+ pairs.each { |raw, count| totals[labels(raw)] += count.to_i }
64
+ totals.sort_by { |labels, _| labels.values }
65
+ end
66
+
67
+ private
68
+
69
+ def parse(payload)
70
+ ::JSON.parse(payload)
71
+ rescue ::JSON::ParserError
72
+ nil
73
+ end
74
+
75
+ def label(value)
76
+ value if value.is_a?(::String) && !value.empty?
77
+ end
78
+
79
+ def labels(raw)
80
+ queue, klass = parse(raw)
81
+ { 'queue' => label(queue) || UNKNOWN, 'class' => label(klass) || UNKNOWN }
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -6,11 +6,13 @@ require_relative '../keys'
6
6
  require_relative '../version'
7
7
  require_relative '../queue_consumers'
8
8
  require_relative 'process_counters'
9
+ require_relative 'deaths'
9
10
 
10
11
  module Wurk
11
12
  module Metrics
12
13
  # Prometheus text exposition (format 0.0.4) served by the health listener at
13
- # `GET /metrics`. Reads only keys Sidekiq already writes — no new Redis keys.
14
+ # `GET /metrics`. Reads keys Sidekiq already writes, plus Wurk's own death
15
+ # counter (`stat:deaths`, Metrics::Deaths); it writes none.
14
16
  #
15
17
  # Two families, and the difference matters when aggregating:
16
18
  # * cluster gauges/counters (`wurk_queue_*`, `wurk_processed_total`, …)
@@ -57,7 +59,7 @@ module Wurk
57
59
  # scheduler is behind.
58
60
  SET_HEADS = Scheduled::SETS.map { |set| ['ZRANGE', set, 0, 0, 'WITHSCORES'] }.freeze
59
61
  # Names come last: the detail pass is built from them.
60
- FIRST_PASS = [*SCALARS.values, *SET_HEADS, ['GET', Wurk::Leader::DEFAULT_KEY],
62
+ FIRST_PASS = [*SCALARS.values, *SET_HEADS, ['GET', Wurk::Leader::DEFAULT_KEY], ['HGETALL', Keys::STAT_DEATHS],
61
63
  ['SMEMBERS', Keys::QUEUES_SET], ['SMEMBERS', Keys::PROCESSES]].freeze
62
64
 
63
65
  QUEUE_GAUGES = [
@@ -98,7 +100,7 @@ module Wurk
98
100
  ProcessRow = Struct.new(:identity, :pid, :busy, :concurrency, :beat, :rss_kb, :quiet, :queues, :rtt_us,
99
101
  :oldest_run_at)
100
102
  Snapshot = Struct.new(:ok, :taken_at, :processed, :failed, :scheduled, :retries, :dead, :queues, :processes,
101
- :leader, :set_heads, keyword_init: true)
103
+ :leader, :set_heads, :deaths, keyword_init: true)
102
104
 
103
105
  def initialize(config, ttl: TTL, nonce: Component::PROCESS_NONCE, hostname: Component.hostname,
104
106
  clock: -> { ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) })
@@ -139,6 +141,7 @@ module Wurk
139
141
  ProcessFamilies.render(out, swarm: swarm)
140
142
  if snap.ok
141
143
  cluster(out, snap)
144
+ family(out, *Deaths::FAMILY, snap.deaths)
142
145
  local(out, snap, expected_children, fresh_window)
143
146
  end
144
147
  out
@@ -155,7 +158,7 @@ module Wurk
155
158
  queues, processes = names(first)
156
159
  rows = process_rows(processes, detail.drop(queues.size * 2), local_identities(processes))
157
160
  Snapshot.new(ok: true, taken_at: now, **scalars(first), leader: first[SCALARS.size + SET_HEADS.size],
158
- set_heads: heads_of_sets(first),
161
+ set_heads: heads_of_sets(first), deaths: Deaths.samples(first[SCALARS.size + SET_HEADS.size + 1]),
159
162
  queues: queue_rows(queues, detail, rows), processes: rows)
160
163
  rescue StandardError => e
161
164
  @config.logger&.warn { "Wurk::Metrics::Prometheus refresh failed: #{e.class}: #{e.message}" }
@@ -4,6 +4,7 @@ require_relative 'component'
4
4
  require_relative 'keys'
5
5
  require_relative 'lua'
6
6
  require_relative 'lua/loader'
7
+ require_relative 'metrics/deaths'
7
8
  require_relative 'client'
8
9
  require_relative 'process_set'
9
10
  require_relative 'timer_loop'
@@ -150,10 +151,12 @@ module Wurk
150
151
  def bury(jobstr, sset)
151
152
  # Two commands, no MULTI: a server that refuses the ZADD mid-transaction
152
153
  # (Dragonfly at maxmemory) still applies the ZREM, and the member is gone.
153
- # A crash between them costs one more trip through here.
154
+ # A crash between them costs one more trip through here. The count runs
155
+ # last and never raises, so a replay of the block cannot count twice.
154
156
  @config.redis(idempotent: true) do |conn|
155
157
  conn.call('ZADD', Keys::DEAD, real_time.to_s, jobstr)
156
158
  conn.call('ZREM', sset, jobstr)
159
+ Metrics::Deaths.record(conn, jobstr, config: @config)
157
160
  end
158
161
  rescue *TRANSIENT_ERRORS => e
159
162
  handle_exception(e, { context: 'scheduler_restore', set: sset })
@@ -238,8 +241,9 @@ module Wurk
238
241
  promoted = Wurk::Lua::Loader.eval_cached(
239
242
  conn,
240
243
  :reliable_schedule_promote,
241
- keys: [sset, Keys::QUEUES_SET, Keys::DEAD],
242
- argv: [real_time.to_s, Keys::QUEUE_PREFIX, real_ms.to_s, PROMOTE_BATCH.to_s]
244
+ keys: [sset, Keys::QUEUES_SET, Keys::DEAD, Keys::STAT_DEATHS],
245
+ argv: [real_time.to_s, Keys::QUEUE_PREFIX, real_ms.to_s, PROMOTE_BATCH.to_s,
246
+ Metrics::Deaths::MAX_SERIES.to_s, Metrics::Deaths::OVERFLOW]
243
247
  ).to_i
244
248
  Scheduled.record_promotions(sset, promoted)
245
249
  break if promoted < PROMOTE_BATCH || @done
data/lib/wurk/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wurk
4
- VERSION = '1.19.0'
4
+ VERSION = '1.20.0'
5
5
  end
@@ -1,4 +1,4 @@
1
1
  {
2
- "version": "1.19.0",
3
- "timestamp": "2026-11-23T15:33:33.692Z"
2
+ "version": "1.20.0",
3
+ "timestamp": "2026-11-24T02:15:30.375Z"
4
4
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wurk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.19.0
4
+ version: 1.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - developerz.ai
@@ -332,6 +332,7 @@ files:
332
332
  - lib/wurk/lua/batch_push.lua
333
333
  - lib/wurk/lua/batch_remove_jobs.lua
334
334
  - lib/wurk/lua/batch_schedule.lua
335
+ - lib/wurk/lua/count_death.lua
335
336
  - lib/wurk/lua/debounce.lua
336
337
  - lib/wurk/lua/fast_delete.rb
337
338
  - lib/wurk/lua/fast_delete_window.lua
@@ -370,6 +371,7 @@ files:
370
371
  - lib/wurk/metrics.rb
371
372
  - lib/wurk/metrics/accumulator.rb
372
373
  - lib/wurk/metrics/dashboard_series.rb
374
+ - lib/wurk/metrics/deaths.rb
373
375
  - lib/wurk/metrics/exposition.rb
374
376
  - lib/wurk/metrics/flusher.rb
375
377
  - lib/wurk/metrics/histogram.rb