Class: Wurk::Import::SidekiqCron

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/import/sidekiq_cron.rb

Overview

Reads the schedule sidekiq-cron keeps in Redis and maps each entry onto a native periodic loop (Wurk::Cron::Loop). rake wurk:import:cron drives it: a dry run prints the plan plus a config.periodic block to paste into code; APPLY=1 persists the loops straight into the periodic registry.

sidekiq-cron's layout (v1.12+ / 2.x, namespaced): cron_jobs:<namespace> is a SET of cron_job:<namespace>:<name> HASH keys. Releases before namespaces used one cron_jobs SET of cron_job:<name> keys; both are read. Nothing here writes or deletes a sidekiq-cron key, so a rollback to Sidekiq still finds its schedule intact.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

LEGACY_SET =
'cron_jobs'
NAMESPACED_SETS =
'cron_jobs:*'
GLOBALID_KEY =
'_sc_globalid'
TRUE_FLAG =
/\A(?:true|t|yes|y|1)\z/i
ZONE_TOKEN =
%r{\A[A-Za-z][A-Za-z0-9_+\-/]*\z}

Instance Method Summary collapse

Instance Method Details

#apply!(list = entries) ⇒ Object

Persists every importable entry and returns the entries it wrote. Idempotent: a loop's lid is a hash of its schedule, class and options, so a second run rewrites the same keys, and paused is only seeded (Cron.persist uses HSETNX), so a dashboard pause survives a re-import.



42
43
44
# File 'lib/wurk/import/sidekiq_cron.rb', line 42

def apply!(list = entries)
  list.select(&:importable?).each { |entry| Cron.persist(entry.loop) }
end

#entries ⇒ Object



30
31
32
33
34
35
36
# File 'lib/wurk/import/sidekiq_cron.rb', line 30

def entries
  rows = redis do |c|
    keys = schedule_keys(c)
    keys.map { |key| [key, c.call('HGETALL', key).to_h] }
  end
  rows.reject { |(_key, h)| h.empty? }.map { |key, h| build_entry(key, h) }
end

#registered?(lid) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/wurk/import/sidekiq_cron.rb', line 46

def registered?(lid)
  redis { |c| c.call('SISMEMBER', Cron::PERIODIC_KEY, lid) }.to_i == 1
end

#run(apply:, out: $stdout) ⇒ Object

What rake wurk:import:cron prints. Returns the entries it found.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wurk/import/sidekiq_cron.rb', line 51

def run(apply:, out: $stdout)
  list = entries
  if list.empty?
    out.puts "No sidekiq-cron entries found (looked for #{NAMESPACED_SETS} and #{LEGACY_SET})."
    return list
  end

  report(list, out)
  if apply
    written = apply!(list)
    out.puts "\nRegistered #{written.size} loop(s) in the `#{Cron::PERIODIC_KEY}` registry. " \
             'sidekiq-cron keys were left untouched.'
  else
    out.puts "\nDry run: nothing was written. Re-run with APPLY=1 to register the importable loops in Redis."
  end
  list
end