zizq 0.6.0 → 0.8.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 +82 -11
- data/lib/active_job/queue_adapters/zizq_adapter.rb +4 -23
- data/lib/zizq/bulk_enqueue.rb +61 -14
- data/lib/zizq/client.rb +580 -77
- data/lib/zizq/crontab.rb +36 -3
- data/lib/zizq/crontab_builder.rb +10 -1
- data/lib/zizq/crontab_entry_builder.rb +66 -14
- data/lib/zizq/enqueue_request.rb +22 -21
- data/lib/zizq/enqueue_with.rb +65 -8
- data/lib/zizq/error.rb +10 -0
- data/lib/zizq/job_config.rb +95 -12
- data/lib/zizq/query.rb +111 -39
- data/lib/zizq/resources/budget.rb +109 -0
- data/lib/zizq/resources/cron_group.rb +4 -0
- data/lib/zizq/resources/job.rb +108 -2
- data/lib/zizq/resources/job_template.rb +15 -0
- data/lib/zizq/resources.rb +1 -0
- data/lib/zizq/test/client.rb +6 -3
- data/lib/zizq/version.rb +1 -1
- data/lib/zizq.rb +191 -14
- data/sig/generated/zizq/bulk_enqueue.rbs +4 -16
- data/sig/generated/zizq/client.rbs +377 -8
- data/sig/generated/zizq/crontab.rbs +17 -1
- data/sig/generated/zizq/crontab_builder.rbs +9 -0
- data/sig/generated/zizq/crontab_entry_builder.rbs +4 -17
- data/sig/generated/zizq/enqueue_request.rbs +11 -4
- data/sig/generated/zizq/enqueue_with.rbs +4 -9
- data/sig/generated/zizq/error.rbs +10 -0
- data/sig/generated/zizq/job_config.rbs +65 -0
- data/sig/generated/zizq/query.rbs +79 -2
- data/sig/generated/zizq/resources/budget.rbs +97 -0
- data/sig/generated/zizq/resources/cron_group.rbs +3 -0
- data/sig/generated/zizq/resources/job.rbs +94 -0
- data/sig/generated/zizq/resources/job_template.rbs +11 -0
- data/sig/generated/zizq.rbs +111 -33
- data/sig/zizq.rbs +100 -7
- metadata +4 -2
data/lib/zizq/crontab.rb
CHANGED
|
@@ -22,7 +22,11 @@ module Zizq
|
|
|
22
22
|
#
|
|
23
23
|
# By default schedules operate in the system time zone of the Zizq server
|
|
24
24
|
# but an explicit IANA timezone name can be specified when defining the
|
|
25
|
-
# Crontab.
|
|
25
|
+
# Crontab. It applies to every entry that does not specify one of its own,
|
|
26
|
+
# and is stored on the server as the schedule's own timezone, so it is still
|
|
27
|
+
# there when the schedule is read back.
|
|
28
|
+
#
|
|
29
|
+
# A schedule timezone requires Zizq 0.7.0 or newer on the server.
|
|
26
30
|
class Crontab
|
|
27
31
|
# The name of the cron group that this schedule is backed by.
|
|
28
32
|
attr_reader :name #: String
|
|
@@ -33,6 +37,9 @@ module Zizq
|
|
|
33
37
|
# and only advances the timer.
|
|
34
38
|
attr_writer :paused #: bool?
|
|
35
39
|
|
|
40
|
+
# The timezone in which entries without one of their own are processed.
|
|
41
|
+
attr_writer :timezone #: String?
|
|
42
|
+
|
|
36
43
|
# Initialize the Crontab with the given group name.
|
|
37
44
|
#
|
|
38
45
|
# @rbs name: String
|
|
@@ -42,6 +49,7 @@ module Zizq
|
|
|
42
49
|
@name = name #: String
|
|
43
50
|
@entries = {} #: Hash[String, Zizq::CrontabEntry]
|
|
44
51
|
@paused = false #: bool?
|
|
52
|
+
@timezone = nil #: String?
|
|
45
53
|
@paused_at = nil #: Float?
|
|
46
54
|
@resumed_at = nil #: Float?
|
|
47
55
|
end
|
|
@@ -64,6 +72,7 @@ module Zizq
|
|
|
64
72
|
def clear #: () -> self
|
|
65
73
|
@entries = {}
|
|
66
74
|
@paused = nil
|
|
75
|
+
@timezone = nil
|
|
67
76
|
@paused_at = nil
|
|
68
77
|
@resumed_at = nil
|
|
69
78
|
@materialized = false
|
|
@@ -102,6 +111,14 @@ module Zizq
|
|
|
102
111
|
# Alias of #paused.
|
|
103
112
|
def paused? = paused #: () -> bool?
|
|
104
113
|
|
|
114
|
+
# Return the timezone applied to entries that do not specify their own.
|
|
115
|
+
#
|
|
116
|
+
# Nil means entries fall back to the system timezone of the Zizq server.
|
|
117
|
+
def timezone #: () -> String?
|
|
118
|
+
materialize
|
|
119
|
+
@timezone
|
|
120
|
+
end
|
|
121
|
+
|
|
105
122
|
# Return the timestamp at which this Crontab schedule was last paused.
|
|
106
123
|
def paused_at #: () -> Float?
|
|
107
124
|
materialize
|
|
@@ -129,6 +146,10 @@ module Zizq
|
|
|
129
146
|
# This is equivalent to calling `Zizq.define_crontab` and is idempotent
|
|
130
147
|
# when given the same schedule more than once.
|
|
131
148
|
#
|
|
149
|
+
# The timezone is sent as the schedule's own, rather than being copied
|
|
150
|
+
# onto each entry, so it survives a later read of the schedule. Entries
|
|
151
|
+
# that specify their own timezone keep it.
|
|
152
|
+
#
|
|
132
153
|
# @rbs ?timezone: String?
|
|
133
154
|
# @rbs ?paused: bool?
|
|
134
155
|
# @rbs &block: (Zizq::CrontabBuilder) -> void
|
|
@@ -136,12 +157,16 @@ module Zizq
|
|
|
136
157
|
def redefine(timezone: nil, paused: nil, &block)
|
|
137
158
|
@building = true
|
|
138
159
|
|
|
139
|
-
|
|
160
|
+
builder = CrontabBuilder.new(self, timezone:, paused:)
|
|
161
|
+
yield builder
|
|
140
162
|
|
|
141
163
|
materialize_with(
|
|
142
164
|
Zizq.client.replace_cron_group(
|
|
143
165
|
name,
|
|
144
166
|
paused:,
|
|
167
|
+
# Read back off the builder, so assigning `cron.timezone =` inside
|
|
168
|
+
# the block works the same as passing it in.
|
|
169
|
+
timezone: builder.timezone,
|
|
145
170
|
entries: entries.values.map(&:to_params)
|
|
146
171
|
)
|
|
147
172
|
)
|
|
@@ -221,6 +246,7 @@ module Zizq
|
|
|
221
246
|
# @rbs return: self
|
|
222
247
|
def materialize_with(result)
|
|
223
248
|
@paused = result.paused?
|
|
249
|
+
@timezone = result.timezone
|
|
224
250
|
@paused_at = result.paused_at
|
|
225
251
|
@resumed_at = result.resumed_at
|
|
226
252
|
|
|
@@ -242,7 +268,12 @@ module Zizq
|
|
|
242
268
|
self,
|
|
243
269
|
result.name,
|
|
244
270
|
result.expression,
|
|
271
|
+
timezone: result.timezone,
|
|
245
272
|
job:
|
|
273
|
+
# Every field the template carries has to be listed here.
|
|
274
|
+
# One left out is not only absent from a read: a
|
|
275
|
+
# read-then-redefine writes the entry back without it,
|
|
276
|
+
# silently unbinding a budget or dropping a batch config.
|
|
246
277
|
EnqueueRequest.new(
|
|
247
278
|
type: result.job.type,
|
|
248
279
|
queue: result.job.queue,
|
|
@@ -252,7 +283,9 @@ module Zizq
|
|
|
252
283
|
backoff: result.job.backoff,
|
|
253
284
|
retention: result.job.retention,
|
|
254
285
|
unique_key: result.job.unique_key,
|
|
255
|
-
unique_while: result.job.unique_while
|
|
286
|
+
unique_while: result.job.unique_while,
|
|
287
|
+
batch: result.job.batch,
|
|
288
|
+
budgets: result.job.budgets
|
|
256
289
|
),
|
|
257
290
|
paused: result.paused?,
|
|
258
291
|
paused_at: result.paused_at,
|
data/lib/zizq/crontab_builder.rb
CHANGED
|
@@ -14,6 +14,10 @@ module Zizq
|
|
|
14
14
|
attr_reader :target #: Zizq::Crontab
|
|
15
15
|
|
|
16
16
|
# Optional timezone to be applied to all entries by default.
|
|
17
|
+
#
|
|
18
|
+
# This is sent to the server as the schedule's own timezone rather than
|
|
19
|
+
# being copied onto each entry, so reading the schedule back still says
|
|
20
|
+
# which timezone it runs in. Entries that specify their own override it.
|
|
17
21
|
attr_accessor :timezone #: String?
|
|
18
22
|
|
|
19
23
|
# Initialize the builder with the given Crontab instance.
|
|
@@ -34,12 +38,17 @@ module Zizq
|
|
|
34
38
|
# entry with the same name exist, this entry replaces that entry. If the
|
|
35
39
|
# entry is the same as the original, the result is idempotent.
|
|
36
40
|
#
|
|
41
|
+
# An entry without a timezone of its own runs in the schedule's, which the
|
|
42
|
+
# server applies. It is deliberately not copied onto the entry here — an
|
|
43
|
+
# entry that carried a copy would look like it had chosen that timezone
|
|
44
|
+
# for itself, and would not follow the schedule if it later changed.
|
|
45
|
+
#
|
|
37
46
|
# @rbs name: String
|
|
38
47
|
# @rbs expression: String
|
|
39
48
|
# @rbs timezone: String?
|
|
40
49
|
# @rbs paused: bool?
|
|
41
50
|
# @rbs return: Zizq::CrontabEntryBuilder
|
|
42
|
-
def define_entry(name, expression, timezone:
|
|
51
|
+
def define_entry(name, expression, timezone: nil, paused: nil)
|
|
43
52
|
CrontabEntryBuilder.new(target, name, expression, timezone:, paused:)
|
|
44
53
|
end
|
|
45
54
|
end
|
|
@@ -63,28 +63,80 @@ module Zizq
|
|
|
63
63
|
# @rbs kwargs: Hash[Symbol, untyped]
|
|
64
64
|
# @rbs &block: ?(EnqueueRequest) -> void
|
|
65
65
|
# @rbs return: void
|
|
66
|
-
def
|
|
66
|
+
def enqueue_job_class(job_class, *args, **kwargs, &block)
|
|
67
67
|
push_entry(Zizq.build_enqueue_request(job_class, *args, **kwargs, &block))
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
+
# Enqueue via this entry, by class or by raw inputs.
|
|
71
|
+
#
|
|
72
|
+
# cron.define_entry("d", "0 9 * * *").enqueue(DigestJob)
|
|
73
|
+
# cron.define_entry("d", "0 9 * * *")
|
|
74
|
+
# .enqueue(queue: "emails", type: "digest", payload: {})
|
|
75
|
+
#
|
|
76
|
+
# A cron entry fires on its schedule, so the raw form here omits
|
|
77
|
+
# `ready_at` and `delay` — passing either does not typecheck. Both
|
|
78
|
+
# still bind at runtime and raise an `ArgumentError`, which is what
|
|
79
|
+
# untyped callers get.
|
|
80
|
+
#
|
|
81
|
+
# @rbs skip
|
|
82
|
+
# @rbs!
|
|
83
|
+
# def enqueue: (
|
|
84
|
+
# Class & Zizq::JobConfig job_class,
|
|
85
|
+
# *untyped args,
|
|
86
|
+
# **untyped kwargs
|
|
87
|
+
# ) ?{ (EnqueueRequest) -> void } -> void
|
|
88
|
+
# | (
|
|
89
|
+
# queue: String,
|
|
90
|
+
# type: String,
|
|
91
|
+
# payload: untyped,
|
|
92
|
+
# ?priority: Integer?,
|
|
93
|
+
# ?retry_limit: Integer?,
|
|
94
|
+
# ?backoff: Zizq::backoff?,
|
|
95
|
+
# ?retention: Zizq::retention?,
|
|
96
|
+
# ?unique_key: String?,
|
|
97
|
+
# ?unique_while: Zizq::unique_scope?,
|
|
98
|
+
# ?batch: Zizq::batch?,
|
|
99
|
+
# ?budgets: Array[Zizq::budget_binding_params]?
|
|
100
|
+
# ) ?{ (EnqueueRequest) -> void } -> void
|
|
101
|
+
def enqueue(*args, **kwargs, &block)
|
|
102
|
+
return enqueue_job_class(*args, **kwargs, &block) unless args.empty?
|
|
103
|
+
|
|
104
|
+
begin
|
|
105
|
+
# See `Zizq.enqueue` for why only this branch is wrapped.
|
|
106
|
+
enqueue_raw(**kwargs, &block) # steep:ignore InsufficientKeywordArguments
|
|
107
|
+
rescue ArgumentError => e
|
|
108
|
+
raise ArgumentError, e.message, caller(1)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
70
112
|
# Process a raw job enqueue for this entry.
|
|
71
113
|
#
|
|
72
114
|
# This is used for low-level or cross-language support.
|
|
73
115
|
#
|
|
74
|
-
#
|
|
75
|
-
#
|
|
76
|
-
#
|
|
77
|
-
#
|
|
78
|
-
#
|
|
79
|
-
# @rbs
|
|
80
|
-
# @rbs
|
|
81
|
-
#
|
|
82
|
-
#
|
|
83
|
-
#
|
|
84
|
-
#
|
|
85
|
-
#
|
|
116
|
+
# `ready_at` and `delay` are absent from the signature: a cron entry
|
|
117
|
+
# fires on its schedule and cannot also be scheduled. They still
|
|
118
|
+
# bind at runtime and raise an `ArgumentError`, since the method
|
|
119
|
+
# takes `**opts` and cannot refuse a keyword by itself.
|
|
120
|
+
#
|
|
121
|
+
# @rbs skip
|
|
122
|
+
# @rbs!
|
|
123
|
+
# def enqueue_raw: (
|
|
124
|
+
# queue: String,
|
|
125
|
+
# type: String,
|
|
126
|
+
# payload: untyped,
|
|
127
|
+
# ?priority: Integer?,
|
|
128
|
+
# ?retry_limit: Integer?,
|
|
129
|
+
# ?backoff: Zizq::backoff?,
|
|
130
|
+
# ?retention: Zizq::retention?,
|
|
131
|
+
# ?unique_key: String?,
|
|
132
|
+
# ?unique_while: Zizq::unique_scope?,
|
|
133
|
+
# ?batch: Zizq::batch?,
|
|
134
|
+
# ?budgets: Array[Zizq::budget_binding_params]?
|
|
135
|
+
# ) ?{ (EnqueueRequest) -> void } -> void
|
|
86
136
|
def enqueue_raw(queue:, type:, payload:, **opts)
|
|
87
|
-
|
|
137
|
+
req = EnqueueRequest.new(queue:, type:, payload:, **opts)
|
|
138
|
+
yield req if block_given?
|
|
139
|
+
push_entry(req)
|
|
88
140
|
end
|
|
89
141
|
|
|
90
142
|
# Bulk enqueues are not supported via cron.
|
data/lib/zizq/enqueue_request.rb
CHANGED
|
@@ -51,6 +51,10 @@ module Zizq
|
|
|
51
51
|
# Batch configuration for folded jobs.
|
|
52
52
|
attr_accessor :batch #: Zizq::batch?
|
|
53
53
|
|
|
54
|
+
# Budgets this job must satisfy on when it dispatches. `nil` or empty
|
|
55
|
+
# array dispatches normally as soon as it reaches the front of the queue.
|
|
56
|
+
attr_accessor :budgets #: Array[Zizq::budget_binding_params]?
|
|
57
|
+
|
|
54
58
|
# @rbs type: String
|
|
55
59
|
# @rbs queue: String
|
|
56
60
|
# @rbs payload: untyped
|
|
@@ -63,6 +67,7 @@ module Zizq
|
|
|
63
67
|
# @rbs unique_key: String?
|
|
64
68
|
# @rbs unique_while: Zizq::unique_scope?
|
|
65
69
|
# @rbs batch: Zizq::batch?
|
|
70
|
+
# @rbs budgets: Array[Zizq::budget_binding_params]?
|
|
66
71
|
# @rbs return: void
|
|
67
72
|
def initialize(
|
|
68
73
|
type:,
|
|
@@ -76,7 +81,8 @@ module Zizq
|
|
|
76
81
|
retention: nil,
|
|
77
82
|
unique_key: nil,
|
|
78
83
|
unique_while: nil,
|
|
79
|
-
batch: nil
|
|
84
|
+
batch: nil,
|
|
85
|
+
budgets: nil
|
|
80
86
|
)
|
|
81
87
|
update(
|
|
82
88
|
type:,
|
|
@@ -90,7 +96,8 @@ module Zizq
|
|
|
90
96
|
retention:,
|
|
91
97
|
unique_key:,
|
|
92
98
|
unique_while:,
|
|
93
|
-
batch
|
|
99
|
+
batch:,
|
|
100
|
+
budgets:
|
|
94
101
|
)
|
|
95
102
|
end
|
|
96
103
|
|
|
@@ -120,6 +127,7 @@ module Zizq
|
|
|
120
127
|
# @rbs unique_key: String?
|
|
121
128
|
# @rbs unique_while: Zizq::unique_scope?
|
|
122
129
|
# @rbs batch: Zizq::batch?
|
|
130
|
+
# @rbs budgets: Array[Zizq::budget_binding_params]?
|
|
123
131
|
# @rbs return: self
|
|
124
132
|
def update(
|
|
125
133
|
type: @type,
|
|
@@ -133,7 +141,8 @@ module Zizq
|
|
|
133
141
|
retention: @retention,
|
|
134
142
|
unique_key: @unique_key,
|
|
135
143
|
unique_while: @unique_while,
|
|
136
|
-
batch: @batch
|
|
144
|
+
batch: @batch,
|
|
145
|
+
budgets: @budgets
|
|
137
146
|
)
|
|
138
147
|
@type = type
|
|
139
148
|
@queue = queue
|
|
@@ -147,13 +156,15 @@ module Zizq
|
|
|
147
156
|
@unique_key = unique_key
|
|
148
157
|
@unique_while = unique_while
|
|
149
158
|
@batch = batch
|
|
159
|
+
@budgets = budgets
|
|
150
160
|
self
|
|
151
161
|
end
|
|
152
162
|
|
|
153
163
|
# Convert to the params expected by `Client#enqueue`.
|
|
154
164
|
#
|
|
155
|
-
# Handles
|
|
156
|
-
#
|
|
165
|
+
# Handles delay -> ready_at resolution and nil omission. Durations
|
|
166
|
+
# stay in the fractional seconds they were written in; `Client`
|
|
167
|
+
# converts them on the way out.
|
|
157
168
|
def to_enqueue_params #: () -> Hash[Symbol, untyped]
|
|
158
169
|
params = { queue:, type:, payload: } #: Hash[Symbol, untyped]
|
|
159
170
|
params[:priority] = priority if priority
|
|
@@ -163,27 +174,17 @@ module Zizq
|
|
|
163
174
|
|
|
164
175
|
params[:retry_limit] = retry_limit if retry_limit
|
|
165
176
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
if retention
|
|
175
|
-
ret = {} #: Hash[Symbol, Integer]
|
|
176
|
-
ret[:completed_ms] = (
|
|
177
|
-
retention[:completed].to_f * 1000
|
|
178
|
-
).to_i if retention[:completed]
|
|
179
|
-
ret[:dead_ms] = (retention[:dead].to_f * 1000).to_i if retention[:dead]
|
|
180
|
-
params[:retention] = ret
|
|
181
|
-
end
|
|
177
|
+
# Durations stay in fractional seconds here. `Client` converts to
|
|
178
|
+
# milliseconds at the boundary, so converting again would multiply
|
|
179
|
+
# them a second time.
|
|
180
|
+
params[:backoff] = backoff if backoff
|
|
181
|
+
params[:retention] = retention if retention
|
|
182
182
|
|
|
183
183
|
params[:unique_key] = unique_key if unique_key
|
|
184
184
|
params[:unique_while] = unique_while.to_s if unique_while
|
|
185
185
|
|
|
186
186
|
params[:batch] = batch if batch
|
|
187
|
+
params[:budgets] = budgets if budgets && !budgets.empty?
|
|
187
188
|
|
|
188
189
|
params
|
|
189
190
|
end
|
data/lib/zizq/enqueue_with.rb
CHANGED
|
@@ -73,23 +73,80 @@ module Zizq
|
|
|
73
73
|
# @rbs kwargs: Hash[Symbol, untyped]
|
|
74
74
|
# @rbs &block: ?(EnqueueRequest) -> void
|
|
75
75
|
# @rbs return: untyped
|
|
76
|
-
def
|
|
76
|
+
def enqueue_job_class(job_class, *args, **kwargs, &block)
|
|
77
77
|
@target.enqueue(job_class, *args, **kwargs) do |req|
|
|
78
78
|
req.update(**@overrides)
|
|
79
79
|
block&.call(req)
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
|
|
83
|
+
# Enqueue through the target, by class or by raw inputs, with the
|
|
84
|
+
# scoped overrides applied either way.
|
|
85
|
+
#
|
|
86
|
+
# Zizq.enqueue_with(priority: 0)
|
|
87
|
+
# .enqueue(queue: "emails", type: "send_email", payload: {})
|
|
88
|
+
#
|
|
89
|
+
# @rbs skip
|
|
90
|
+
# @rbs!
|
|
91
|
+
# def enqueue: (
|
|
92
|
+
# Class & Zizq::JobConfig job_class,
|
|
93
|
+
# *untyped args,
|
|
94
|
+
# **untyped kwargs
|
|
95
|
+
# ) ?{ (EnqueueRequest) -> void } -> untyped
|
|
96
|
+
# | (
|
|
97
|
+
# queue: String,
|
|
98
|
+
# type: String,
|
|
99
|
+
# payload: untyped,
|
|
100
|
+
# ?priority: Integer?,
|
|
101
|
+
# ?delay: Zizq::to_f?,
|
|
102
|
+
# ?ready_at: Zizq::to_f?,
|
|
103
|
+
# ?retry_limit: Integer?,
|
|
104
|
+
# ?backoff: Zizq::backoff?,
|
|
105
|
+
# ?retention: Zizq::retention?,
|
|
106
|
+
# ?unique_key: String?,
|
|
107
|
+
# ?unique_while: Zizq::unique_scope?,
|
|
108
|
+
# ?batch: Zizq::batch?,
|
|
109
|
+
# ?budgets: Array[Zizq::budget_binding_params]?
|
|
110
|
+
# ) ?{ (EnqueueRequest) -> void } -> untyped
|
|
111
|
+
def enqueue(*args, **kwargs, &block)
|
|
112
|
+
return enqueue_job_class(*args, **kwargs, &block) unless args.empty?
|
|
113
|
+
|
|
114
|
+
begin
|
|
115
|
+
# See `Zizq.enqueue` for why only this branch is wrapped.
|
|
116
|
+
enqueue_raw(**kwargs, &block) # steep:ignore InsufficientKeywordArguments
|
|
117
|
+
rescue ArgumentError => e
|
|
118
|
+
raise ArgumentError, e.message, caller(1)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
83
122
|
# Enqueue a raw request via the underlying target, with overrides
|
|
84
123
|
# merged into the kwargs (explicit kwargs take precedence).
|
|
85
124
|
#
|
|
86
|
-
# @rbs
|
|
87
|
-
# @rbs
|
|
88
|
-
#
|
|
89
|
-
#
|
|
90
|
-
#
|
|
91
|
-
|
|
92
|
-
|
|
125
|
+
# @rbs skip
|
|
126
|
+
# @rbs!
|
|
127
|
+
# def enqueue_raw: (
|
|
128
|
+
# queue: String,
|
|
129
|
+
# type: String,
|
|
130
|
+
# payload: untyped,
|
|
131
|
+
# ?priority: Integer?,
|
|
132
|
+
# ?delay: Zizq::to_f?,
|
|
133
|
+
# ?ready_at: Zizq::to_f?,
|
|
134
|
+
# ?retry_limit: Integer?,
|
|
135
|
+
# ?backoff: Zizq::backoff?,
|
|
136
|
+
# ?retention: Zizq::retention?,
|
|
137
|
+
# ?unique_key: String?,
|
|
138
|
+
# ?unique_while: Zizq::unique_scope?,
|
|
139
|
+
# ?batch: Zizq::batch?,
|
|
140
|
+
# ?budgets: Array[Zizq::budget_binding_params]?
|
|
141
|
+
# ) ?{ (EnqueueRequest) -> void } -> untyped
|
|
142
|
+
def enqueue_raw(queue:, type:, payload:, **opts, &block)
|
|
143
|
+
@target.enqueue_raw(
|
|
144
|
+
queue:,
|
|
145
|
+
type:,
|
|
146
|
+
payload:,
|
|
147
|
+
**@overrides.merge(opts),
|
|
148
|
+
&block
|
|
149
|
+
)
|
|
93
150
|
end
|
|
94
151
|
|
|
95
152
|
# Wrap a bulk block so that every enqueue inside it inherits the
|
data/lib/zizq/error.rb
CHANGED
|
@@ -39,6 +39,16 @@ module Zizq
|
|
|
39
39
|
class NotFoundError < ClientError
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
# 409 specifically — the resource is in a state that refuses the
|
|
43
|
+
# operation, and repeating the request where nothing else has changed
|
|
44
|
+
# will not help.
|
|
45
|
+
#
|
|
46
|
+
# Raised where something already exists and cannot be overwritten, and
|
|
47
|
+
# where something is still referenced that would conflict with the
|
|
48
|
+
# operation.
|
|
49
|
+
class ConflictError < ClientError
|
|
50
|
+
end
|
|
51
|
+
|
|
42
52
|
# 5xx server error.
|
|
43
53
|
class ServerError < ResponseError
|
|
44
54
|
end
|
data/lib/zizq/job_config.rb
CHANGED
|
@@ -153,6 +153,64 @@ module Zizq
|
|
|
153
153
|
end
|
|
154
154
|
end
|
|
155
155
|
|
|
156
|
+
# Declare a budget this job is bound to.
|
|
157
|
+
#
|
|
158
|
+
# Budgets are used for concurrency control and rate limiting, and
|
|
159
|
+
# require a Pro license on the server.
|
|
160
|
+
#
|
|
161
|
+
# This method is additive, called once per budget:
|
|
162
|
+
#
|
|
163
|
+
# zizq_budget "emails", cost: 2
|
|
164
|
+
# zizq_budget "stripe"
|
|
165
|
+
#
|
|
166
|
+
# The `cost` is how many tokens one job of this class consumes when
|
|
167
|
+
# it dispatches, defaulting to 1. `create_with` declares the
|
|
168
|
+
# budget's policy should it not exist yet, so jobs can be enqueued
|
|
169
|
+
# without first creating the budget in a separate step:
|
|
170
|
+
#
|
|
171
|
+
# zizq_budget "emails",
|
|
172
|
+
# create_with: {
|
|
173
|
+
# allocation: 100,
|
|
174
|
+
# strategy: { type: :time_based, duration: 60 }
|
|
175
|
+
# }
|
|
176
|
+
#
|
|
177
|
+
# It is ignored when the budget already exists, so this cannot
|
|
178
|
+
# accidentally overwrite an already configured budget.
|
|
179
|
+
#
|
|
180
|
+
# Declaring the same key twice raises `ArgumentError' — the server
|
|
181
|
+
# would reject a duplicate key outright.
|
|
182
|
+
#
|
|
183
|
+
# A per-enqueue `budgets` replaces these rather than adding to
|
|
184
|
+
# them, the same way a block-assigned `queue` or `priority` does:
|
|
185
|
+
#
|
|
186
|
+
# Zizq.enqueue(SendEmailJob) { |req| req.budgets = [...] }
|
|
187
|
+
# Zizq.enqueue(SendEmailJob) { |req| req.budgets << {key: "x"} }
|
|
188
|
+
#
|
|
189
|
+
# @rbs key: String
|
|
190
|
+
# @rbs cost: Integer?
|
|
191
|
+
# @rbs create_with: Zizq::budget_policy?
|
|
192
|
+
# @rbs return: Zizq::budget_binding_params
|
|
193
|
+
def zizq_budget(key, cost: nil, create_with: nil)
|
|
194
|
+
if zizq_budgets.any? { |b| b[:key] == key }
|
|
195
|
+
raise ArgumentError, "#{self}: budget '#{key}' is already declared"
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
binding = { key: } #: Zizq::budget_binding_params
|
|
199
|
+
binding[:cost] = cost if cost
|
|
200
|
+
binding[:create_with] = create_with if create_with
|
|
201
|
+
|
|
202
|
+
@zizq_budgets = zizq_budgets << binding.freeze
|
|
203
|
+
binding
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Budgets declared on this class, in declaration order.
|
|
207
|
+
#
|
|
208
|
+
# A fresh array each call, so mutating it cannot corrupt the
|
|
209
|
+
# class-level declaration. The bindings themselves are frozen.
|
|
210
|
+
def zizq_budgets #: () -> Array[Zizq::budget_binding_params]
|
|
211
|
+
(@zizq_budgets || []).dup
|
|
212
|
+
end
|
|
213
|
+
|
|
156
214
|
# Declare uniqueness for this job class.
|
|
157
215
|
#
|
|
158
216
|
# Requires a pro license.
|
|
@@ -359,18 +417,43 @@ module Zizq
|
|
|
359
417
|
# req
|
|
360
418
|
# end
|
|
361
419
|
def zizq_enqueue_request(*args, **kwargs) #: (*untyped, **untyped) -> EnqueueRequest
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
420
|
+
req =
|
|
421
|
+
EnqueueRequest.new(
|
|
422
|
+
type: name || raise(ArgumentError, "Cannot enqueue anonymous class"),
|
|
423
|
+
queue: zizq_queue,
|
|
424
|
+
payload: zizq_serialize(*args, **kwargs),
|
|
425
|
+
priority: zizq_priority
|
|
426
|
+
)
|
|
427
|
+
zizq_apply_class_config(req, *args, **kwargs)
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Apply this class's declared configuration to an enqueue request.
|
|
431
|
+
#
|
|
432
|
+
# Everything set here is a property of the class rather than of one
|
|
433
|
+
# enqueue, so it applies the same way however the request was built.
|
|
434
|
+
# `zizq_enqueue_request` builds one entirely from the class and
|
|
435
|
+
# calls this. The ActiveJob adapter cannot: `job_id`, a
|
|
436
|
+
# `set(queue:)` override and a `set(wait:)` delay exist only on the
|
|
437
|
+
# job *instance*, so it constructs those fields itself and then
|
|
438
|
+
# calls this for the rest.
|
|
439
|
+
#
|
|
440
|
+
# Adding a field to the DSL means adding it here, and both paths
|
|
441
|
+
# pick it up. Setting it in only one of them is how a declaration
|
|
442
|
+
# silently stops reaching the server for half the callers.
|
|
443
|
+
#
|
|
444
|
+
# @rbs req: EnqueueRequest
|
|
445
|
+
# @rbs *args: untyped
|
|
446
|
+
# @rbs **kwargs: untyped
|
|
447
|
+
# @rbs return: EnqueueRequest
|
|
448
|
+
def zizq_apply_class_config(req, *args, **kwargs)
|
|
449
|
+
req.retry_limit = zizq_retry_limit
|
|
450
|
+
req.backoff = zizq_backoff
|
|
451
|
+
req.retention = zizq_retention
|
|
452
|
+
req.unique_while = zizq_unique ? zizq_unique_scope : nil
|
|
453
|
+
req.unique_key = zizq_unique ? zizq_unique_key(*args, **kwargs) : nil
|
|
454
|
+
req.batch = zizq_batched ? batch_for_enqueue(*args, **kwargs) : nil
|
|
455
|
+
req.budgets = zizq_budgets
|
|
456
|
+
req
|
|
374
457
|
end
|
|
375
458
|
|
|
376
459
|
private
|