wurk 1.17.1 → 1.18.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 +4 -4
- data/README.md +1 -1
- data/lib/sidekiq/cron/web.rb +10 -0
- data/lib/sidekiq/cron.rb +10 -0
- data/lib/sidekiq-cron.rb +11 -0
- data/lib/wurk/boot_checks.rb +8 -6
- data/lib/wurk/compat.rb +19 -15
- data/lib/wurk/finished_job_cleanup.rb +3 -2
- data/lib/wurk/launcher.rb +15 -4
- data/lib/wurk/lua/unique_jobs_delete.lua +5 -0
- data/lib/wurk/lua/unique_jobs_delete_by_digest.lua +6 -0
- data/lib/wurk/lua/unique_jobs_delete_job_by_digest.lua +42 -0
- data/lib/wurk/lua/unique_jobs_lock.lua +54 -0
- data/lib/wurk/lua/unique_jobs_queue.lua +43 -0
- data/lib/wurk/lua/unique_jobs_queue_and_lock.lua +67 -0
- data/lib/wurk/lua/unique_jobs_unlock.lua +61 -0
- data/lib/wurk/preflight/checks.rb +6 -5
- data/lib/wurk/rspec.rb +246 -0
- data/lib/wurk/sidekiq_cron/arguments.rb +69 -0
- data/lib/wurk/sidekiq_cron/configuration.rb +43 -0
- data/lib/wurk/sidekiq_cron/job/enqueue.rb +142 -0
- data/lib/wurk/sidekiq_cron/job/store.rb +130 -0
- data/lib/wurk/sidekiq_cron/job.rb +292 -0
- data/lib/wurk/sidekiq_cron/namespace.rb +52 -0
- data/lib/wurk/sidekiq_cron/poller.rb +71 -0
- data/lib/wurk/sidekiq_cron/schedule.rb +114 -0
- data/lib/wurk/sidekiq_cron/schedule_loader.rb +50 -0
- data/lib/wurk/sidekiq_cron/web_rows.rb +55 -0
- data/lib/wurk/sidekiq_cron.rb +52 -0
- data/lib/wurk/unique_jobs/api_extension.rb +84 -0
- data/lib/wurk/unique_jobs/config.rb +75 -0
- data/lib/wurk/unique_jobs/constants.rb +53 -0
- data/lib/wurk/unique_jobs/digests.rb +169 -0
- data/lib/wurk/unique_jobs/errors.rb +71 -0
- data/lib/wurk/unique_jobs/job.rb +226 -0
- data/lib/wurk/unique_jobs/lock.rb +345 -0
- data/lib/wurk/unique_jobs/locksmith.rb +240 -0
- data/lib/wurk/unique_jobs/middleware.rb +109 -0
- data/lib/wurk/unique_jobs/on_conflict.rb +107 -0
- data/lib/wurk/unique_jobs/orphans.rb +258 -0
- data/lib/wurk/unique_jobs/reflections.rb +72 -0
- data/lib/wurk/unique_jobs/require/sidekiq-unique-jobs.rb +6 -0
- data/lib/wurk/unique_jobs/require/sidekiq_unique_jobs/web.rb +8 -0
- data/lib/wurk/unique_jobs/require/sidekiq_unique_jobs.rb +5 -0
- data/lib/wurk/unique_jobs/sidekiq_unique_jobs.rb +15 -0
- data/lib/wurk/unique_jobs/web/views/_paging.erb +10 -0
- data/lib/wurk/unique_jobs/web/views/changelogs.erb +62 -0
- data/lib/wurk/unique_jobs/web/views/lock.erb +110 -0
- data/lib/wurk/unique_jobs/web/views/locks.erb +61 -0
- data/lib/wurk/unique_jobs/web.rb +150 -0
- data/lib/wurk/unique_jobs.rb +142 -0
- data/lib/wurk/unique_jobs_compat.rb +44 -3
- data/lib/wurk/version.rb +1 -1
- data/lib/wurk/web/dashboard/api_actions.rb +1 -2
- data/lib/wurk/web/enterprise.rb +23 -0
- data/lib/wurk.rb +3 -1
- data/vendor/assets/dashboard/assets/{ArgsValue-s6SvnYnn.js → ArgsValue-8zbIUK63.js} +1 -1
- data/vendor/assets/dashboard/assets/ArgsValue-8zbIUK63.js.br +0 -0
- data/vendor/assets/dashboard/assets/ArgsValue-8zbIUK63.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{BatchDetail-CprIJHVF.js → BatchDetail-B7FnhWOm.js} +1 -1
- data/vendor/assets/dashboard/assets/BatchDetail-B7FnhWOm.js.br +0 -0
- data/vendor/assets/dashboard/assets/BatchDetail-B7FnhWOm.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Batches-_UtmkdcL.js → Batches-KBN4rVe3.js} +1 -1
- data/vendor/assets/dashboard/assets/Batches-KBN4rVe3.js.br +0 -0
- data/vendor/assets/dashboard/assets/Batches-KBN4rVe3.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Busy-CxUt_KaT.js → Busy-Gz7qEa1m.js} +1 -1
- data/vendor/assets/dashboard/assets/Busy-Gz7qEa1m.js.br +0 -0
- data/vendor/assets/dashboard/assets/Busy-Gz7qEa1m.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Cron-B8esPvHL.js +1 -0
- data/vendor/assets/dashboard/assets/Cron-B8esPvHL.js.br +0 -0
- data/vendor/assets/dashboard/assets/Cron-B8esPvHL.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Dashboard-BpeXrgWN.js → Dashboard-BfVW4Db7.js} +1 -1
- data/vendor/assets/dashboard/assets/Dashboard-BfVW4Db7.js.br +0 -0
- data/vendor/assets/dashboard/assets/Dashboard-BfVW4Db7.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Dead-DxG8m9Ea.js → Dead-i929U3Al.js} +1 -1
- data/vendor/assets/dashboard/assets/Dead-i929U3Al.js.br +0 -0
- data/vendor/assets/dashboard/assets/Dead-i929U3Al.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Extension-Bdw1AWK9.js → Extension-DtT_BQGI.js} +1 -1
- data/vendor/assets/dashboard/assets/Extension-DtT_BQGI.js.br +0 -0
- data/vendor/assets/dashboard/assets/Extension-DtT_BQGI.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{FlowDetail-BbLiOUVq.js → FlowDetail-dSxWLJwW.js} +1 -1
- data/vendor/assets/dashboard/assets/FlowDetail-dSxWLJwW.js.br +0 -0
- data/vendor/assets/dashboard/assets/FlowDetail-dSxWLJwW.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Flows-Cdh5HxbD.js → Flows-DBXdrnl_.js} +1 -1
- data/vendor/assets/dashboard/assets/Flows-DBXdrnl_.js.br +0 -0
- data/vendor/assets/dashboard/assets/Flows-DBXdrnl_.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{JobDetailModal-DyrUaaGZ.js → JobDetailModal-wz97ZwuO.js} +1 -1
- data/vendor/assets/dashboard/assets/JobDetailModal-wz97ZwuO.js.br +0 -0
- data/vendor/assets/dashboard/assets/JobDetailModal-wz97ZwuO.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Limiters-D7r6cfSD.js → Limiters-LLzkwl5z.js} +1 -1
- data/vendor/assets/dashboard/assets/Limiters-LLzkwl5z.js.br +0 -0
- data/vendor/assets/dashboard/assets/Limiters-LLzkwl5z.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Metrics-BLenaRy4.js → Metrics-C0ilxg59.js} +1 -1
- data/vendor/assets/dashboard/assets/Metrics-C0ilxg59.js.br +0 -0
- data/vendor/assets/dashboard/assets/Metrics-C0ilxg59.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{NotFound-P0HnKG_i.js → NotFound-CHPpvK-n.js} +1 -1
- data/vendor/assets/dashboard/assets/NotFound-CHPpvK-n.js.br +0 -0
- data/vendor/assets/dashboard/assets/NotFound-CHPpvK-n.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{PageHeader-Dr0x5uFD.js → PageHeader-CGep_jFP.js} +1 -1
- data/vendor/assets/dashboard/assets/PageHeader-CGep_jFP.js.br +0 -0
- data/vendor/assets/dashboard/assets/PageHeader-CGep_jFP.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Profiles-DwBcuGsp.js → Profiles-D_59KJVX.js} +1 -1
- data/vendor/assets/dashboard/assets/Profiles-D_59KJVX.js.br +0 -0
- data/vendor/assets/dashboard/assets/Profiles-D_59KJVX.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Queues-BpB_QfMT.js → Queues-B84cTtnr.js} +1 -1
- data/vendor/assets/dashboard/assets/Queues-B84cTtnr.js.br +0 -0
- data/vendor/assets/dashboard/assets/Queues-B84cTtnr.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{RefreshError-CFF9cD72.js → RefreshError-LAOkINDS.js} +1 -1
- data/vendor/assets/dashboard/assets/RefreshError-LAOkINDS.js.br +2 -0
- data/vendor/assets/dashboard/assets/RefreshError-LAOkINDS.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Retries-CvpBa3nA.js → Retries-sdTIe5PU.js} +1 -1
- data/vendor/assets/dashboard/assets/Retries-sdTIe5PU.js.br +0 -0
- data/vendor/assets/dashboard/assets/Retries-sdTIe5PU.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Scheduled-B5_rNwmD.js → Scheduled-mWorUG81.js} +1 -1
- data/vendor/assets/dashboard/assets/Scheduled-mWorUG81.js.br +0 -0
- data/vendor/assets/dashboard/assets/Scheduled-mWorUG81.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{Search-DBiXXQ1X.js → Search-DQg2ky9F.js} +1 -1
- data/vendor/assets/dashboard/assets/Search-DQg2ky9F.js.br +0 -0
- data/vendor/assets/dashboard/assets/Search-DQg2ky9F.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{charts-BowrQgy3.js → charts-D2ZSzfcT.js} +1 -1
- data/vendor/assets/dashboard/assets/charts-D2ZSzfcT.js.br +0 -0
- data/vendor/assets/dashboard/assets/charts-D2ZSzfcT.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{index-lb7Hct36.js → index-CYxK_mMH.js} +2 -2
- data/vendor/assets/dashboard/assets/index-CYxK_mMH.js.br +0 -0
- data/vendor/assets/dashboard/assets/index-CYxK_mMH.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{useJobSetList-DOJEPjtn.js → useJobSetList-Bq8gXURX.js} +1 -1
- data/vendor/assets/dashboard/assets/useJobSetList-Bq8gXURX.js.br +0 -0
- data/vendor/assets/dashboard/assets/useJobSetList-Bq8gXURX.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{useResetPageOnEmpty-BkhAVjcM.js → useResetPageOnEmpty-DBe-fKXL.js} +1 -1
- data/vendor/assets/dashboard/assets/useResetPageOnEmpty-DBe-fKXL.js.br +0 -0
- data/vendor/assets/dashboard/assets/useResetPageOnEmpty-DBe-fKXL.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{useSort-BH4O54LC.js → useSort-CB33x7jI.js} +1 -1
- data/vendor/assets/dashboard/assets/useSort-CB33x7jI.js.br +2 -0
- data/vendor/assets/dashboard/assets/useSort-CB33x7jI.js.gz +0 -0
- data/vendor/assets/dashboard/assets/{utils-5e3YBxoh.js → utils-DvEdG1B-.js} +1 -1
- data/vendor/assets/dashboard/assets/utils-DvEdG1B-.js.br +0 -0
- data/vendor/assets/dashboard/assets/utils-DvEdG1B-.js.gz +0 -0
- data/vendor/assets/dashboard/index.html +1 -1
- data/vendor/assets/dashboard/wurk-manifest.json +2 -2
- metadata +126 -82
- data/vendor/assets/dashboard/assets/ArgsValue-s6SvnYnn.js.br +0 -0
- data/vendor/assets/dashboard/assets/ArgsValue-s6SvnYnn.js.gz +0 -0
- data/vendor/assets/dashboard/assets/BatchDetail-CprIJHVF.js.br +0 -0
- data/vendor/assets/dashboard/assets/BatchDetail-CprIJHVF.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Batches-_UtmkdcL.js.br +0 -0
- data/vendor/assets/dashboard/assets/Batches-_UtmkdcL.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Busy-CxUt_KaT.js.br +0 -0
- data/vendor/assets/dashboard/assets/Busy-CxUt_KaT.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Cron-BO4CLzcL.js +0 -1
- data/vendor/assets/dashboard/assets/Cron-BO4CLzcL.js.br +0 -0
- data/vendor/assets/dashboard/assets/Cron-BO4CLzcL.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Dashboard-BpeXrgWN.js.br +0 -0
- data/vendor/assets/dashboard/assets/Dashboard-BpeXrgWN.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Dead-DxG8m9Ea.js.br +0 -0
- data/vendor/assets/dashboard/assets/Dead-DxG8m9Ea.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Extension-Bdw1AWK9.js.br +0 -0
- data/vendor/assets/dashboard/assets/Extension-Bdw1AWK9.js.gz +0 -0
- data/vendor/assets/dashboard/assets/FlowDetail-BbLiOUVq.js.br +0 -0
- data/vendor/assets/dashboard/assets/FlowDetail-BbLiOUVq.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Flows-Cdh5HxbD.js.br +0 -0
- data/vendor/assets/dashboard/assets/Flows-Cdh5HxbD.js.gz +0 -0
- data/vendor/assets/dashboard/assets/JobDetailModal-DyrUaaGZ.js.br +0 -0
- data/vendor/assets/dashboard/assets/JobDetailModal-DyrUaaGZ.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Limiters-D7r6cfSD.js.br +0 -0
- data/vendor/assets/dashboard/assets/Limiters-D7r6cfSD.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Metrics-BLenaRy4.js.br +0 -0
- data/vendor/assets/dashboard/assets/Metrics-BLenaRy4.js.gz +0 -0
- data/vendor/assets/dashboard/assets/NotFound-P0HnKG_i.js.br +0 -0
- data/vendor/assets/dashboard/assets/NotFound-P0HnKG_i.js.gz +0 -0
- data/vendor/assets/dashboard/assets/PageHeader-Dr0x5uFD.js.br +0 -0
- data/vendor/assets/dashboard/assets/PageHeader-Dr0x5uFD.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Profiles-DwBcuGsp.js.br +0 -0
- data/vendor/assets/dashboard/assets/Profiles-DwBcuGsp.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Queues-BpB_QfMT.js.br +0 -0
- data/vendor/assets/dashboard/assets/Queues-BpB_QfMT.js.gz +0 -0
- data/vendor/assets/dashboard/assets/RefreshError-CFF9cD72.js.br +0 -0
- data/vendor/assets/dashboard/assets/RefreshError-CFF9cD72.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Retries-CvpBa3nA.js.br +0 -0
- data/vendor/assets/dashboard/assets/Retries-CvpBa3nA.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Scheduled-B5_rNwmD.js.br +0 -0
- data/vendor/assets/dashboard/assets/Scheduled-B5_rNwmD.js.gz +0 -0
- data/vendor/assets/dashboard/assets/Search-DBiXXQ1X.js.br +0 -0
- data/vendor/assets/dashboard/assets/Search-DBiXXQ1X.js.gz +0 -0
- data/vendor/assets/dashboard/assets/charts-BowrQgy3.js.br +0 -0
- data/vendor/assets/dashboard/assets/charts-BowrQgy3.js.gz +0 -0
- data/vendor/assets/dashboard/assets/index-lb7Hct36.js.br +0 -0
- data/vendor/assets/dashboard/assets/index-lb7Hct36.js.gz +0 -0
- data/vendor/assets/dashboard/assets/useJobSetList-DOJEPjtn.js.br +0 -0
- data/vendor/assets/dashboard/assets/useJobSetList-DOJEPjtn.js.gz +0 -0
- data/vendor/assets/dashboard/assets/useResetPageOnEmpty-BkhAVjcM.js.br +0 -0
- data/vendor/assets/dashboard/assets/useResetPageOnEmpty-BkhAVjcM.js.gz +0 -0
- data/vendor/assets/dashboard/assets/useSort-BH4O54LC.js.br +0 -0
- data/vendor/assets/dashboard/assets/useSort-BH4O54LC.js.gz +0 -0
- data/vendor/assets/dashboard/assets/utils-5e3YBxoh.js.br +0 -0
- data/vendor/assets/dashboard/assets/utils-5e3YBxoh.js.gz +0 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require 'time'
|
|
6
|
+
require_relative 'arguments'
|
|
7
|
+
require_relative 'schedule'
|
|
8
|
+
require_relative 'job/store'
|
|
9
|
+
require_relative 'job/enqueue'
|
|
10
|
+
|
|
11
|
+
module Wurk
|
|
12
|
+
module SidekiqCron
|
|
13
|
+
# `Sidekiq::Cron::Job`, native. One named cron entry, stored where and how
|
|
14
|
+
# sidekiq-cron 2.3/2.4 stores it, so a fleet can run the gem and this side
|
|
15
|
+
# by side, or swap one for the other, with no data migration:
|
|
16
|
+
#
|
|
17
|
+
# cron_jobs:<namespace> SET of the job hash keys
|
|
18
|
+
# cron_job:<namespace>:<name> HASH, the `to_hash` fields below
|
|
19
|
+
# cron_job:<namespace>:<name>:enqueued ZSET, one member per fired occurrence
|
|
20
|
+
# cron_job:<namespace>:<name>:jid_history LIST, newest first
|
|
21
|
+
#
|
|
22
|
+
# State is keyed by name: `status` lives in the hash and only #enable! and
|
|
23
|
+
# #disable! (or a `status` key in the loaded data) change it, so editing a
|
|
24
|
+
# schedule keeps a hand-disabled job disabled.
|
|
25
|
+
#
|
|
26
|
+
# The `:enqueued` ZADD is the exactly-once gate, and the reason a gem pod and
|
|
27
|
+
# a native pod never both fire one occurrence: both add the same member (the
|
|
28
|
+
# occurrence as ISO-8601 UTC) and only the one that added it pushes.
|
|
29
|
+
class Job
|
|
30
|
+
extend Store
|
|
31
|
+
include Enqueue
|
|
32
|
+
|
|
33
|
+
JOBS_PREFIX = 'cron_jobs:'
|
|
34
|
+
JOB_PREFIX = 'cron_job:'
|
|
35
|
+
LEGACY_JOBS_KEY = 'cron_jobs'
|
|
36
|
+
REMEMBER_THRESHOLD = 24 * 60 * 60
|
|
37
|
+
LAST_ENQUEUE_TIME_FORMAT = '%Y-%m-%d %H:%M:%S %z'
|
|
38
|
+
GLOBALID_KEY = Arguments::GLOBALID_KEY
|
|
39
|
+
TRUE_FLAG = /\A(true|t|yes|y|1)\z/i
|
|
40
|
+
|
|
41
|
+
attr_accessor :name, :namespace, :cron, :description, :klass, :message
|
|
42
|
+
attr_reader :last_enqueue_time, :fetch_missing_args, :source, :args, :status
|
|
43
|
+
|
|
44
|
+
def initialize(input_args = {})
|
|
45
|
+
args = input_args.to_h.transform_keys(&:to_s)
|
|
46
|
+
@fetch_missing_args = args.delete('fetch_missing_args')
|
|
47
|
+
@fetch_missing_args = true if @fetch_missing_args.nil?
|
|
48
|
+
assign_identity(args)
|
|
49
|
+
assign_options(args)
|
|
50
|
+
build_message(args)
|
|
51
|
+
@queue_name_with_prefix = queue_name_with_prefix
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# --- state -----------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
def enable!
|
|
57
|
+
@status = 'enabled'
|
|
58
|
+
save
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def disable!
|
|
62
|
+
@status = 'disabled'
|
|
63
|
+
save
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def enabled? = @status == 'enabled'
|
|
67
|
+
|
|
68
|
+
def disabled? = !enabled?
|
|
69
|
+
|
|
70
|
+
def errors
|
|
71
|
+
@errors ||= []
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def valid? # rubocop:disable Metrics/AbcSize -- the gem's checks, one line each
|
|
75
|
+
@errors = []
|
|
76
|
+
errors << "'name' must be set" if @name.nil? || @name.empty?
|
|
77
|
+
errors << "'namespace' must be set" if @namespace.nil? || @namespace.empty?
|
|
78
|
+
errors << "'namespace' cannot be '*'" if @namespace == '*'
|
|
79
|
+
validate_cron
|
|
80
|
+
errors << "'klass' (or class) must be set" unless klass_valid
|
|
81
|
+
errors.empty?
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def klass_valid
|
|
85
|
+
@klass.is_a?(::Class) || (@klass.is_a?(::String) && !@klass.empty?)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The gem's hash, field for field: a gem pod reads what this writes.
|
|
89
|
+
def to_hash
|
|
90
|
+
{
|
|
91
|
+
name: @name, namespace: @namespace, klass: @klass.to_s, cron: @cron, description: @description,
|
|
92
|
+
source: @source, args: @args.is_a?(::String) ? @args : ::JSON.generate(@args || []),
|
|
93
|
+
date_as_argument: date_as_argument? ? '1' : '0',
|
|
94
|
+
message: @message.is_a?(::String) ? @message : ::JSON.generate(@message || {}),
|
|
95
|
+
status: @status, active_job: @active_job ? '1' : '0',
|
|
96
|
+
queue_name_prefix: @active_job_queue_name_prefix, queue_name_delimiter: @active_job_queue_name_delimiter,
|
|
97
|
+
retry: @retry.nil? || @retry.is_a?(::Numeric) ? @retry : @retry.to_s,
|
|
98
|
+
last_enqueue_time: serialized_last_enqueue_time, symbolize_args: symbolize_args? ? '1' : '0'
|
|
99
|
+
}
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Saving an entry the first time also marks its latest past occurrence as
|
|
103
|
+
# fired, so a job added a minute after its slot does not run at once.
|
|
104
|
+
def save # rubocop:disable Naming/PredicateMethod -- the gem's name; true or false as there
|
|
105
|
+
return false unless valid?
|
|
106
|
+
|
|
107
|
+
fields = to_hash.transform_values { |value| value || '' }.flatten
|
|
108
|
+
redis do |conn|
|
|
109
|
+
conn.sadd(self.class.jobs_key(@namespace), [redis_key])
|
|
110
|
+
conn.hset(redis_key, *fields)
|
|
111
|
+
mark_created(conn) unless [true, 1].include?(conn.exists(job_enqueued_key))
|
|
112
|
+
end
|
|
113
|
+
true
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def destroy # rubocop:disable Naming/PredicateMethod -- the gem's name
|
|
117
|
+
redis do |conn|
|
|
118
|
+
conn.srem(self.class.jobs_key(@namespace), [redis_key])
|
|
119
|
+
conn.del(job_enqueued_key)
|
|
120
|
+
conn.del(jid_history_key)
|
|
121
|
+
conn.del(redis_key)
|
|
122
|
+
end
|
|
123
|
+
::Sidekiq.logger.info { "Cron Jobs - deleted job with name #{@name} from namespace #{@namespace}" }
|
|
124
|
+
true
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def exists? = self.class.exists?(@name, @namespace)
|
|
128
|
+
|
|
129
|
+
def status_from_redis
|
|
130
|
+
return 'enabled' unless fetch_missing_args
|
|
131
|
+
|
|
132
|
+
redis { |conn| conn.hget(redis_key, 'status') } || 'enabled'
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def last_enqueue_time_from_redis
|
|
136
|
+
return nil unless fetch_missing_args
|
|
137
|
+
|
|
138
|
+
parse_enqueue_time(redis { |conn| conn.hget(redis_key, 'last_enqueue_time') })
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# --- schedule --------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
def schedule
|
|
144
|
+
@schedule = [@cron, Schedule.new(@cron)] unless @schedule&.first == @cron
|
|
145
|
+
@schedule.last
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# The occurrence a poll at `now` would fire (fugit's `previous_time`).
|
|
149
|
+
# Looks back as far as a save needs; the poller asks only for the grace
|
|
150
|
+
# window (#due_slot).
|
|
151
|
+
def last_time(now = ::Time.now.utc)
|
|
152
|
+
schedule.previous_time(now, Schedule::CREATION_LOOKBACK)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def formatted_enqueue_time(now = ::Time.now.utc) = last_time(now).to_f.to_s
|
|
156
|
+
|
|
157
|
+
def formatted_last_time(now = ::Time.now.utc) = last_time(now).iso8601
|
|
158
|
+
|
|
159
|
+
def human_cron = @cron
|
|
160
|
+
|
|
161
|
+
def cron_expression_string = [schedule.crontab, schedule.zone_name].compact.join(' ')
|
|
162
|
+
|
|
163
|
+
def pretty_message
|
|
164
|
+
::JSON.pretty_generate(@message.is_a?(::String) ? ::JSON.parse(@message) : @message)
|
|
165
|
+
rescue ::JSON::ParserError
|
|
166
|
+
@message
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def sort_name = "#{enabled? ? 0 : 1}_#{name}".downcase
|
|
170
|
+
|
|
171
|
+
def args=(args)
|
|
172
|
+
@args = Arguments.parse(args, @symbolize_args)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def redis_key = self.class.redis_key(@name, @namespace)
|
|
176
|
+
|
|
177
|
+
def job_enqueued_key = self.class.job_enqueued_key(@name, @namespace)
|
|
178
|
+
|
|
179
|
+
def jid_history_key = self.class.jid_history_key(@name, @namespace)
|
|
180
|
+
|
|
181
|
+
private
|
|
182
|
+
|
|
183
|
+
def redis(&) = ::Sidekiq.redis(&)
|
|
184
|
+
|
|
185
|
+
def flag?(value) = value == true || TRUE_FLAG.match?(value.to_s)
|
|
186
|
+
|
|
187
|
+
# Status and last enqueue time come from Redis when the data has none,
|
|
188
|
+
# which is how a reload keeps a hand-set status.
|
|
189
|
+
def assign_identity(args)
|
|
190
|
+
@name = args['name']
|
|
191
|
+
@cron = args['cron']
|
|
192
|
+
@description = args['description'] if args['description']
|
|
193
|
+
@source = args['source'] == 'schedule' ? 'schedule' : 'dynamic'
|
|
194
|
+
@namespace = resolve_namespace(args['namespace'])
|
|
195
|
+
@klass = args['klass'] || args['class']
|
|
196
|
+
@status = args['status'] || status_from_redis
|
|
197
|
+
@last_enqueue_time = initial_last_enqueue_time(args['last_enqueue_time'])
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def assign_options(args)
|
|
201
|
+
@symbolize_args = flag?(args['symbolize_args'])
|
|
202
|
+
@args = Arguments.parse(args['args'], @symbolize_args)
|
|
203
|
+
@date_as_argument = flag?(args['date_as_argument'])
|
|
204
|
+
@active_job = flag?(args['active_job'])
|
|
205
|
+
@active_job_queue_name_prefix = args['queue_name_prefix']
|
|
206
|
+
@active_job_queue_name_delimiter = args['queue_name_delimiter']
|
|
207
|
+
warn_symbolize_args if @symbolize_args && !@active_job
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def resolve_namespace(requested)
|
|
211
|
+
default = SidekiqCron.configuration.default_namespace
|
|
212
|
+
namespace = requested || default
|
|
213
|
+
return namespace unless Namespace.available_namespaces_provided? && !Namespace.all.include?(namespace)
|
|
214
|
+
|
|
215
|
+
::Sidekiq.logger.warn do
|
|
216
|
+
"Cron Jobs - unexpected namespace #{namespace} encountered. Assigning to default namespace."
|
|
217
|
+
end
|
|
218
|
+
default
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def initial_last_enqueue_time(given)
|
|
222
|
+
return parse_enqueue_time(given) if given.is_a?(::String) && !given.empty?
|
|
223
|
+
|
|
224
|
+
last_enqueue_time_from_redis
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def warn_symbolize_args
|
|
228
|
+
::Sidekiq.logger.warn do
|
|
229
|
+
"Cron Jobs - 'symbolize_args' is gonna be ignored, as it is only used when 'active_job' is true"
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# `message` comes back from Redis as the JSON the gem stored; built here
|
|
234
|
+
# it is the class's sidekiq_options with the entry's queue/retry on top.
|
|
235
|
+
def build_message(args) # rubocop:disable Metrics/AbcSize -- the gem's precedence, kept in one place
|
|
236
|
+
if args['message']
|
|
237
|
+
@message = args['message']
|
|
238
|
+
data = ::JSON.parse(@message) || {}
|
|
239
|
+
@queue = data['queue'] || 'default'
|
|
240
|
+
@retry = data['retry']
|
|
241
|
+
elsif @klass
|
|
242
|
+
data = job_options(@klass, @args).merge('class' => @klass.to_s, 'args' => @args)
|
|
243
|
+
@queue = data['queue'] = args['queue'] || data['queue'] || 'default'
|
|
244
|
+
data['retry'] = args['retry'] unless args['retry'].nil?
|
|
245
|
+
@retry = data['retry']
|
|
246
|
+
@message = data
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def job_options(klass, args)
|
|
251
|
+
klass = constantize(klass) unless klass.is_a?(::Class)
|
|
252
|
+
return { 'queue' => 'default' } if klass.nil?
|
|
253
|
+
return { 'queue' => klass.new(args).queue_name } if is_active_job?(klass)
|
|
254
|
+
|
|
255
|
+
klass.get_sidekiq_options.dup
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def constantize(name)
|
|
259
|
+
::Object.const_get(name)
|
|
260
|
+
rescue ::NameError
|
|
261
|
+
nil
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def validate_cron
|
|
265
|
+
return errors << "'cron' must be set" if @cron.nil? || @cron.empty?
|
|
266
|
+
|
|
267
|
+
schedule
|
|
268
|
+
rescue ArgumentError => e
|
|
269
|
+
errors << "'cron' -> #{@cron.inspect} -> #{e.class}: #{e.message}"
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def mark_created(conn)
|
|
273
|
+
now = ::Time.now.utc
|
|
274
|
+
slot = last_time(now)
|
|
275
|
+
conn.zadd(job_enqueued_key, now.to_f.to_s, slot.iso8601) if slot
|
|
276
|
+
::Sidekiq.logger.info { "Cron Jobs - added job with name #{@name} in the namespace #{@namespace}" }
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def serialized_last_enqueue_time
|
|
280
|
+
@last_enqueue_time&.strftime(LAST_ENQUEUE_TIME_FORMAT)
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def parse_enqueue_time(timestamp)
|
|
284
|
+
return nil if timestamp.nil? || timestamp.empty?
|
|
285
|
+
|
|
286
|
+
::Time.strptime(timestamp, LAST_ENQUEUE_TIME_FORMAT).utc
|
|
287
|
+
rescue ArgumentError
|
|
288
|
+
::Time.parse(timestamp).utc
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wurk
|
|
4
|
+
module SidekiqCron
|
|
5
|
+
# `Sidekiq::Cron::Namespace`: which `cron_jobs:<namespace>` sets exist.
|
|
6
|
+
# `available_namespaces` is the explicit list, `:auto` reads Redis, nil
|
|
7
|
+
# means only the default.
|
|
8
|
+
class Namespace
|
|
9
|
+
class << self
|
|
10
|
+
def all
|
|
11
|
+
configured = SidekiqCron.configuration.available_namespaces
|
|
12
|
+
names = case configured
|
|
13
|
+
when nil then []
|
|
14
|
+
when ::Array then configured
|
|
15
|
+
when :auto then discovered
|
|
16
|
+
else raise ArgumentError, "Unexpected value for `available_namespaces`: #{configured.inspect}"
|
|
17
|
+
end
|
|
18
|
+
names | [SidekiqCron.configuration.default_namespace]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def all_with_count
|
|
22
|
+
all.map { |name| { count: count(name), name: name } }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def count(name = SidekiqCron.configuration.default_namespace)
|
|
26
|
+
::Sidekiq.redis { |conn| conn.scard("#{Job::JOBS_PREFIX}#{name}") }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def available_namespaces_provided?
|
|
30
|
+
configured = SidekiqCron.configuration.available_namespaces
|
|
31
|
+
!configured.nil? && configured != :auto
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
# SCAN, not the gem's KEYS: the same answer without blocking the server.
|
|
37
|
+
def discovered
|
|
38
|
+
found = []
|
|
39
|
+
cursor = '0'
|
|
40
|
+
::Sidekiq.redis do |conn|
|
|
41
|
+
loop do
|
|
42
|
+
cursor, keys = conn.call('SCAN', cursor, 'MATCH', "#{Job::JOBS_PREFIX}*", 'COUNT', 1000)
|
|
43
|
+
found.concat(keys)
|
|
44
|
+
break if cursor == '0'
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
found.uniq.map { |key| key.delete_prefix(Job::JOBS_PREFIX) }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../component'
|
|
4
|
+
require_relative '../timer_loop'
|
|
5
|
+
|
|
6
|
+
module Wurk
|
|
7
|
+
module SidekiqCron
|
|
8
|
+
# Fires due sidekiq-cron entries every `cron_poll_interval` seconds. The
|
|
9
|
+
# Launcher runs one in every process, and only the cluster leader's ticks
|
|
10
|
+
# do anything (`dear-leader`, as the periodic loops).
|
|
11
|
+
#
|
|
12
|
+
# The leader is the cheap half of exactly-once, not the guarantee: a leader
|
|
13
|
+
# handover, or a sidekiq-cron gem pod still polling during a rollout, can
|
|
14
|
+
# reach the same occurrence, and each entry's `:enqueued` ZADD lets exactly
|
|
15
|
+
# one of them push it (Job#should_enqueue?).
|
|
16
|
+
class Poller
|
|
17
|
+
include Component
|
|
18
|
+
|
|
19
|
+
def initialize(config)
|
|
20
|
+
@config = config
|
|
21
|
+
@timer = TimerLoop.new(SidekiqCron.configuration.cron_poll_interval.to_i)
|
|
22
|
+
@thread = nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def start
|
|
26
|
+
return @thread if @thread
|
|
27
|
+
|
|
28
|
+
@timer.reset
|
|
29
|
+
@thread = safe_thread('sidekiq-cron-poller') do
|
|
30
|
+
migrate
|
|
31
|
+
@timer.run { tick }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Cleared only on a confirmed join, as the other leader-gated loops: a
|
|
36
|
+
# tick still in flight would push next to the next leader's first one.
|
|
37
|
+
def terminate
|
|
38
|
+
@timer.terminate
|
|
39
|
+
@thread = nil if @thread&.join(TimerLoop::JOIN_TIMEOUT)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def tick(now = ::Time.now.utc)
|
|
43
|
+
return unless leader?
|
|
44
|
+
|
|
45
|
+
enqueue(now)
|
|
46
|
+
rescue StandardError => e
|
|
47
|
+
handle_exception(e, { context: 'sidekiq-cron-poller' })
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# One pass over every entry, bypassing the leader gate. Rescued per
|
|
51
|
+
# entry, so one that raises on every poll cannot starve the rest.
|
|
52
|
+
def enqueue(now = ::Time.now.utc)
|
|
53
|
+
Job.all('*').each do |job|
|
|
54
|
+
job.test_and_enqueue_for_time!(now) if job.valid?
|
|
55
|
+
rescue StandardError => e
|
|
56
|
+
handle_exception(e, { context: 'sidekiq-cron-poller', name: job.name, namespace: job.namespace })
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
# The gem does this as its poller starts; off the boot path here, so a
|
|
63
|
+
# Redis blip costs the migration one boot, not the process.
|
|
64
|
+
def migrate
|
|
65
|
+
Job.migrate_old_jobs_if_needed!
|
|
66
|
+
rescue StandardError => e
|
|
67
|
+
handle_exception(e, { context: 'sidekiq-cron-migrate' })
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wurk
|
|
4
|
+
module SidekiqCron
|
|
5
|
+
# A sidekiq-cron `cron` string, read the way its fugit parser reads the
|
|
6
|
+
# forms a schedule file uses: five crontab fields or an `@alias`, then an
|
|
7
|
+
# optional IANA zone (`*/5 * * * * America/Caracas`). Month and weekday
|
|
8
|
+
# names (`jan`, `mon-fri`) are accepted. Wurk's own {Cron::Parser} answers
|
|
9
|
+
# the match, so the native periodic loops and this agree on every schedule.
|
|
10
|
+
#
|
|
11
|
+
# Not accepted, and reported as a validation error rather than guessed:
|
|
12
|
+
# a seconds field, fugit's natural language (`every day at 5pm`), `L`/`#`
|
|
13
|
+
# day forms, and numeric offsets in place of a zone.
|
|
14
|
+
class Schedule
|
|
15
|
+
MONTHS = %w[jan feb mar apr may jun jul aug sep oct nov dec].freeze
|
|
16
|
+
DAYS = %w[sun mon tue wed thu fri sat].freeze
|
|
17
|
+
|
|
18
|
+
# The newest occurrence the poller can still fire is at most
|
|
19
|
+
# `reschedule_grace_period` old; a save looks back further, to the
|
|
20
|
+
# occurrence the gem marks as already enqueued (`Job#save`).
|
|
21
|
+
CREATION_LOOKBACK = 366 * 24 * 3600
|
|
22
|
+
|
|
23
|
+
# No zone in the string means the process's local zone, as fugit reads
|
|
24
|
+
# it (ENV['TZ'], else the system's), not UTC as a periodic loop does.
|
|
25
|
+
module LocalZone
|
|
26
|
+
def self.at(epoch) = ::Time.at(epoch)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
attr_reader :parser, :zone
|
|
30
|
+
|
|
31
|
+
def initialize(string)
|
|
32
|
+
tokens = string.to_s.strip.split(/\s+/)
|
|
33
|
+
fields, rest = split_fields(tokens, string)
|
|
34
|
+
raise ArgumentError, "expected one trailing timezone, got #{rest.join(' ').inspect}" if rest.size > 1
|
|
35
|
+
|
|
36
|
+
@zone = rest.empty? ? LocalZone : resolve_zone(rest.first, string)
|
|
37
|
+
@parser = Cron::Parser.new(fields.join(' '))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def crontab = @parser.expression
|
|
41
|
+
|
|
42
|
+
# The zone the string names, nil for the local zone.
|
|
43
|
+
def zone_name = @zone.equal?(LocalZone) ? nil : @zone.identifier
|
|
44
|
+
|
|
45
|
+
# Latest occurrence at or before `time - 1s`, whole seconds, as a UTC Time
|
|
46
|
+
# (fugit's `previous_time`: it steps one second back, then truncates).
|
|
47
|
+
# Nil when there is none within `lookback` seconds. sidekiq-cron keys a
|
|
48
|
+
# fire by this instant, so it has to be the same one the gem computes.
|
|
49
|
+
def previous_time(time, lookback)
|
|
50
|
+
from = (time.to_f - 1).floor
|
|
51
|
+
epoch = from - (from % 60)
|
|
52
|
+
floor = from - lookback
|
|
53
|
+
while epoch >= floor
|
|
54
|
+
local = @parser.local_components(epoch, @zone)
|
|
55
|
+
return ::Time.at(epoch).utc if @parser.match?(::Time.at(epoch), @zone)
|
|
56
|
+
|
|
57
|
+
# Inside an hour whose hour or day cannot match, skip to the minute
|
|
58
|
+
# before that hour began; otherwise step one minute.
|
|
59
|
+
epoch -= hour_open?(local) ? 60 : (local[0] * 60) + 60
|
|
60
|
+
end
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def next_time(time)
|
|
65
|
+
epoch = @parser.next_fire_at(time.to_i, @zone)
|
|
66
|
+
epoch && ::Time.at(epoch).utc
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def split_fields(tokens, string)
|
|
72
|
+
return split_alias(tokens, string) if tokens.first.to_s.start_with?('@')
|
|
73
|
+
raise ArgumentError, "not a 5-field crontab: #{string.inspect}" if tokens.size < 5
|
|
74
|
+
|
|
75
|
+
fields = tokens.first(5)
|
|
76
|
+
fields[3] = names_to_numbers(fields[3], MONTHS, 1)
|
|
77
|
+
fields[4] = names_to_numbers(fields[4], DAYS, 0)
|
|
78
|
+
[fields, tokens.drop(5)]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def split_alias(tokens, string)
|
|
82
|
+
crontab = Cron::Parser::ALIASES[tokens.first.downcase]
|
|
83
|
+
raise ArgumentError, "unknown cron alias in #{string.inspect}" unless crontab
|
|
84
|
+
|
|
85
|
+
[crontab.split, tokens.drop(1)]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def names_to_numbers(field, names, base)
|
|
89
|
+
field.downcase.gsub(/[a-z]{3}/) do |name|
|
|
90
|
+
index = names.index(name)
|
|
91
|
+
raise ArgumentError, "unknown name #{name.inspect} in cron field #{field.inspect}" unless index
|
|
92
|
+
|
|
93
|
+
(index + base).to_s
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def resolve_zone(name, string)
|
|
98
|
+
zone = name.match?(%r{\A[A-Za-z][A-Za-z0-9_+\-/]*\z}) ? Cron::Parser.resolve_zone(name) : nil
|
|
99
|
+
return zone if zone
|
|
100
|
+
|
|
101
|
+
raise ArgumentError, "#{name.inspect} in #{string.inspect} is not a timezone tzinfo knows " \
|
|
102
|
+
'(a seconds field and natural-language schedules are not supported)'
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Never false for an hour that holds a match: the day test is the looser
|
|
106
|
+
# OR of the two day fields, whichever the parser applies.
|
|
107
|
+
def hour_open?(local)
|
|
108
|
+
_min, hour, dom, mon, dow = local
|
|
109
|
+
fields = @parser.fields
|
|
110
|
+
fields[1].include?(hour) && fields[3].include?(mon) && (fields[2].include?(dom) || fields[4].include?(dow))
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'erb'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
|
|
6
|
+
module Wurk
|
|
7
|
+
module SidekiqCron
|
|
8
|
+
# The gem's startup load of `cron_schedule_file` (default
|
|
9
|
+
# `config/schedule.yml`, falling back to `.yaml`), a Hash or an Array of
|
|
10
|
+
# entries, ERB allowed. Runs on `:startup` in server processes only, and
|
|
11
|
+
# not at all with `enabled = false`.
|
|
12
|
+
class ScheduleLoader
|
|
13
|
+
# The `:startup` hook: nothing when the gem owns Sidekiq::Cron, when
|
|
14
|
+
# `enabled` is false, or when there is no schedule file.
|
|
15
|
+
def self.on_startup
|
|
16
|
+
return unless SidekiqCron.active? && SidekiqCron.configuration.enabled
|
|
17
|
+
|
|
18
|
+
loader = new
|
|
19
|
+
loader.load_schedule if loader.has_schedule_file?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def load_schedule
|
|
23
|
+
case schedule
|
|
24
|
+
when ::Hash then Job.load_from_hash!(schedule, source: 'schedule')
|
|
25
|
+
when ::Array then Job.load_from_array!(schedule, source: 'schedule')
|
|
26
|
+
else raise "Not supported schedule format. Confirm your #{schedule_file_name}"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# rubocop:disable-next Naming/PredicatePrefix -- the gem's name
|
|
31
|
+
def has_schedule_file? = ::File.exist?(schedule_file_name)
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def schedule
|
|
36
|
+
@schedule ||= ::YAML.safe_load(::ERB.new(::File.read(schedule_file_name)).result,
|
|
37
|
+
permitted_classes: [::Symbol], aliases: true)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def schedule_file_name
|
|
41
|
+
@schedule_file_name ||= begin
|
|
42
|
+
name = SidekiqCron.configuration.cron_schedule_file.to_s
|
|
43
|
+
::File.exist?(name) ? name : name.sub(/\.yml\z/, '.yaml')
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
Wurk.configure_server { |config| config.on(:startup) { Wurk::SidekiqCron::ScheduleLoader.on_startup } }
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wurk
|
|
4
|
+
module SidekiqCron
|
|
5
|
+
# sidekiq-cron entries on the dashboard's Cron page, beside the periodic
|
|
6
|
+
# loops, with the same row shape and the same pause/unpause/enqueue/history
|
|
7
|
+
# actions. A row's `lid` is `sc-` plus the hex of `namespace NUL name`, so
|
|
8
|
+
# any name survives the URL path and cannot collide with a loop's lid.
|
|
9
|
+
module WebRows
|
|
10
|
+
PREFIX = 'sc-'
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def id?(lid) = lid.to_s.start_with?(PREFIX)
|
|
15
|
+
|
|
16
|
+
def lid_for(job) = "#{PREFIX}#{"#{job.namespace}\0#{job.name}".unpack1('H*')}"
|
|
17
|
+
|
|
18
|
+
def rows(now = ::Time.now)
|
|
19
|
+
Job.all('*').sort_by(&:sort_name).map { |job| row(job, now) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def row(job, now) # rubocop:disable Metrics/AbcSize -- one flat row
|
|
23
|
+
schedule = job.valid? ? job.schedule : nil
|
|
24
|
+
{
|
|
25
|
+
lid: lid_for(job), label: job.name, namespace: job.namespace, description: job.description,
|
|
26
|
+
source: 'sidekiq-cron', schedule: job.cron, klass: job.klass.to_s, queue: job.queue_name_with_prefix,
|
|
27
|
+
tz: schedule&.zone_name, paused: job.disabled?, args: job.args,
|
|
28
|
+
last_fire_at: job.last_enqueue_time&.to_i, next_fire_at: schedule&.next_time(now)&.to_i
|
|
29
|
+
}
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def find(lid)
|
|
33
|
+
namespace, name = [lid.to_s.delete_prefix(PREFIX)].pack('H*').force_encoding(::Encoding::UTF_8).split("\0", 2)
|
|
34
|
+
name && Job.find(name, namespace)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def pause(lid) = (job = find(lid)) ? job.disable! : false
|
|
38
|
+
|
|
39
|
+
def unpause(lid) = (job = find(lid)) ? job.enable! : false
|
|
40
|
+
|
|
41
|
+
def enqueue_now(lid)
|
|
42
|
+
job = find(lid)
|
|
43
|
+
job&.enqueue!
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# `[enqueued_at, jid]`, newest first, the periodic history's shape.
|
|
47
|
+
def history(lid)
|
|
48
|
+
job = find(lid)
|
|
49
|
+
return [] unless job
|
|
50
|
+
|
|
51
|
+
job.jid_history_from_redis.map { |entry| [::Time.parse(entry['enqueued'].to_s).to_i, entry['jid']] }
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../wurk'
|
|
4
|
+
require_relative 'sidekiq_cron/configuration'
|
|
5
|
+
require_relative 'sidekiq_cron/namespace'
|
|
6
|
+
require_relative 'sidekiq_cron/job'
|
|
7
|
+
require_relative 'sidekiq_cron/poller'
|
|
8
|
+
require_relative 'sidekiq_cron/schedule_loader'
|
|
9
|
+
require_relative 'sidekiq_cron/web_rows'
|
|
10
|
+
|
|
11
|
+
module Wurk
|
|
12
|
+
# sidekiq-cron, native: `Sidekiq::Cron` is this module whenever the
|
|
13
|
+
# sidekiq-cron gem is not in the bundle. Same DSL (`Sidekiq::Cron::Job`,
|
|
14
|
+
# `Sidekiq::Cron.configure`), same Redis keys and fields as the gem's 2.x,
|
|
15
|
+
# so dropping the gem from a running fleet needs no data migration and a
|
|
16
|
+
# disabled entry stays disabled across the swap.
|
|
17
|
+
#
|
|
18
|
+
# A store of its own, not the periodic loops (`Wurk::Cron`, `periodic` /
|
|
19
|
+
# `loops:<lid>`): a loop is keyed by a hash of its schedule, a sidekiq-cron
|
|
20
|
+
# entry by its name, and the gem's pods read only their own keys. The two
|
|
21
|
+
# share the crontab parser and the cluster leader, nothing else.
|
|
22
|
+
#
|
|
23
|
+
# With the gem bundled this module never loads (lib/sidekiq/cron.rb hands
|
|
24
|
+
# every require to the gem), so the gem's poller is the only one.
|
|
25
|
+
# See docs/periodic-jobs.md.
|
|
26
|
+
module SidekiqCron
|
|
27
|
+
class << self
|
|
28
|
+
attr_writer :configuration
|
|
29
|
+
|
|
30
|
+
def configuration
|
|
31
|
+
@configuration ||= Configuration.new
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def configure
|
|
35
|
+
yield(configuration) if block_given?
|
|
36
|
+
configuration
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def reset!
|
|
40
|
+
@configuration = Configuration.new
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# True when `Sidekiq::Cron` is this module, so the Launcher starts its
|
|
44
|
+
# poller and the dashboard lists its entries.
|
|
45
|
+
def active?
|
|
46
|
+
!defined?(::Sidekiq::Cron).nil? && ::Sidekiq::Cron.equal?(self)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
Sidekiq::Cron = Wurk::SidekiqCron unless Sidekiq.const_defined?(:Cron, false) && Sidekiq.autoload?(:Cron).nil?
|