anway 0.5.2-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 +117 -2
- data/lib/anway/api.rb +73 -0
- data/lib/anway/cli.rb +41 -4
- data/lib/anway/client.rb +17 -0
- data/lib/anway/configuration.rb +2 -1
- data/lib/anway/cron.rb +46 -0
- data/lib/anway/daemon.rb +1 -1
- data/lib/anway/inspect.rb +93 -0
- data/lib/anway/inspect_command.rb +143 -0
- data/lib/anway/job_info.rb +47 -0
- data/lib/anway/options.rb +7 -5
- data/lib/anway/runner.rb +88 -6
- data/lib/anway/server.rb +7 -0
- data/lib/anway/settings.rb +57 -7
- 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 +77 -0
- data/lib/generators/anway/install/templates/anway.yml +9 -0
- data/libexec/anway-server +0 -0
- metadata +6 -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
|
|
@@ -325,6 +378,59 @@ At a stop, jobs get `--timeout` seconds to finish. Then `Anway::Shutdown` is rai
|
|
|
325
378
|
|
|
326
379
|
Keep the database pool at the concurrency value or more. A job queue with no worker has no error, and its jobs wait.
|
|
327
380
|
|
|
381
|
+
## Debugging jobs
|
|
382
|
+
|
|
383
|
+
When a job "did not run", ask the server. `anway jobs` reads its HTTP API with the app's settings, starts nothing, and needs no browser:
|
|
384
|
+
|
|
385
|
+
```
|
|
386
|
+
$ bin/anway jobs
|
|
387
|
+
waiting 3 oldest 4s mail.send
|
|
388
|
+
working 1 EmitInvoiceJob on mbp:84600
|
|
389
|
+
retries 2 EmitInvoiceJob ×2 Net::ReadTimeout: execution expired
|
|
390
|
+
scheduled 0
|
|
391
|
+
dead 0
|
|
392
|
+
|
|
393
|
+
$ bin/anway jobs --set retries --type EmitInvoice
|
|
394
|
+
JID TYPE QUEUE TRY NEXT ERROR
|
|
395
|
+
a1b2c3d4 EmitInvoiceJob default 2 in 38s Net::ReadTimeout: execution expired
|
|
396
|
+
|
|
397
|
+
$ bin/anway job a1b2c3d4
|
|
398
|
+
EmitInvoiceJob (Active Job, wrapper Anway::ActiveJobWrapper)
|
|
399
|
+
state retries (try 2, next run 16:42:10)
|
|
400
|
+
queue default
|
|
401
|
+
args ["15063a22-f96b-41d9-b744-411a0ba69833"]
|
|
402
|
+
error Net::ReadTimeout: execution expired
|
|
403
|
+
app/services/nfse/sefin_client.rb:88:in 'post'
|
|
404
|
+
app/jobs/emit_invoice_job.rb:14:in 'perform'
|
|
405
|
+
(8 lines more, --full shows all)
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
- `--set` is `waiting`, `working`, `retries`, `scheduled`, `dead`, or `queue:<name>`. `--type` matches a part of the job type or of the Active Job class. `--queue`, `--limit` (20), `--full`, and `--json` (the API answer, for scripts and agents).
|
|
409
|
+
- A finished job is not kept: `anway job` says `not found: it finished, or it never existed` and exits 1. With no server, it says `no server at ... (is bin/dev running?)` and exits 1.
|
|
410
|
+
- The API: an embedded server opens it on `127.0.0.1:7420` and saves its random token in its data folder (only your user can read it), so every process of the app finds it. For a server that runs apart, set `ANWAY_API_URL` and `ANWAY_WEB_TOKEN`, or `console.api` and `console.token`.
|
|
411
|
+
|
|
412
|
+
In the Rails console, or in the tests of the app:
|
|
413
|
+
|
|
414
|
+
```ruby
|
|
415
|
+
Anway.jobs # the summary
|
|
416
|
+
Anway.jobs(set: :retries, type: "EmitInvoiceJob").first(5)
|
|
417
|
+
Anway.job(EmitInvoiceJob.perform_later(id).provider_job_id) # Anway::JobInfo, or nil
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
`Anway::JobInfo` has `jid`, `type`, `wrapped`, `name`, `queue`, `state`, `args`, `custom`, `retry_count`, `next_at`, `enqueued_at`, `started_at`, `worker`, and `error` (`class`, `message`, `backtrace`). `Anway.jobs(set:)` reads pages only as far as you read. With `Anway::Testing.fake!`, both read the fake jobs.
|
|
421
|
+
|
|
422
|
+
### Job tags on log lines
|
|
423
|
+
|
|
424
|
+
While a job runs, every line of the app logger carries the job, Active Record and HTTP client lines too:
|
|
425
|
+
|
|
426
|
+
```
|
|
427
|
+
[anway EmitInvoiceJob jid=a1b2c3d4 try=2] Invoice Load (0.4ms) SELECT "invoices".* ...
|
|
428
|
+
anway done EmitInvoiceJob jid=a1b2c3d4 312ms
|
|
429
|
+
anway failed EmitInvoiceJob jid=a1b2c3d4 try=2 Net::ReadTimeout: execution expired (retry) 5012ms
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
So `grep jid=a1b2c3d4 log/development.log` shows one run, and `grep EmitInvoiceJob` every run of the class. The tags need a tagged logger, which `Rails.logger` is; with another logger nothing changes. Tags are per thread, so jobs on other threads keep their own. `log_tags: false` in `config/anway.yml` turns them off.
|
|
433
|
+
|
|
328
434
|
## Tests in your app
|
|
329
435
|
|
|
330
436
|
```ruby
|
|
@@ -341,6 +447,15 @@ Anway::Testing.events.last['topic']
|
|
|
341
447
|
Anway::Testing.inline!
|
|
342
448
|
```
|
|
343
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
|
+
|
|
344
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.
|
|
345
460
|
|
|
346
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/api.rb
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'net/http'
|
|
5
|
+
require 'uri'
|
|
6
|
+
|
|
7
|
+
module Anway
|
|
8
|
+
# Api reads the server's HTTP API (WEB_ADDR) with its web token. It is
|
|
9
|
+
# what `anway jobs` and Anway.jobs use.
|
|
10
|
+
class Api
|
|
11
|
+
# The server does not answer on the API address.
|
|
12
|
+
class Unreachable < Error; end
|
|
13
|
+
|
|
14
|
+
def self.from_settings(settings = Settings.load)
|
|
15
|
+
url = settings.api_url
|
|
16
|
+
|
|
17
|
+
raise Unreachable, 'no server API: set ANWAY_API_URL, or console.api in config/anway.yml' unless url
|
|
18
|
+
|
|
19
|
+
new(url: url, token: settings.api_token)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
attr_reader :url
|
|
23
|
+
|
|
24
|
+
def initialize(url:, token: nil, timeout: 5)
|
|
25
|
+
@url = url.to_s.chomp('/')
|
|
26
|
+
@token = token
|
|
27
|
+
@timeout = timeout
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def overview = get('/api/v1/overview')
|
|
31
|
+
|
|
32
|
+
def jobs(set:, cursor: '', limit: 50, queue: nil, jobtype: nil)
|
|
33
|
+
get('/api/v1/jobs', { set: set, cursor: cursor, limit: limit, queue: queue, jobtype: jobtype }.compact)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# One job in any set, or nil when it finished or never existed.
|
|
37
|
+
def job(jid)
|
|
38
|
+
get("/api/v1/jobs/#{URI.encode_www_form_component(jid.to_s)}")
|
|
39
|
+
rescue NotFound
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The answer was 404.
|
|
44
|
+
class NotFound < Error; end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def get(path, query = {})
|
|
49
|
+
uri = URI("#{@url}#{path}")
|
|
50
|
+
uri.query = URI.encode_www_form(query) unless query.empty?
|
|
51
|
+
|
|
52
|
+
request = Net::HTTP::Get.new(uri)
|
|
53
|
+
request['Authorization'] = "Bearer #{@token}" if @token
|
|
54
|
+
|
|
55
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', open_timeout: @timeout, read_timeout: @timeout) { |http| http.request(request) }
|
|
56
|
+
|
|
57
|
+
parse(response)
|
|
58
|
+
rescue SystemCallError, IOError, SocketError, Net::OpenTimeout, Net::ReadTimeout
|
|
59
|
+
raise Unreachable, "no server at #{@url} (is bin/dev running?)"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def parse(response)
|
|
63
|
+
body = JSON.parse(response.body.to_s.empty? ? '{}' : response.body)
|
|
64
|
+
|
|
65
|
+
raise NotFound, body['error'] || 'not found' if response.code == '404'
|
|
66
|
+
raise Error, "the server API answered #{response.code}: #{body['error']}" unless response.code.start_with?('2')
|
|
67
|
+
|
|
68
|
+
body
|
|
69
|
+
rescue JSON::ParserError
|
|
70
|
+
raise Error, "the server API answered #{response.code} without JSON"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
data/lib/anway/cli.rb
CHANGED
|
@@ -17,12 +17,14 @@ module Anway
|
|
|
17
17
|
# bundle exec anway server only the server
|
|
18
18
|
# bundle exec anway console only the console
|
|
19
19
|
# bundle exec anway worker -q mail only workers
|
|
20
|
+
# bundle exec anway jobs [--set retries --type MyJob] the jobs on the server
|
|
21
|
+
# bundle exec anway job JID [--full] one job, with its error
|
|
20
22
|
class CLI
|
|
21
23
|
EXIT_OK = 0
|
|
22
24
|
EXIT_BOOT = 1
|
|
23
25
|
EXIT_USAGE = 2
|
|
24
26
|
DEFAULTS = { concurrency: 10, timeout: 25, labels: [] }.freeze
|
|
25
|
-
COMMANDS = %w[server console worker].freeze
|
|
27
|
+
COMMANDS = %w[server console worker jobs job].freeze
|
|
26
28
|
RAILS_ENTRY = 'config/environment.rb'
|
|
27
29
|
|
|
28
30
|
def initialize(argv)
|
|
@@ -37,6 +39,8 @@ module Anway
|
|
|
37
39
|
|
|
38
40
|
boot
|
|
39
41
|
|
|
42
|
+
return inspect_jobs if %w[jobs job].include?(@command)
|
|
43
|
+
|
|
40
44
|
return Server.new(@settings).exec if @command == 'server'
|
|
41
45
|
return Console.new(@settings).exec if @command == 'console'
|
|
42
46
|
|
|
@@ -63,6 +67,9 @@ module Anway
|
|
|
63
67
|
parser.parse!(@argv)
|
|
64
68
|
|
|
65
69
|
@command = @argv.shift if COMMANDS.include?(@argv.first)
|
|
70
|
+
@jid = @argv.shift if @command == 'job'
|
|
71
|
+
|
|
72
|
+
raise ArgumentError, 'anway job needs a jid' if @command == 'job' && @jid.to_s.empty?
|
|
66
73
|
|
|
67
74
|
raise ArgumentError, "unexpected argument: #{@argv.first}" unless @argv.empty?
|
|
68
75
|
|
|
@@ -71,7 +78,7 @@ module Anway
|
|
|
71
78
|
|
|
72
79
|
def parser
|
|
73
80
|
OptionParser.new do |options|
|
|
74
|
-
options.banner = 'Usage: anway [server|console|worker] [options]'
|
|
81
|
+
options.banner = 'Usage: anway [server|console|worker|jobs|job JID] [options]'
|
|
75
82
|
|
|
76
83
|
options.on('-q', '--queues LIST', 'Queues in order, as name or name:weight') do |list|
|
|
77
84
|
@flags[:queues] = list.split(',').map { |entry| entry.split(':') }
|
|
@@ -86,6 +93,12 @@ module Anway
|
|
|
86
93
|
options.on('-g', '--tag NAME', 'Name of the app in the console') { |name| @flags[:tag] = name }
|
|
87
94
|
options.on('-a', '--autoscale MAX', Integer, 'Turn on autoscale, up to MAX threads') { |n| @flags[:autoscale] = { max: n } }
|
|
88
95
|
options.on('-v', '--verbose', 'Debug logs') { @flags[:verbose] = true }
|
|
96
|
+
options.on('--set SET', 'jobs: waiting, working, retries, scheduled, dead, or queue:NAME') { |set| @flags[:set] = set }
|
|
97
|
+
options.on('--type TYPE', 'jobs: a part of the job type or Active Job class') { |type| @flags[:type] = type }
|
|
98
|
+
options.on('--queue NAME', 'jobs: only this queue') { |name| @flags[:queue] = name }
|
|
99
|
+
options.on('--limit N', Integer, 'jobs: at most N jobs (20)') { |n| @flags[:limit] = n }
|
|
100
|
+
options.on('--full', 'job: the whole backtrace') { @flags[:full] = true }
|
|
101
|
+
options.on('--json', 'jobs, job: the API answer as JSON') { @flags[:json] = true }
|
|
89
102
|
|
|
90
103
|
options.on('-V', '--version', 'Print the version') do
|
|
91
104
|
puts "anway #{VERSION}"
|
|
@@ -101,6 +114,26 @@ module Anway
|
|
|
101
114
|
end
|
|
102
115
|
end
|
|
103
116
|
|
|
117
|
+
# anway jobs and anway job read the server's HTTP API. They need no app
|
|
118
|
+
# and start nothing. When no server answers, they say so and exit 1.
|
|
119
|
+
def inspect_jobs
|
|
120
|
+
command = InspectCommand.new(Api.from_settings(@settings))
|
|
121
|
+
|
|
122
|
+
return (command.show(@jid, full: @flags[:full], json: @flags[:json]) ? EXIT_OK : EXIT_BOOT) if @command == 'job'
|
|
123
|
+
|
|
124
|
+
if @flags[:set] || @flags[:type]
|
|
125
|
+
command.list(set: @flags[:set] || 'waiting', type: @flags[:type], queue: @flags[:queue], limit: @flags[:limit] || 20, json: @flags[:json])
|
|
126
|
+
else
|
|
127
|
+
command.summary(json: @flags[:json])
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
EXIT_OK
|
|
131
|
+
rescue Api::Unreachable => e
|
|
132
|
+
warn("anway: #{e.message}")
|
|
133
|
+
|
|
134
|
+
EXIT_BOOT
|
|
135
|
+
end
|
|
136
|
+
|
|
104
137
|
def boot
|
|
105
138
|
environment = @flags[:environment] || ENV['RAILS_ENV'] || ENV['APP_ENV'] || 'development'
|
|
106
139
|
|
|
@@ -111,6 +144,7 @@ module Anway
|
|
|
111
144
|
@options = DEFAULTS.merge(@settings.worker_options).merge(@flags)
|
|
112
145
|
|
|
113
146
|
Anway.config.logger.level = Logger::DEBUG if @options[:verbose]
|
|
147
|
+
Anway.config.log_tags = @settings.log_tags?
|
|
114
148
|
end
|
|
115
149
|
|
|
116
150
|
# The app loads first, so that a usage error never leaves a server running.
|
|
@@ -175,6 +209,8 @@ module Anway
|
|
|
175
209
|
labels: @options[:labels],
|
|
176
210
|
hostname: @options[:hostname],
|
|
177
211
|
tag: @options[:tag] || default_tag,
|
|
212
|
+
cron: Anway.cron(@settings),
|
|
213
|
+
cron_tag: @options[:tag],
|
|
178
214
|
autoscale: @options[:autoscale],
|
|
179
215
|
limit: database_pool
|
|
180
216
|
)
|
|
@@ -199,14 +235,15 @@ module Anway
|
|
|
199
235
|
Signal.trap('TTIN') { runner.dump }
|
|
200
236
|
end
|
|
201
237
|
|
|
202
|
-
# Without a list, the worker uses the queues of its job classes
|
|
203
|
-
#
|
|
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
|
|
204
240
|
# subscription queue that is not in it.
|
|
205
241
|
def queues
|
|
206
242
|
return @options[:queues] if @options[:queues]
|
|
207
243
|
|
|
208
244
|
names = Worker.classes.map { |klass| klass.anway_options[:queue]&.to_s }
|
|
209
245
|
names.concat(Worker.subscriptions.map(&:queue))
|
|
246
|
+
names.concat(Anway.cron(@settings).map { |entry| entry.dig('job', 'queue') || 'default' })
|
|
210
247
|
names.concat(active_job_queues)
|
|
211
248
|
|
|
212
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/configuration.rb
CHANGED
|
@@ -4,7 +4,7 @@ require 'logger'
|
|
|
4
4
|
|
|
5
5
|
module Anway
|
|
6
6
|
class Configuration
|
|
7
|
-
attr_accessor :url, :console_url, :pool_size, :timeout, :tls, :logger, :push_wrapper, :reloader
|
|
7
|
+
attr_accessor :url, :console_url, :pool_size, :timeout, :tls, :logger, :push_wrapper, :reloader, :log_tags
|
|
8
8
|
attr_reader :middleware, :error_handlers
|
|
9
9
|
|
|
10
10
|
def initialize
|
|
@@ -13,6 +13,7 @@ module Anway
|
|
|
13
13
|
@pool_size = 8
|
|
14
14
|
@timeout = 10
|
|
15
15
|
@tls = nil
|
|
16
|
+
@log_tags = true
|
|
16
17
|
@logger = Logger.new($stdout, level: Logger::INFO)
|
|
17
18
|
@push_wrapper = ->(&block) { block.call }
|
|
18
19
|
@reloader = ->(&block) { block.call }
|
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/daemon.rb
CHANGED
|
@@ -26,7 +26,7 @@ module Anway
|
|
|
26
26
|
# random web token, that process has another token, so the start fails.
|
|
27
27
|
def start
|
|
28
28
|
if listening?
|
|
29
|
-
raise Error, "a #{name} already listens on #{address} with another web token. Stop it, or set server.web_token in config/anway.yml." if @settings.random_web_token?
|
|
29
|
+
raise Error, "a #{name} already listens on #{address} with another web token. Stop it, or set server.web_token in config/anway.yml." if @settings.random_web_token? && !@settings.saved_web_token?
|
|
30
30
|
|
|
31
31
|
@logger.info("anway: a #{name} already listens on #{address}, so this process uses it")
|
|
32
32
|
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anway
|
|
4
|
+
# Inspect reads the jobs on the server for people: `anway jobs`,
|
|
5
|
+
# Anway.jobs and Anway.job. With Anway::Testing.fake!, it reads the fake
|
|
6
|
+
# jobs instead, so app tests use the same calls.
|
|
7
|
+
module Inspect
|
|
8
|
+
SETS = %w[waiting working retries scheduled dead].freeze
|
|
9
|
+
|
|
10
|
+
# Summary is one line for each set: how many jobs, and the most common
|
|
11
|
+
# job type, worker and error in its first page.
|
|
12
|
+
Summary = Data.define(:sets) do
|
|
13
|
+
def [](set) = sets.find { |line| line[:set] == set.to_s }
|
|
14
|
+
|
|
15
|
+
def to_h = { sets: sets }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Jobs reads a set page by page, only as far as it is read, so
|
|
19
|
+
# `first(5)` asks for one page. type matches a part of the job type or
|
|
20
|
+
# of the Active Job class.
|
|
21
|
+
class Jobs
|
|
22
|
+
include Enumerable
|
|
23
|
+
|
|
24
|
+
def initialize(api, set:, type: nil, queue: nil, page: 50)
|
|
25
|
+
@api = api
|
|
26
|
+
@set = set.to_s
|
|
27
|
+
@type = type&.to_s
|
|
28
|
+
@queue = queue
|
|
29
|
+
@page = page
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def each
|
|
33
|
+
return enum_for(:each) unless block_given?
|
|
34
|
+
|
|
35
|
+
cursor = ''
|
|
36
|
+
|
|
37
|
+
loop do
|
|
38
|
+
reply = @api.jobs(set: @set, cursor: cursor, limit: @page, queue: @queue)
|
|
39
|
+
|
|
40
|
+
reply.fetch('jobs', []).each do |view|
|
|
41
|
+
info = JobInfo.from_api(view, set: set_name)
|
|
42
|
+
|
|
43
|
+
yield info if match?(info)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
cursor = reply['next_cursor'].to_s
|
|
47
|
+
|
|
48
|
+
break if cursor.empty?
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def set_name = @set.start_with?('queue:') ? 'waiting' : @set
|
|
55
|
+
|
|
56
|
+
def match?(info)
|
|
57
|
+
@type.nil? || info.type.to_s.include?(@type) || info.wrapped.to_s.include?(@type)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
module_function
|
|
62
|
+
|
|
63
|
+
def summary(api)
|
|
64
|
+
counts = api.overview.fetch('states', {})
|
|
65
|
+
|
|
66
|
+
sets = SETS.map do |set|
|
|
67
|
+
count = counts[set].to_i
|
|
68
|
+
sample = count.zero? ? [] : Jobs.new(api, set: set, page: 50).first(50)
|
|
69
|
+
|
|
70
|
+
{ set: set, count: count, top: top(sample, &:name), worker: top(sample, &:worker), error: top(sample) { |info| info.error && "#{info.error[:class]}: #{info.error[:message]}" }, oldest: sample.filter_map(&:enqueued_at).min }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
Summary.new(sets: sets)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def top(infos)
|
|
77
|
+
counts = infos.filter_map { |info| yield(info) }.tally
|
|
78
|
+
|
|
79
|
+
return nil if counts.empty?
|
|
80
|
+
|
|
81
|
+
value, count = counts.max_by { |_, n| n }
|
|
82
|
+
|
|
83
|
+
{ value: value, count: count }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The fake jobs, as JobInfo in the waiting set.
|
|
87
|
+
def fake(jobs)
|
|
88
|
+
jobs.map do |job|
|
|
89
|
+
JobInfo.from_api({ 'jid' => job.jid, 'jobtype' => job.type, 'queue' => job.queue || 'default', 'args' => job.args, 'custom' => job.custom, 'enqueued_at' => job.enqueued_at }, set: 'waiting')
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|