anway 0.5.2-x86_64-linux-gnu → 0.5.3-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 +53 -0
- data/lib/anway/api.rb +73 -0
- data/lib/anway/cli.rb +36 -2
- data/lib/anway/configuration.rb +2 -1
- 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/runner.rb +46 -4
- data/lib/anway/server.rb +7 -0
- data/lib/anway/settings.rb +45 -7
- data/lib/anway/version.rb +2 -2
- data/lib/anway.rb +47 -0
- data/libexec/anway-server +0 -0
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dfd5f3c13b1ccda6d6a1def236f7a261e0256373d242b76c7c67a5f74ea3ddd1
|
|
4
|
+
data.tar.gz: 92b13ba385af46a8d5fecf5fd0c0b1750c2abaec45a4510822b2d917b4e024ab
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 05b6ac45de5a52ff55aaf7ecdc4bcba62d1ba07bc0f87c6d5ab4e9d2be5718684e901c4e7db87e36b503aefe9c07f3af8f687abae724258dbcd015acf63e7573
|
|
7
|
+
data.tar.gz: 7c4b72b5d9d12144567026c086ed16a658ff80eef4c65dab05ce98ac5441dbf5287d015597ba7a6335928d2334996bab3c68dae4fcd9814cb8f4558e84a3122f
|
data/README.md
CHANGED
|
@@ -325,6 +325,59 @@ At a stop, jobs get `--timeout` seconds to finish. Then `Anway::Shutdown` is rai
|
|
|
325
325
|
|
|
326
326
|
Keep the database pool at the concurrency value or more. A job queue with no worker has no error, and its jobs wait.
|
|
327
327
|
|
|
328
|
+
## Debugging jobs
|
|
329
|
+
|
|
330
|
+
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:
|
|
331
|
+
|
|
332
|
+
```
|
|
333
|
+
$ bin/anway jobs
|
|
334
|
+
waiting 3 oldest 4s mail.send
|
|
335
|
+
working 1 EmitInvoiceJob on mbp:84600
|
|
336
|
+
retries 2 EmitInvoiceJob ×2 Net::ReadTimeout: execution expired
|
|
337
|
+
scheduled 0
|
|
338
|
+
dead 0
|
|
339
|
+
|
|
340
|
+
$ bin/anway jobs --set retries --type EmitInvoice
|
|
341
|
+
JID TYPE QUEUE TRY NEXT ERROR
|
|
342
|
+
a1b2c3d4 EmitInvoiceJob default 2 in 38s Net::ReadTimeout: execution expired
|
|
343
|
+
|
|
344
|
+
$ bin/anway job a1b2c3d4
|
|
345
|
+
EmitInvoiceJob (Active Job, wrapper Anway::ActiveJobWrapper)
|
|
346
|
+
state retries (try 2, next run 16:42:10)
|
|
347
|
+
queue default
|
|
348
|
+
args ["15063a22-f96b-41d9-b744-411a0ba69833"]
|
|
349
|
+
error Net::ReadTimeout: execution expired
|
|
350
|
+
app/services/nfse/sefin_client.rb:88:in 'post'
|
|
351
|
+
app/jobs/emit_invoice_job.rb:14:in 'perform'
|
|
352
|
+
(8 lines more, --full shows all)
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
- `--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).
|
|
356
|
+
- 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.
|
|
357
|
+
- 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`.
|
|
358
|
+
|
|
359
|
+
In the Rails console, or in the tests of the app:
|
|
360
|
+
|
|
361
|
+
```ruby
|
|
362
|
+
Anway.jobs # the summary
|
|
363
|
+
Anway.jobs(set: :retries, type: "EmitInvoiceJob").first(5)
|
|
364
|
+
Anway.job(EmitInvoiceJob.perform_later(id).provider_job_id) # Anway::JobInfo, or nil
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
`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.
|
|
368
|
+
|
|
369
|
+
### Job tags on log lines
|
|
370
|
+
|
|
371
|
+
While a job runs, every line of the app logger carries the job, Active Record and HTTP client lines too:
|
|
372
|
+
|
|
373
|
+
```
|
|
374
|
+
[anway EmitInvoiceJob jid=a1b2c3d4 try=2] Invoice Load (0.4ms) SELECT "invoices".* ...
|
|
375
|
+
anway done EmitInvoiceJob jid=a1b2c3d4 312ms
|
|
376
|
+
anway failed EmitInvoiceJob jid=a1b2c3d4 try=2 Net::ReadTimeout: execution expired (retry) 5012ms
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
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.
|
|
380
|
+
|
|
328
381
|
## Tests in your app
|
|
329
382
|
|
|
330
383
|
```ruby
|
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.
|
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/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
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module Anway
|
|
6
|
+
# InspectCommand prints `anway jobs` and `anway job <jid>`.
|
|
7
|
+
#
|
|
8
|
+
# anway jobs one line for each set
|
|
9
|
+
# anway jobs --set retries --type EmitInvoice
|
|
10
|
+
# anway job a1b2c3d4 [--full]
|
|
11
|
+
# --json prints the API answer, for scripts and agents
|
|
12
|
+
class InspectCommand
|
|
13
|
+
BACKTRACE = 3
|
|
14
|
+
|
|
15
|
+
def initialize(api, out: $stdout, now: Time.now)
|
|
16
|
+
@api = api
|
|
17
|
+
@out = out
|
|
18
|
+
@now = now
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def summary(json: false)
|
|
22
|
+
summary = Inspect.summary(@api)
|
|
23
|
+
|
|
24
|
+
return print_json(summary.to_h) if json
|
|
25
|
+
|
|
26
|
+
summary.sets.each { |line| @out.puts(summary_line(line)) }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def list(set:, type: nil, queue: nil, limit: 20, json: false)
|
|
30
|
+
infos = Inspect::Jobs.new(@api, set: set, type: type, queue: queue).first(limit)
|
|
31
|
+
|
|
32
|
+
return print_json(infos.map(&:to_h)) if json
|
|
33
|
+
return @out.puts("no #{set} jobs#{" of #{type}" if type}") if infos.empty?
|
|
34
|
+
|
|
35
|
+
rows = infos.map { |info| [short(info.jid), info.name, info.queue, try(info), when_of(info), error_line(info)] }
|
|
36
|
+
|
|
37
|
+
table(%w[JID TYPE QUEUE TRY NEXT ERROR], rows)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Returns false when the job is not found.
|
|
41
|
+
def show(jid, full: false, json: false)
|
|
42
|
+
reply = @api.job(jid)
|
|
43
|
+
|
|
44
|
+
unless reply
|
|
45
|
+
warn("anway: not found: it finished, or it never existed (#{jid})")
|
|
46
|
+
|
|
47
|
+
return false
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
return print_json(reply) || true if json
|
|
51
|
+
|
|
52
|
+
describe(JobInfo.from_api(reply['job'], set: reply['set']), full)
|
|
53
|
+
|
|
54
|
+
true
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def summary_line(line)
|
|
60
|
+
parts = [line[:set].ljust(9), line[:count].to_s.rjust(4)]
|
|
61
|
+
|
|
62
|
+
parts << "oldest #{ago(line[:oldest])}" if line[:set] == 'waiting' && line[:oldest]
|
|
63
|
+
parts << top_line(line[:top], line[:set] == 'working' ? line[:worker] : nil) if line[:top]
|
|
64
|
+
parts << line[:error][:value] if line[:error]
|
|
65
|
+
|
|
66
|
+
parts.join(' ')
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def top_line(top, worker)
|
|
70
|
+
text = top[:count] > 1 ? "#{top[:value]} ×#{top[:count]}" : top[:value]
|
|
71
|
+
|
|
72
|
+
worker ? "#{text} on #{worker[:value]}" : text
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def describe(info, full)
|
|
76
|
+
label = info.wrapped ? "#{info.wrapped} (Active Job, wrapper #{info.type})" : info.type
|
|
77
|
+
|
|
78
|
+
@out.puts(label)
|
|
79
|
+
|
|
80
|
+
field('state', state_text(info))
|
|
81
|
+
field('queue', info.queue)
|
|
82
|
+
field('args', JSON.generate(info.args))
|
|
83
|
+
field('custom', JSON.generate(info.custom)) unless info.custom.empty?
|
|
84
|
+
field('enqueued', clock(info.enqueued_at)) if info.enqueued_at
|
|
85
|
+
field('started', "#{clock(info.started_at)} on #{info.worker}") if info.started_at
|
|
86
|
+
|
|
87
|
+
error(info.error, full) if info.error
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def error(error, full)
|
|
91
|
+
field('error', "#{error[:class]}: #{error[:message]}")
|
|
92
|
+
|
|
93
|
+
lines = full ? error[:backtrace] : error[:backtrace].first(BACKTRACE)
|
|
94
|
+
|
|
95
|
+
lines.each { |line| @out.puts(" #{line}") }
|
|
96
|
+
|
|
97
|
+
rest = error[:backtrace].size - lines.size
|
|
98
|
+
|
|
99
|
+
@out.puts(" (#{rest} lines more, --full shows all)") if rest.positive?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def state_text(info)
|
|
103
|
+
return info.state unless %w[retries scheduled].include?(info.state) && info.next_at
|
|
104
|
+
|
|
105
|
+
"#{info.state} (#{"try #{info.retry_count}, " if info.retry_count.positive?}next run #{clock(info.next_at)})"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def field(name, value) = @out.puts("#{name.ljust(9)} #{value}")
|
|
109
|
+
|
|
110
|
+
def table(headers, rows)
|
|
111
|
+
widths = headers.each_index.map { |i| ([headers[i]] + rows.map { |row| row[i].to_s }).map(&:length).max }
|
|
112
|
+
|
|
113
|
+
[headers, *rows].each { |row| @out.puts(row.each_with_index.map { |cell, i| cell.to_s.ljust(widths[i]) }.join(' ').rstrip) }
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def short(jid) = jid.to_s[0, 8]
|
|
117
|
+
|
|
118
|
+
def try(info) = info.retry_count.positive? ? info.retry_count.to_s : '-'
|
|
119
|
+
|
|
120
|
+
def when_of(info) = info.next_at ? "in #{seconds(info.next_at - @now)}" : '-'
|
|
121
|
+
|
|
122
|
+
def error_line(info) = info.error ? "#{info.error[:class]}: #{info.error[:message]}"[0, 60] : ''
|
|
123
|
+
|
|
124
|
+
def ago(time) = seconds(@now - time)
|
|
125
|
+
|
|
126
|
+
def seconds(value)
|
|
127
|
+
value = value.round
|
|
128
|
+
|
|
129
|
+
return "#{value}s" if value.abs < 120
|
|
130
|
+
return "#{value / 60}m" if value.abs < 7200
|
|
131
|
+
|
|
132
|
+
"#{value / 3600}h"
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def clock(time) = time.localtime.strftime('%H:%M:%S')
|
|
136
|
+
|
|
137
|
+
def print_json(value)
|
|
138
|
+
@out.puts(JSON.pretty_generate(value))
|
|
139
|
+
|
|
140
|
+
nil
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
|
|
5
|
+
module Anway
|
|
6
|
+
# JobInfo is a read-only view of one job on the server, from the HTTP API
|
|
7
|
+
# (Anway.job, Anway.jobs, `anway job`).
|
|
8
|
+
#
|
|
9
|
+
# state is waiting, working, retries, scheduled or dead. wrapped is the
|
|
10
|
+
# Active Job class when the job type is the Active Job wrapper. error has
|
|
11
|
+
# class, message and backtrace when the job failed.
|
|
12
|
+
JobInfo = Data.define(
|
|
13
|
+
:jid, :type, :wrapped, :queue, :state, :args, :custom, :retry_count, :retry_max,
|
|
14
|
+
:next_at, :enqueued_at, :started_at, :worker, :error
|
|
15
|
+
) do
|
|
16
|
+
def self.from_api(view, set: nil)
|
|
17
|
+
custom = view['custom'] || {}
|
|
18
|
+
failure = view['failure']
|
|
19
|
+
|
|
20
|
+
new(
|
|
21
|
+
jid: view['jid'],
|
|
22
|
+
type: view['jobtype'],
|
|
23
|
+
wrapped: custom['wrapped'],
|
|
24
|
+
queue: view['queue'],
|
|
25
|
+
state: set || state_of(view['state']),
|
|
26
|
+
args: view['args'] || [],
|
|
27
|
+
custom: custom,
|
|
28
|
+
retry_count: failure ? failure['retry_count'].to_i + 1 : 0,
|
|
29
|
+
retry_max: view['retry'],
|
|
30
|
+
next_at: time(failure&.dig('next_at')) || (%w[scheduled retries].include?(set) ? time(view['at']) : nil),
|
|
31
|
+
enqueued_at: time(view['enqueued_at']),
|
|
32
|
+
started_at: time(view['started_at']),
|
|
33
|
+
worker: view['worker'],
|
|
34
|
+
error: failure && { class: failure['errtype'], message: failure['message'], backtrace: failure['backtrace'] || [] }
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.state_of(state) = (state == 'queues') ? 'waiting' : state
|
|
39
|
+
|
|
40
|
+
def self.time(value)
|
|
41
|
+
Time.parse(value.to_s) if value && !value.to_s.empty?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The class to show: the Active Job class, else the job type.
|
|
45
|
+
def name = wrapped || type
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/anway/runner.rb
CHANGED
|
@@ -13,6 +13,7 @@ module Anway
|
|
|
13
13
|
ERROR_PAUSE = 1
|
|
14
14
|
EMPTY_FETCH_MINIMUM = 0.2
|
|
15
15
|
BACKTRACE_LINES = 30
|
|
16
|
+
DEFAULT_RETRY = 25
|
|
16
17
|
|
|
17
18
|
attr_reader :wid
|
|
18
19
|
|
|
@@ -270,18 +271,59 @@ module Anway
|
|
|
270
271
|
end
|
|
271
272
|
|
|
272
273
|
def perform(job)
|
|
273
|
-
|
|
274
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
275
|
+
|
|
276
|
+
tagged(job) { Thread.handle_interrupt(Shutdown => :immediate) { Executor.call(job) } }
|
|
277
|
+
|
|
278
|
+
@logger.info("anway done #{job_label(job)} #{elapsed(started)}ms")
|
|
274
279
|
|
|
275
280
|
nil
|
|
276
281
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
277
|
-
|
|
282
|
+
unless e.is_a?(Shutdown)
|
|
283
|
+
@logger.warn("anway failed #{job_label(job)} try=#{try_of(job)} #{e.class}: #{e.message} (#{after_failure(job)}) #{elapsed(started)}ms")
|
|
284
|
+
|
|
285
|
+
notify_error(e, job)
|
|
286
|
+
end
|
|
278
287
|
|
|
279
288
|
e
|
|
280
289
|
end
|
|
281
290
|
|
|
282
|
-
|
|
283
|
-
|
|
291
|
+
# While a job runs, every line of a tagged logger (Rails.logger, and with
|
|
292
|
+
# it Active Record) carries the job. Tags are per thread, so jobs on two
|
|
293
|
+
# threads keep their own. A logger without tags, or an error in it, never
|
|
294
|
+
# stops the job.
|
|
295
|
+
def tagged(job, &block)
|
|
296
|
+
logger = Anway.config.logger
|
|
284
297
|
|
|
298
|
+
return yield unless Anway.config.log_tags && logger.respond_to?(:tagged)
|
|
299
|
+
|
|
300
|
+
logger.tagged("anway #{job_label(job)} try=#{try_of(job)}", &block)
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
def job_label(job)
|
|
304
|
+
wrapped = job.custom.is_a?(Hash) ? job.custom['wrapped'] : nil
|
|
305
|
+
|
|
306
|
+
"#{wrapped || job.type} jid=#{job.jid}"
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def try_of(job)
|
|
310
|
+
job.failure.is_a?(Hash) ? job.failure['retry_count'].to_i + 2 : 1
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# What the server does with this failure (job-lifecycle.md): retry while
|
|
314
|
+
# retries are left, then dead; retry 0 discards, retry -1 goes to dead.
|
|
315
|
+
def after_failure(job)
|
|
316
|
+
retries = job.retry.nil? ? DEFAULT_RETRY : job.retry.to_i
|
|
317
|
+
|
|
318
|
+
return 'discarded' if retries.zero?
|
|
319
|
+
return 'dead' if retries.negative? || try_of(job) > retries
|
|
320
|
+
|
|
321
|
+
'retry'
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def elapsed(started) = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round
|
|
325
|
+
|
|
326
|
+
def notify_error(error, job)
|
|
285
327
|
Anway.config.error_handlers.each { |handler| handler.call(error, job) }
|
|
286
328
|
rescue StandardError => e
|
|
287
329
|
@logger.error("error handler failed: #{e.message}")
|
data/lib/anway/server.rb
CHANGED
|
@@ -38,6 +38,13 @@ module Anway
|
|
|
38
38
|
|
|
39
39
|
def prepare
|
|
40
40
|
FileUtils.mkdir_p(@settings.data)
|
|
41
|
+
|
|
42
|
+
save_web_token if @settings.random_web_token?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def save_web_token
|
|
46
|
+
File.open(@settings.web_token_file, File::WRONLY | File::CREAT | File::TRUNC, 0o600) { |file| file.write(@settings.web_token) }
|
|
47
|
+
File.chmod(0o600, @settings.web_token_file)
|
|
41
48
|
end
|
|
42
49
|
|
|
43
50
|
def announce
|
data/lib/anway/settings.rb
CHANGED
|
@@ -23,7 +23,7 @@ module Anway
|
|
|
23
23
|
DEFAULT_WEB = '127.0.0.1:7420'
|
|
24
24
|
DEFAULT_CONSOLE = '127.0.0.1:7421'
|
|
25
25
|
MODES = %w[embedded external].freeze
|
|
26
|
-
KEYS = %w[queues concurrency timeout labels hostname tag autoscale require url server console].freeze
|
|
26
|
+
KEYS = %w[queues concurrency timeout labels hostname tag autoscale require url server console log_tags].freeze
|
|
27
27
|
SERVER_KEYS = %w[mode bind data password web web_token].freeze
|
|
28
28
|
CONSOLE_KEYS = %w[path bind api token].freeze
|
|
29
29
|
|
|
@@ -65,6 +65,11 @@ module Anway
|
|
|
65
65
|
options.compact
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
+
# log_tags: false turns off the job tags on log lines. On by default.
|
|
69
|
+
def log_tags?
|
|
70
|
+
@values['log_tags'] != false
|
|
71
|
+
end
|
|
72
|
+
|
|
68
73
|
# autoscale: { max: n } or { min: n, max: n }, or nil when it is off.
|
|
69
74
|
def autoscale
|
|
70
75
|
value = @values['autoscale']
|
|
@@ -110,16 +115,43 @@ module Anway
|
|
|
110
115
|
"tcp://#{auth}#{Daemon.local_host(host)}:#{port}"
|
|
111
116
|
end
|
|
112
117
|
|
|
113
|
-
# The address of the HTTP API that the web console
|
|
114
|
-
#
|
|
118
|
+
# The address of the HTTP API that the web console and `anway jobs` read.
|
|
119
|
+
# An embedded server opens it on the loopback; an external one only when
|
|
120
|
+
# it is set.
|
|
115
121
|
def web
|
|
116
|
-
present(@server['web']) || (
|
|
122
|
+
present(@server['web']) || (embedded? ? DEFAULT_WEB : nil)
|
|
117
123
|
end
|
|
118
124
|
|
|
119
|
-
# Without a token in the file, the server and the console
|
|
120
|
-
#
|
|
125
|
+
# Without a token in the file, the server and the console share a random
|
|
126
|
+
# one. A running embedded server saved its token in the data folder, so
|
|
127
|
+
# every process of the app (Puma, workers, `anway jobs`) uses the same.
|
|
121
128
|
def web_token
|
|
122
|
-
present(@server['web_token']) || (web ? (@random_token ||= SecureRandom.hex(16)) : nil)
|
|
129
|
+
present(@server['web_token']) || (web ? (@random_token ||= saved_web_token || SecureRandom.hex(16)) : nil)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# True when a random token was saved by a server of this data folder, so
|
|
133
|
+
# another process can use that server.
|
|
134
|
+
def saved_web_token?
|
|
135
|
+
!saved_web_token.nil?
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# The file where an embedded server keeps its random web token, so that
|
|
139
|
+
# `anway jobs` in another process can read the API. Only this user can
|
|
140
|
+
# read it.
|
|
141
|
+
def web_token_file
|
|
142
|
+
File.join(File.expand_path(data), 'web_token')
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# The HTTP API that `anway jobs` and Anway.jobs read: ANWAY_API_URL, then
|
|
146
|
+
# console.api, then the embedded server's API.
|
|
147
|
+
def api_url
|
|
148
|
+
present(@env['ANWAY_API_URL']) || console_api
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Its token: ANWAY_WEB_TOKEN, then console.token, then server.web_token,
|
|
152
|
+
# then the token file of a running embedded server.
|
|
153
|
+
def api_token
|
|
154
|
+
present(@env['ANWAY_WEB_TOKEN']) || present(@console['token']) || present(@server['web_token']) || saved_web_token
|
|
123
155
|
end
|
|
124
156
|
|
|
125
157
|
# A random token changes at each start, so a server or console from an
|
|
@@ -180,6 +212,12 @@ module Anway
|
|
|
180
212
|
|
|
181
213
|
private
|
|
182
214
|
|
|
215
|
+
def saved_web_token
|
|
216
|
+
present(File.read(web_token_file).strip)
|
|
217
|
+
rescue SystemCallError
|
|
218
|
+
nil
|
|
219
|
+
end
|
|
220
|
+
|
|
183
221
|
def section_of(hash, name, keys)
|
|
184
222
|
value = hash[name]
|
|
185
223
|
|
data/lib/anway/version.rb
CHANGED
data/lib/anway.rb
CHANGED
|
@@ -17,6 +17,10 @@ require_relative 'anway/daemon'
|
|
|
17
17
|
require_relative 'anway/server'
|
|
18
18
|
require_relative 'anway/console'
|
|
19
19
|
require_relative 'anway/web'
|
|
20
|
+
require_relative 'anway/api'
|
|
21
|
+
require_relative 'anway/job_info'
|
|
22
|
+
require_relative 'anway/inspect'
|
|
23
|
+
require_relative 'anway/inspect_command'
|
|
20
24
|
require_relative 'anway/puma/plugin'
|
|
21
25
|
require_relative 'anway/railtie' if defined?(Rails::Railtie)
|
|
22
26
|
|
|
@@ -113,11 +117,54 @@ module Anway
|
|
|
113
117
|
results
|
|
114
118
|
end
|
|
115
119
|
|
|
120
|
+
# The jobs on the server. With no set, a summary of each set. With a set
|
|
121
|
+
# (:waiting, :working, :retries, :scheduled, :dead or "queue:name"), an
|
|
122
|
+
# Enumerable of JobInfo that reads pages only as far as it is read.
|
|
123
|
+
#
|
|
124
|
+
# Anway.jobs
|
|
125
|
+
# Anway.jobs(set: :retries, type: "EmitInvoiceJob").first(5)
|
|
126
|
+
def jobs(set: nil, type: nil, queue: nil)
|
|
127
|
+
if fake_testing?
|
|
128
|
+
infos = Inspect.fake(Testing.jobs)
|
|
129
|
+
|
|
130
|
+
return set ? infos.select { |info| set.to_s == 'waiting' && (type.nil? || info.name.include?(type.to_s)) && (queue.nil? || info.queue == queue) } : Inspect::Summary.new(sets: [{ set: 'waiting', count: infos.size }])
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
return Inspect.summary(api) unless set
|
|
134
|
+
|
|
135
|
+
Inspect::Jobs.new(api, set: set, type: type, queue: queue)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# One job by its jid, as JobInfo, or nil when it finished or never
|
|
139
|
+
# existed. Active Job sets provider_job_id to the jid:
|
|
140
|
+
#
|
|
141
|
+
# Anway.job(MyJob.perform_later(id).provider_job_id)
|
|
142
|
+
def job(jid)
|
|
143
|
+
return Inspect.fake(Testing.jobs).find { |info| info.jid == jid } if fake_testing?
|
|
144
|
+
|
|
145
|
+
reply = api.job(jid)
|
|
146
|
+
|
|
147
|
+
reply && JobInfo.from_api(reply['job'], set: reply['set'])
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The HTTP API client for Anway.jobs and Anway.job.
|
|
151
|
+
def api
|
|
152
|
+
@api ||= Api.from_settings
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
attr_writer :api
|
|
156
|
+
|
|
116
157
|
def reset!
|
|
117
158
|
@lock.synchronize do
|
|
118
159
|
@client&.close
|
|
119
160
|
@client = nil
|
|
120
161
|
end
|
|
162
|
+
|
|
163
|
+
@api = nil
|
|
121
164
|
end
|
|
165
|
+
|
|
166
|
+
private
|
|
167
|
+
|
|
168
|
+
def fake_testing? = const_defined?(:Testing) && Testing.mode == :fake
|
|
122
169
|
end
|
|
123
170
|
end
|
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.5.
|
|
4
|
+
version: 0.5.3
|
|
5
5
|
platform: x86_64-linux-gnu
|
|
6
6
|
authors:
|
|
7
7
|
- Thadeu
|
|
@@ -33,6 +33,7 @@ files:
|
|
|
33
33
|
- exe/anway
|
|
34
34
|
- lib/active_job/queue_adapters/anway_adapter.rb
|
|
35
35
|
- lib/anway.rb
|
|
36
|
+
- lib/anway/api.rb
|
|
36
37
|
- lib/anway/cli.rb
|
|
37
38
|
- lib/anway/client.rb
|
|
38
39
|
- lib/anway/configuration.rb
|
|
@@ -41,7 +42,10 @@ files:
|
|
|
41
42
|
- lib/anway/daemon.rb
|
|
42
43
|
- lib/anway/errors.rb
|
|
43
44
|
- lib/anway/executor.rb
|
|
45
|
+
- lib/anway/inspect.rb
|
|
46
|
+
- lib/anway/inspect_command.rb
|
|
44
47
|
- lib/anway/job.rb
|
|
48
|
+
- lib/anway/job_info.rb
|
|
45
49
|
- lib/anway/options.rb
|
|
46
50
|
- lib/anway/pool.rb
|
|
47
51
|
- lib/anway/puma/plugin.rb
|