anway 0.5.3-x86_64-linux-gnu → 0.6.0-x86_64-linux-gnu
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 +64 -2
- data/lib/anway/cli.rb +5 -2
- data/lib/anway/client.rb +17 -0
- data/lib/anway/cron.rb +46 -0
- data/lib/anway/options.rb +7 -5
- data/lib/anway/runner.rb +42 -2
- data/lib/anway/settings.rb +13 -1
- data/lib/anway/testing.rb +24 -1
- data/lib/anway/version.rb +2 -2
- data/lib/anway/worker.rb +29 -5
- data/lib/anway.rb +30 -0
- data/lib/generators/anway/install/templates/anway.yml +9 -0
- data/libexec/anway-server +0 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d2b3873a30b7e93e068e421393640af582073e4b84fe9c031c66f53d54be81d5
|
|
4
|
+
data.tar.gz: 68f71d306315096d7d3fd9f7ac0e4ad9ccdffc411e0f68964b73dd004a8f9e2c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bf9411c58a85bb62bfeb31fd2c53d33305a528e2f54d4acc44ec1a32d0450d654ba6caf113a69ea47633777295e66eb068f5209136104650563759d7dae54fe2
|
|
7
|
+
data.tar.gz: 6bee99094535a940bfef8d137bd63da7fe22df610c092ef754be9d7da6ce8f90468a53ccec44cbac54f2fad53d82f881f830b0941616437d0053273b7246839a
|
data/README.md
CHANGED
|
@@ -100,6 +100,7 @@ class WelcomeJob
|
|
|
100
100
|
|
|
101
101
|
anway_options event: 'email.welcome', retry: 5
|
|
102
102
|
anway_options topics: %w[user.created], queue: 'mailers'
|
|
103
|
+
anway_options cron: '0 9 * * *'
|
|
103
104
|
|
|
104
105
|
def perform(user_id)
|
|
105
106
|
# ...
|
|
@@ -114,7 +115,7 @@ WelcomeJob.set(queue: 'low', unique_for: 60).perform_async(1)
|
|
|
114
115
|
- `anway_options` declares the options of the class. You can call it more than once. An unknown key raises `ArgumentError`.
|
|
115
116
|
- `event` is the name of what the job handles. It goes to the server as the job type. The default is the class name. Use a name when a worker in another language runs the job.
|
|
116
117
|
- Two classes with the same event raise `ArgumentError`. A worker has one class for each event.
|
|
117
|
-
- A subclass inherits all options except `event` and `
|
|
118
|
+
- A subclass inherits all options except `event`, `topics`, and `cron`.
|
|
118
119
|
- `set` changes the options for one push. It takes the push keys below.
|
|
119
120
|
- Args must be JSON types with string hash keys. Symbols, times, and objects raise `ArgumentError`.
|
|
120
121
|
- Delivery is at least once, so jobs must be idempotent.
|
|
@@ -139,11 +140,12 @@ All Anway clients use the same keys. The gem translates them to the fields of th
|
|
|
139
140
|
| `on_dead` | `custom.on_dead` | `{ event:, queue: }`. When the job dies, the server pushes a job of this event. |
|
|
140
141
|
| `custom` | `custom` | The keys of your app. A key above wins over a key with the same name here. |
|
|
141
142
|
| `topics` | One `SUBSCRIBE` for each topic | Only in `anway_options`. See [Topics](#topics). |
|
|
143
|
+
| `cron` | One entry of a `SCHEDULE` for each schedule | A string or an array of strings. Only in `anway_options`. See [Cron](#cron). |
|
|
142
144
|
|
|
143
145
|
When a class has more than one option set, the sets merge in call order:
|
|
144
146
|
|
|
145
147
|
- A single value, such as `queue` or `retry`: the last set that has it wins.
|
|
146
|
-
- `topics`: the
|
|
148
|
+
- `topics` and `cron`: the values of all sets add up, in order, with no duplicates.
|
|
147
149
|
- `custom`: the hashes merge, and a later key wins.
|
|
148
150
|
|
|
149
151
|
`set` merges with the options of the class by the same rules. `WelcomeJob.anway_options`, with no keys, returns the merged options.
|
|
@@ -227,6 +229,57 @@ results.reject { |result| result.error.nil? }.each { |result| Rails.logger.warn(
|
|
|
227
229
|
|
|
228
230
|
[docs/topics.md](docs/topics.md) explains the chat example with three apps, the options, the tests, and problems.
|
|
229
231
|
|
|
232
|
+
## Cron
|
|
233
|
+
|
|
234
|
+
Since 0.6.0, the app declares its cron entries in its code or in `config/anway.yml`, as Solid Queue recurring tasks do. The server keeps them for the tag of the app, also after a restart, and runs them with the cron entries of its own conf.d. You do not need access to the server files. Schedules use UTC.
|
|
235
|
+
|
|
236
|
+
Declare a schedule in a job class:
|
|
237
|
+
|
|
238
|
+
```ruby
|
|
239
|
+
class RefillCredits
|
|
240
|
+
include Anway::Worker
|
|
241
|
+
|
|
242
|
+
anway_options event: 'credits.refill', queue: 'default', retry: 3, cron: '0 0 * * *'
|
|
243
|
+
|
|
244
|
+
def perform
|
|
245
|
+
# ...
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Or in `config/anway.yml`, in the format of the cron entries of the server conf.d:
|
|
251
|
+
|
|
252
|
+
```yaml
|
|
253
|
+
tag: shop
|
|
254
|
+
|
|
255
|
+
cron:
|
|
256
|
+
- schedule: "0 6 * * *"
|
|
257
|
+
job:
|
|
258
|
+
type: report.daily
|
|
259
|
+
queue: reports
|
|
260
|
+
args: [7]
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
- `cron` takes a string or an array of strings. Each schedule makes one entry. Its job has the `event` of the class as its type, and the `queue`, `retry`, and `reserve_for` of the class. A job class has no args, so give args in `config/anway.yml`.
|
|
264
|
+
- A YAML entry has `schedule`, and `job` with `type`, `queue`, `args`, `retry`, `reserve_for`, and `custom`. The section of an environment replaces the `cron` list at the top.
|
|
265
|
+
- When it starts, the worker sends the entries of the job classes and of the file in one `SCHEDULE`, with the `tag` of `config/anway.yml`, `ANWAY_TAG`, or `--tag`. The server replaces all entries of the tag, so an entry that you remove from the code goes away at the next start.
|
|
266
|
+
- **A tag is required.** With cron entries and no tag, `bin/anway` stops at start with an error. The app folder, which is the default tag in the console, is not used, because two apps can have the same folder name, such as `/rails` in Docker, and one tag would then join their entries.
|
|
267
|
+
- A worker with no cron entries sends no `SCHEDULE`, so it does not remove the entries that another process sent. To remove the last entry, run `Anway.unschedule('shop')` once, for example in the Rails console.
|
|
268
|
+
- All processes with one tag must declare the same entries, because the last `SCHEDULE` wins. In a Rails app, every process loads all job classes, so this is true.
|
|
269
|
+
- Without a queue list, the worker also fetches the queues of the cron jobs.
|
|
270
|
+
- A bad schedule or an entry with no job type stops the worker at start with `Anway::ServerError`, and the entries on the server do not change.
|
|
271
|
+
- The console of the server shows each entry on the Schedules page, with its source and tag.
|
|
272
|
+
|
|
273
|
+
The same operations work from any process, such as the Rails console. They use the connection of `Anway.client`:
|
|
274
|
+
|
|
275
|
+
```ruby
|
|
276
|
+
Anway.schedule('shop', [{ schedule: '0 0 * * *', job: { type: 'credits.refill', queue: 'default' } }])
|
|
277
|
+
Anway.unschedule('shop')
|
|
278
|
+
Anway.cron # => the entries that the worker of this app sends at start
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
`client.schedule(tag, entries)` and `client.unschedule(tag)` are the same commands on an `Anway::Client`. In an entry, `job: { event: }` is the same as `job: { type: }`. No entries removes the tag, and a missing tag is not an error for `unschedule`.
|
|
282
|
+
|
|
230
283
|
## Rails
|
|
231
284
|
|
|
232
285
|
```ruby
|
|
@@ -394,6 +447,15 @@ Anway::Testing.events.last['topic']
|
|
|
394
447
|
Anway::Testing.inline!
|
|
395
448
|
```
|
|
396
449
|
|
|
450
|
+
Check the cron declarations of the app in a spec. `Anway.cron` returns the entries that the worker sends, from the job classes and from `config/anway.yml`. In fake and inline mode, `Anway.schedule` and `Anway.unschedule` change `Anway::Testing.schedules`, a hash of the entries of each tag, and send nothing:
|
|
451
|
+
|
|
452
|
+
```ruby
|
|
453
|
+
expect(Anway.cron).to include({ 'schedule' => '0 0 * * *', 'job' => { 'type' => 'credits.refill', 'queue' => 'default', 'retry' => 3 } })
|
|
454
|
+
|
|
455
|
+
Anway.schedule('shop', [{ schedule: '0 0 * * *', job: { type: 'credits.refill' } }])
|
|
456
|
+
Anway::Testing.schedules['shop'].size # => 1
|
|
457
|
+
```
|
|
458
|
+
|
|
397
459
|
In inline mode, `Anway.publish` runs one copy in each job class of the app that subscribes to the topic. `Anway.publish_bulk` works as one `Anway.publish` for each event.
|
|
398
460
|
|
|
399
461
|
`perform_bulk` and `Anway.push_bulk` work as single pushes do: fake mode adds each job to `Anway::Testing.jobs`, and inline mode runs each job. The result is `{}`.
|
data/lib/anway/cli.rb
CHANGED
|
@@ -209,6 +209,8 @@ module Anway
|
|
|
209
209
|
labels: @options[:labels],
|
|
210
210
|
hostname: @options[:hostname],
|
|
211
211
|
tag: @options[:tag] || default_tag,
|
|
212
|
+
cron: Anway.cron(@settings),
|
|
213
|
+
cron_tag: @options[:tag],
|
|
212
214
|
autoscale: @options[:autoscale],
|
|
213
215
|
limit: database_pool
|
|
214
216
|
)
|
|
@@ -233,14 +235,15 @@ module Anway
|
|
|
233
235
|
Signal.trap('TTIN') { runner.dump }
|
|
234
236
|
end
|
|
235
237
|
|
|
236
|
-
# Without a list, the worker uses the queues of its job classes
|
|
237
|
-
#
|
|
238
|
+
# Without a list, the worker uses the queues of its job classes, of their
|
|
239
|
+
# subscriptions, and of the cron entries. With a list, the runner logs a warning for each
|
|
238
240
|
# subscription queue that is not in it.
|
|
239
241
|
def queues
|
|
240
242
|
return @options[:queues] if @options[:queues]
|
|
241
243
|
|
|
242
244
|
names = Worker.classes.map { |klass| klass.anway_options[:queue]&.to_s }
|
|
243
245
|
names.concat(Worker.subscriptions.map(&:queue))
|
|
246
|
+
names.concat(Anway.cron(@settings).map { |entry| entry.dig('job', 'queue') || 'default' })
|
|
244
247
|
names.concat(active_job_queues)
|
|
245
248
|
|
|
246
249
|
[*names.compact, 'default'].uniq
|
data/lib/anway/client.rb
CHANGED
|
@@ -185,6 +185,23 @@ module Anway
|
|
|
185
185
|
true
|
|
186
186
|
end
|
|
187
187
|
|
|
188
|
+
# Replaces all cron entries of the tag on the server, so an entry that is
|
|
189
|
+
# not in entries goes away. No entries removes the tag. Each entry is a
|
|
190
|
+
# hash in the format of Anway::Cron. A bad schedule or an entry with no
|
|
191
|
+
# job type raises ServerError, and then nothing changes.
|
|
192
|
+
def schedule(tag, entries)
|
|
193
|
+
call('SCHEDULE', { tag: tag, entries: Cron.entries(entries.to_a) })
|
|
194
|
+
|
|
195
|
+
true
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Removes all cron entries of the tag. A missing tag is not an error.
|
|
199
|
+
def unschedule(tag)
|
|
200
|
+
call('UNSCHEDULE', { tag: tag })
|
|
201
|
+
|
|
202
|
+
true
|
|
203
|
+
end
|
|
204
|
+
|
|
188
205
|
def batch_new(success: nil, complete: nil, parent_bid: nil, description: nil)
|
|
189
206
|
definition = {
|
|
190
207
|
success: success&.to_h, complete: complete&.to_h, parent_bid: parent_bid, description: description
|
data/lib/anway/cron.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anway
|
|
4
|
+
# Cron entries that an app declares. The worker sends them with SCHEDULE
|
|
5
|
+
# when it starts, and the server keeps them for the tag of the app. An entry
|
|
6
|
+
# has the format of a cron entry in the conf.d of the server:
|
|
7
|
+
#
|
|
8
|
+
# { 'schedule' => '0 0 * * *',
|
|
9
|
+
# 'job' => { 'type' => 'credits.refill', 'queue' => 'default', 'retry' => 3,
|
|
10
|
+
# 'args' => [], 'reserve_for' => 0, 'custom' => {} } }
|
|
11
|
+
#
|
|
12
|
+
# Only schedule and job.type are required. job.event is the input contract
|
|
13
|
+
# name of job.type, and goes out as type.
|
|
14
|
+
module Cron
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
# The entry with string keys. A value that is not a hash with a schedule
|
|
18
|
+
# and a job raises ArgumentError. The server checks the rest.
|
|
19
|
+
def entry(value)
|
|
20
|
+
raise ArgumentError, "a cron entry must be a hash with schedule and job, not #{value.inspect}" unless value.is_a?(Hash)
|
|
21
|
+
|
|
22
|
+
entry = stringify(value)
|
|
23
|
+
job = entry['job']
|
|
24
|
+
|
|
25
|
+
raise ArgumentError, "the cron entry #{entry['schedule'].inspect} needs a job hash" unless job.is_a?(Hash)
|
|
26
|
+
|
|
27
|
+
job['type'] = job.delete('event').to_s if job.key?('event') && !job.key?('type')
|
|
28
|
+
|
|
29
|
+
entry
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def entries(values)
|
|
33
|
+
raise ArgumentError, "cron must be a list of entries, not #{values.inspect}" unless values.is_a?(Array)
|
|
34
|
+
|
|
35
|
+
values.map { |value| entry(value) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def stringify(value)
|
|
39
|
+
case value
|
|
40
|
+
when Hash then value.to_h { |key, item| [key.to_s, stringify(item)] }
|
|
41
|
+
when Array then value.map { |item| stringify(item) }
|
|
42
|
+
else value
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/anway/options.rb
CHANGED
|
@@ -16,6 +16,8 @@ module Anway
|
|
|
16
16
|
# on_dead { event:, queue: } as { "jobtype", "queue" }
|
|
17
17
|
# custom custom, for the keys of the app. The keys above win.
|
|
18
18
|
# topics one SUBSCRIBE for each topic (a declaration only)
|
|
19
|
+
# cron one entry of the SCHEDULE of the worker for each cron
|
|
20
|
+
# schedule (a declaration only)
|
|
19
21
|
module Options
|
|
20
22
|
# Keys with a wire field of the same name.
|
|
21
23
|
FIELDS = %i[queue retry reserve_for backtrace at].freeze
|
|
@@ -27,8 +29,8 @@ module Anway
|
|
|
27
29
|
PUSH = [*FIELDS, *CUSTOM, :custom].freeze
|
|
28
30
|
|
|
29
31
|
# The keys of a declaration: anway_options. at is only for a push, and
|
|
30
|
-
# topics only for a declaration.
|
|
31
|
-
DECLARATION = [:event, *(PUSH - [:at]), :topics].freeze
|
|
32
|
+
# topics and cron only for a declaration.
|
|
33
|
+
DECLARATION = [:event, *(PUSH - [:at]), :topics, :cron].freeze
|
|
32
34
|
|
|
33
35
|
ON_DEAD = %i[event queue].freeze
|
|
34
36
|
|
|
@@ -43,14 +45,14 @@ module Anway
|
|
|
43
45
|
end
|
|
44
46
|
|
|
45
47
|
# Merges option sets in order. A single value from a later set wins.
|
|
46
|
-
# topics add up in order, with no duplicates. custom hashes
|
|
47
|
-
# later key wins.
|
|
48
|
+
# topics and cron add up in order, with no duplicates. custom hashes
|
|
49
|
+
# merge, and a later key wins.
|
|
48
50
|
def merge(*sets)
|
|
49
51
|
sets.each_with_object({}) do |set, merged|
|
|
50
52
|
set.each do |key, value|
|
|
51
53
|
merged[key] =
|
|
52
54
|
case key
|
|
53
|
-
when :topics then (merged[
|
|
55
|
+
when :topics, :cron then (merged[key] || []) | Array(value).map(&:to_s)
|
|
54
56
|
when :custom then (merged[:custom] || {}).merge(stringify(value))
|
|
55
57
|
else value
|
|
56
58
|
end
|
data/lib/anway/runner.rb
CHANGED
|
@@ -23,11 +23,19 @@ module Anway
|
|
|
23
23
|
# as { max: n } or { min: n, max: n }, starts the worker with autoscale on.
|
|
24
24
|
# subscriptions is a list of Worker::Subscription. The default is the
|
|
25
25
|
# subscriptions of all registered job classes, read at run.
|
|
26
|
+
#
|
|
27
|
+
# cron is a list of cron entries (Anway::Cron). The default is the entries
|
|
28
|
+
# of all registered job classes, read at run. The runner sends them in one
|
|
29
|
+
# SCHEDULE with cron_tag, which is tag by default. With entries and no
|
|
30
|
+
# cron_tag, run raises ArgumentError: a default tag could join the entries
|
|
31
|
+
# of two apps on one server.
|
|
26
32
|
def initialize(queues: ['default'], concurrency: 10, timeout: 25, cancel_grace: 5, labels: [],
|
|
27
33
|
hostname: nil, tag: nil, autoscale: nil, limit: nil, subscriptions: nil,
|
|
28
|
-
beat_interval: BEAT_INTERVAL, url: nil, logger: Anway.config.logger)
|
|
34
|
+
cron: nil, cron_tag: tag, beat_interval: BEAT_INTERVAL, url: nil, logger: Anway.config.logger)
|
|
29
35
|
@queues = Queues.new(queues)
|
|
30
36
|
@subscriptions = subscriptions
|
|
37
|
+
@cron = cron
|
|
38
|
+
@cron_tag = cron_tag
|
|
31
39
|
@concurrency = concurrency
|
|
32
40
|
@limit = limit
|
|
33
41
|
@timeout = timeout
|
|
@@ -73,11 +81,15 @@ module Anway
|
|
|
73
81
|
end
|
|
74
82
|
|
|
75
83
|
# The first beat reports the threads, so the server can scale the worker
|
|
76
|
-
# at once. The subscriptions go to the server before
|
|
84
|
+
# at once. The subscriptions and the cron entries go to the server before
|
|
85
|
+
# the first fetch.
|
|
77
86
|
def run
|
|
87
|
+
cron = cron_entries
|
|
88
|
+
|
|
78
89
|
@client.heartbeat(nil, concurrency: @concurrency)
|
|
79
90
|
|
|
80
91
|
subscribe_all
|
|
92
|
+
schedule_all(cron)
|
|
81
93
|
|
|
82
94
|
@lock.synchronize { @processors = Array.new(@concurrency) { start_processor } }
|
|
83
95
|
|
|
@@ -120,6 +132,34 @@ module Anway
|
|
|
120
132
|
end
|
|
121
133
|
end
|
|
122
134
|
|
|
135
|
+
def cron_entries
|
|
136
|
+
entries = @cron || Worker.cron_entries
|
|
137
|
+
|
|
138
|
+
return entries if entries.empty? || @cron_tag
|
|
139
|
+
|
|
140
|
+
raise ArgumentError, 'anway has cron entries but no tag: set tag in config/anway.yml, ANWAY_TAG, or --tag. ' \
|
|
141
|
+
'The server keeps the cron entries of each tag.'
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# SCHEDULE replaces the entries of the tag, so an entry that the app
|
|
145
|
+
# removed goes away. With no entries, the runner sends nothing, so it does
|
|
146
|
+
# not remove the entries that another process sent for the tag.
|
|
147
|
+
def schedule_all(entries)
|
|
148
|
+
return if entries.empty?
|
|
149
|
+
|
|
150
|
+
@client.schedule(@cron_tag, entries)
|
|
151
|
+
|
|
152
|
+
@logger.info("anway scheduled tag=#{@cron_tag} cron=#{entries.size}")
|
|
153
|
+
|
|
154
|
+
entries.each do |entry|
|
|
155
|
+
queue = entry.dig('job', 'queue') || 'default'
|
|
156
|
+
|
|
157
|
+
next if @queues.names.include?(queue)
|
|
158
|
+
|
|
159
|
+
@logger.warn("anway does not fetch queue #{queue}, so the cron jobs #{entry.dig('job', 'type')} wait there. Add the queue to queues.")
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
123
163
|
def wait_for_stop
|
|
124
164
|
loop do
|
|
125
165
|
@events_reader.wait_readable
|
data/lib/anway/settings.rb
CHANGED
|
@@ -14,6 +14,10 @@ module Anway
|
|
|
14
14
|
# production:
|
|
15
15
|
# url: <%= ENV["ANWAY_URL"] %>
|
|
16
16
|
#
|
|
17
|
+
# cron:
|
|
18
|
+
# - schedule: "0 0 * * *"
|
|
19
|
+
# job: { type: credits.refill, queue: default }
|
|
20
|
+
#
|
|
17
21
|
# The server is embedded, so the app runs it, unless a url is set in the
|
|
18
22
|
# file or in ANWAY_URL. `server: { mode: }` or ANWAY_SERVER can still choose.
|
|
19
23
|
class Settings
|
|
@@ -23,7 +27,7 @@ module Anway
|
|
|
23
27
|
DEFAULT_WEB = '127.0.0.1:7420'
|
|
24
28
|
DEFAULT_CONSOLE = '127.0.0.1:7421'
|
|
25
29
|
MODES = %w[embedded external].freeze
|
|
26
|
-
KEYS = %w[queues concurrency timeout labels hostname tag autoscale require url server console log_tags].freeze
|
|
30
|
+
KEYS = %w[queues concurrency timeout labels hostname tag autoscale require url server console log_tags cron].freeze
|
|
27
31
|
SERVER_KEYS = %w[mode bind data password web web_token].freeze
|
|
28
32
|
CONSOLE_KEYS = %w[path bind api token].freeze
|
|
29
33
|
|
|
@@ -65,6 +69,13 @@ module Anway
|
|
|
65
69
|
options.compact
|
|
66
70
|
end
|
|
67
71
|
|
|
72
|
+
# The cron entries of the file, in the format of Anway::Cron: a list in
|
|
73
|
+
# the format of the cron entries of the server conf.d. The section of the
|
|
74
|
+
# environment replaces the list at the top.
|
|
75
|
+
def cron
|
|
76
|
+
@cron ||= Cron.entries(@values['cron'] || [])
|
|
77
|
+
end
|
|
78
|
+
|
|
68
79
|
# log_tags: false turns off the job tags on log lines. On by default.
|
|
69
80
|
def log_tags?
|
|
70
81
|
@values['log_tags'] != false
|
|
@@ -230,6 +241,7 @@ module Anway
|
|
|
230
241
|
|
|
231
242
|
def check!
|
|
232
243
|
check_autoscale!
|
|
244
|
+
cron
|
|
233
245
|
raise ArgumentError, "server mode must be embedded or external, not #{mode}" unless MODES.include?(mode)
|
|
234
246
|
raise ArgumentError, 'an external server needs url or ANWAY_URL' if mode == 'external' && url.nil?
|
|
235
247
|
raise ArgumentError, "server bind must be host:port, not #{bind}" unless split_bind.last.to_s.match?(/\A\d+\z/)
|
data/lib/anway/testing.rb
CHANGED
|
@@ -9,13 +9,18 @@ module Anway
|
|
|
9
9
|
# Anway::Testing.inline! runs each job at push, and each event in the
|
|
10
10
|
# subscribed classes of the app at publish
|
|
11
11
|
# Anway::Testing.disable! uses the real client
|
|
12
|
+
#
|
|
13
|
+
# In both test modes, Anway.schedule and Anway.unschedule change
|
|
14
|
+
# Testing.schedules, a hash of the cron entries of each tag. Anway.cron
|
|
15
|
+
# lists the cron entries that the worker of the app declares.
|
|
12
16
|
module Testing
|
|
13
17
|
@mode = nil
|
|
14
18
|
@jobs = []
|
|
15
19
|
@events = []
|
|
20
|
+
@schedules = {}
|
|
16
21
|
|
|
17
22
|
class << self
|
|
18
|
-
attr_reader :jobs, :events, :mode
|
|
23
|
+
attr_reader :jobs, :events, :schedules, :mode
|
|
19
24
|
|
|
20
25
|
def fake!
|
|
21
26
|
@mode = :fake
|
|
@@ -32,6 +37,24 @@ module Anway
|
|
|
32
37
|
def clear
|
|
33
38
|
@jobs.clear
|
|
34
39
|
@events.clear
|
|
40
|
+
@schedules.clear
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Keeps the entries of the tag, in the format of Anway::Cron, as the
|
|
44
|
+
# server does: they replace the entries of the tag, and no entries
|
|
45
|
+
# remove it. The server checks the schedules, and this does not.
|
|
46
|
+
def schedule(tag, entries)
|
|
47
|
+
entries = Cron.entries(entries.to_a)
|
|
48
|
+
|
|
49
|
+
entries.empty? ? @schedules.delete(tag.to_s) : @schedules[tag.to_s] = entries
|
|
50
|
+
|
|
51
|
+
true
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def unschedule(tag)
|
|
55
|
+
@schedules.delete(tag.to_s)
|
|
56
|
+
|
|
57
|
+
true
|
|
35
58
|
end
|
|
36
59
|
|
|
37
60
|
def enqueue(job)
|
data/lib/anway/version.rb
CHANGED
data/lib/anway/worker.rb
CHANGED
|
@@ -8,6 +8,7 @@ module Anway
|
|
|
8
8
|
#
|
|
9
9
|
# anway_options event: 'email.welcome', retry: 5
|
|
10
10
|
# anway_options topics: %w[user.created], queue: 'mailers'
|
|
11
|
+
# anway_options cron: '0 9 * * *'
|
|
11
12
|
#
|
|
12
13
|
# def perform(user_id); end
|
|
13
14
|
# end
|
|
@@ -18,7 +19,8 @@ module Anway
|
|
|
18
19
|
#
|
|
19
20
|
# The keys are the input contract (Anway::Options). The event is the job
|
|
20
21
|
# type on the wire, and the default is the class name. Each topic makes one
|
|
21
|
-
# subscription,
|
|
22
|
+
# subscription, and each cron schedule one cron entry. The worker sends both
|
|
23
|
+
# when it starts.
|
|
22
24
|
module Worker
|
|
23
25
|
DEFAULT_BACKTRACE = 30
|
|
24
26
|
|
|
@@ -27,7 +29,7 @@ module Anway
|
|
|
27
29
|
Subscription = Data.define(:topic, :queue, :event, :retry, :reserve_for)
|
|
28
30
|
|
|
29
31
|
# A subclass does not inherit these keys.
|
|
30
|
-
OWN_KEYS = %i[event topics].freeze
|
|
32
|
+
OWN_KEYS = %i[event topics cron].freeze
|
|
31
33
|
|
|
32
34
|
@registry = {}
|
|
33
35
|
@lock = Mutex.new
|
|
@@ -67,6 +69,12 @@ module Anway
|
|
|
67
69
|
classes.flat_map(&:subscriptions).uniq
|
|
68
70
|
end
|
|
69
71
|
|
|
72
|
+
# The cron entries of all registered classes, in the format of
|
|
73
|
+
# Anway::Cron. A worker sends them with SCHEDULE when it starts.
|
|
74
|
+
def cron_entries
|
|
75
|
+
classes.flat_map(&:cron_entries).uniq
|
|
76
|
+
end
|
|
77
|
+
|
|
70
78
|
# Only registered classes run. The event comes from the network, so it
|
|
71
79
|
# never becomes a constant lookup. The lookup by the class name below
|
|
72
80
|
# uses a registered name, and returns the newest class after a reload.
|
|
@@ -101,8 +109,8 @@ module Anway
|
|
|
101
109
|
# Anway::Options::DECLARATION, and an unknown key raises ArgumentError.
|
|
102
110
|
#
|
|
103
111
|
# Without keys, returns the options of the class: the options of the
|
|
104
|
-
# parent class without event and
|
|
105
|
-
# event and
|
|
112
|
+
# parent class without event, topics, and cron, then each set in call
|
|
113
|
+
# order. event, topics, and cron are always in the result.
|
|
106
114
|
def anway_options(**options)
|
|
107
115
|
return declared_options if options.empty?
|
|
108
116
|
|
|
@@ -139,6 +147,22 @@ module Anway
|
|
|
139
147
|
end
|
|
140
148
|
end
|
|
141
149
|
|
|
150
|
+
# One cron entry for each cron schedule. Its job has the event, queue,
|
|
151
|
+
# retry, and reserve_for of the class. Without a queue, the job goes to
|
|
152
|
+
# default.
|
|
153
|
+
def cron_entries
|
|
154
|
+
options = declared_options
|
|
155
|
+
|
|
156
|
+
job = {
|
|
157
|
+
'type' => options[:event],
|
|
158
|
+
'queue' => (options[:queue] || 'default').to_s,
|
|
159
|
+
'retry' => options[:retry],
|
|
160
|
+
'reserve_for' => options[:reserve_for]
|
|
161
|
+
}.compact
|
|
162
|
+
|
|
163
|
+
options[:cron].map { |schedule| { 'schedule' => schedule, 'job' => job } }
|
|
164
|
+
end
|
|
165
|
+
|
|
142
166
|
def perform_async(*args)
|
|
143
167
|
enqueue_with(args, {})
|
|
144
168
|
end
|
|
@@ -204,7 +228,7 @@ module Anway
|
|
|
204
228
|
def declared_options
|
|
205
229
|
parent = superclass.respond_to?(:anway_options) ? superclass.anway_options.except(*OWN_KEYS) : {}
|
|
206
230
|
|
|
207
|
-
Options.merge({ event: name, topics: [] }, parent, *@anway_option_sets).freeze
|
|
231
|
+
Options.merge({ event: name, topics: [], cron: [] }, parent, *@anway_option_sets).freeze
|
|
208
232
|
end
|
|
209
233
|
|
|
210
234
|
# A set with an event that another class has does not stay.
|
data/lib/anway.rb
CHANGED
|
@@ -4,6 +4,7 @@ require_relative 'anway/version'
|
|
|
4
4
|
require_relative 'anway/errors'
|
|
5
5
|
require_relative 'anway/configuration'
|
|
6
6
|
require_relative 'anway/options'
|
|
7
|
+
require_relative 'anway/cron'
|
|
7
8
|
require_relative 'anway/job'
|
|
8
9
|
require_relative 'anway/connection'
|
|
9
10
|
require_relative 'anway/pool'
|
|
@@ -117,6 +118,35 @@ module Anway
|
|
|
117
118
|
results
|
|
118
119
|
end
|
|
119
120
|
|
|
121
|
+
# Replaces all cron entries of the tag on the server. In a test mode,
|
|
122
|
+
# Anway::Testing keeps them in Testing.schedules.
|
|
123
|
+
#
|
|
124
|
+
# Anway.schedule('shop', [{ schedule: '0 0 * * *', job: { type: 'credits.refill' } }])
|
|
125
|
+
def schedule(tag, entries)
|
|
126
|
+
testing = const_defined?(:Testing) ? Testing : nil
|
|
127
|
+
|
|
128
|
+
return testing.schedule(tag, entries) if testing&.mode
|
|
129
|
+
|
|
130
|
+
client.schedule(tag, entries)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Removes all cron entries of the tag. A missing tag is not an error.
|
|
134
|
+
def unschedule(tag)
|
|
135
|
+
testing = const_defined?(:Testing) ? Testing : nil
|
|
136
|
+
|
|
137
|
+
return testing.unschedule(tag) if testing&.mode
|
|
138
|
+
|
|
139
|
+
client.unschedule(tag)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# The cron entries that the worker sends with SCHEDULE when it starts:
|
|
143
|
+
# those of the job classes (anway_options cron:), then those of the cron
|
|
144
|
+
# key of config/anway.yml, with no duplicates. An app spec can check its
|
|
145
|
+
# declarations with it.
|
|
146
|
+
def cron(settings = Settings.load)
|
|
147
|
+
(Worker.cron_entries + settings.cron).uniq
|
|
148
|
+
end
|
|
149
|
+
|
|
120
150
|
# The jobs on the server. With no set, a summary of each set. With a set
|
|
121
151
|
# (:waiting, :working, :retries, :scheduled, :dead or "queue:name"), an
|
|
122
152
|
# Enumerable of JobInfo that reads pages only as far as it is read.
|
|
@@ -18,6 +18,15 @@ concurrency: 10
|
|
|
18
18
|
# autoscale:
|
|
19
19
|
# max: 20
|
|
20
20
|
|
|
21
|
+
# Cron entries that the worker sends to the server when it starts, as the
|
|
22
|
+
# cron entries of the server conf.d. They need a tag. A job class can also
|
|
23
|
+
# declare them: anway_options cron: "0 0 * * *". Schedules use UTC.
|
|
24
|
+
# cron:
|
|
25
|
+
# - schedule: "0 0 * * *"
|
|
26
|
+
# job:
|
|
27
|
+
# type: credits.refill
|
|
28
|
+
# queue: default
|
|
29
|
+
|
|
21
30
|
# The web console. bin/anway starts it next to the server, and
|
|
22
31
|
# `mount Anway::Web` in config/routes.rb serves it in the app.
|
|
23
32
|
# console:
|
data/libexec/anway-server
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: anway
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: x86_64-linux-gnu
|
|
6
6
|
authors:
|
|
7
7
|
- Thadeu
|
|
@@ -39,6 +39,7 @@ files:
|
|
|
39
39
|
- lib/anway/configuration.rb
|
|
40
40
|
- lib/anway/connection.rb
|
|
41
41
|
- lib/anway/console.rb
|
|
42
|
+
- lib/anway/cron.rb
|
|
42
43
|
- lib/anway/daemon.rb
|
|
43
44
|
- lib/anway/errors.rb
|
|
44
45
|
- lib/anway/executor.rb
|