anway 0.2.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 +7 -0
- data/LICENSE +22 -0
- data/README.md +270 -0
- data/exe/anway +6 -0
- data/lib/active_job/queue_adapters/anway_adapter.rb +67 -0
- data/lib/anway/cli.rb +218 -0
- data/lib/anway/client.rb +171 -0
- data/lib/anway/configuration.rb +33 -0
- data/lib/anway/connection.rb +197 -0
- data/lib/anway/console.rb +35 -0
- data/lib/anway/daemon.rb +115 -0
- data/lib/anway/errors.rb +34 -0
- data/lib/anway/executor.rb +29 -0
- data/lib/anway/job.rb +81 -0
- data/lib/anway/pool.rb +97 -0
- data/lib/anway/puma/plugin.rb +34 -0
- data/lib/anway/queues.rb +21 -0
- data/lib/anway/railtie.rb +32 -0
- data/lib/anway/runner.rb +324 -0
- data/lib/anway/server.rb +48 -0
- data/lib/anway/settings.rb +229 -0
- data/lib/anway/testing.rb +48 -0
- data/lib/anway/version.rb +8 -0
- data/lib/anway/web.rb +104 -0
- data/lib/anway/worker.rb +127 -0
- data/lib/anway.rb +65 -0
- data/lib/generators/anway/install/install_generator.rb +74 -0
- data/lib/generators/anway/install/templates/anway +12 -0
- data/lib/generators/anway/install/templates/anway.yml +32 -0
- data/lib/puma/plugin/anway.rb +69 -0
- data/libexec/anway-server +0 -0
- metadata +82 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'erb'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require 'yaml'
|
|
6
|
+
|
|
7
|
+
module Anway
|
|
8
|
+
# Settings reads config/anway.yml. The keys at the top apply to every
|
|
9
|
+
# environment, and the section of the current environment overrides them.
|
|
10
|
+
#
|
|
11
|
+
# queues: [critical, default]
|
|
12
|
+
# concurrency: 10
|
|
13
|
+
#
|
|
14
|
+
# production:
|
|
15
|
+
# url: <%= ENV["ANWAY_URL"] %>
|
|
16
|
+
#
|
|
17
|
+
# The server is embedded, so the app runs it, unless a url is set in the
|
|
18
|
+
# file or in ANWAY_URL. `server: { mode: }` or ANWAY_SERVER can still choose.
|
|
19
|
+
class Settings
|
|
20
|
+
DEFAULT_PATH = 'config/anway.yml'
|
|
21
|
+
DEFAULT_BIND = '127.0.0.1:7419'
|
|
22
|
+
DEFAULT_DATA = 'storage/anway'
|
|
23
|
+
DEFAULT_WEB = '127.0.0.1:7420'
|
|
24
|
+
DEFAULT_CONSOLE = '127.0.0.1:7421'
|
|
25
|
+
MODES = %w[embedded external].freeze
|
|
26
|
+
KEYS = %w[queues concurrency timeout labels hostname tag autoscale require url server console].freeze
|
|
27
|
+
SERVER_KEYS = %w[mode bind data password web web_token].freeze
|
|
28
|
+
CONSOLE_KEYS = %w[path bind api token].freeze
|
|
29
|
+
|
|
30
|
+
def self.environment(env = ENV)
|
|
31
|
+
env['RAILS_ENV'] || env['APP_ENV'] || env['RACK_ENV'] || 'development'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# A missing file is fine, unless the caller named it.
|
|
35
|
+
def self.load(path: nil, environment: nil, env: ENV)
|
|
36
|
+
file = path || DEFAULT_PATH
|
|
37
|
+
|
|
38
|
+
raise ArgumentError, "config file not found: #{file}" if path && !File.exist?(file)
|
|
39
|
+
|
|
40
|
+
content = File.exist?(file) ? YAML.safe_load(ERB.new(File.read(file)).result, aliases: true) : {}
|
|
41
|
+
|
|
42
|
+
new(content || {}, environment: environment || self.environment(env), env: env)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def initialize(content, environment:, env: ENV)
|
|
46
|
+
section = content[environment].is_a?(Hash) ? content[environment] : {}
|
|
47
|
+
|
|
48
|
+
@values = content.slice(*KEYS).merge(section.slice(*KEYS))
|
|
49
|
+
@server = section_of(content, 'server', SERVER_KEYS).merge(section_of(section, 'server', SERVER_KEYS))
|
|
50
|
+
@console = section_of(content, 'console', CONSOLE_KEYS).merge(section_of(section, 'console', CONSOLE_KEYS))
|
|
51
|
+
@env = env
|
|
52
|
+
|
|
53
|
+
check!
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Options for the worker, as the CLI names them.
|
|
57
|
+
# ANWAY_HOSTNAME and ANWAY_TAG come before the file.
|
|
58
|
+
def worker_options
|
|
59
|
+
options = @values.slice('queues', 'concurrency', 'timeout', 'labels', 'hostname', 'tag', 'require').transform_keys(&:to_sym)
|
|
60
|
+
|
|
61
|
+
options[:hostname] = present(@env['ANWAY_HOSTNAME']) || present(options[:hostname])
|
|
62
|
+
options[:tag] = present(@env['ANWAY_TAG']) || present(options[:tag])
|
|
63
|
+
options[:autoscale] = autoscale
|
|
64
|
+
|
|
65
|
+
options.compact
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# autoscale: { max: n } or { min: n, max: n }, or nil when it is off.
|
|
69
|
+
def autoscale
|
|
70
|
+
value = @values['autoscale']
|
|
71
|
+
|
|
72
|
+
return nil unless value.is_a?(Hash)
|
|
73
|
+
|
|
74
|
+
value.slice('min', 'max').transform_keys(&:to_sym)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The url of a server that runs apart: from the file, then from ANWAY_URL.
|
|
78
|
+
def url
|
|
79
|
+
present(@values['url']) || present(@env['ANWAY_URL'])
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def mode
|
|
83
|
+
present(@env['ANWAY_SERVER']) || present(@server['mode']) || (url ? 'external' : 'embedded')
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def embedded?
|
|
87
|
+
mode == 'embedded'
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def bind
|
|
91
|
+
present(@server['bind']) || DEFAULT_BIND
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def data
|
|
95
|
+
present(@server['data']) || DEFAULT_DATA
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def password
|
|
99
|
+
present(@server['password'])
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The url that clients in this app use: the external server, or the
|
|
103
|
+
# embedded one on its local address.
|
|
104
|
+
def client_url
|
|
105
|
+
return url unless embedded?
|
|
106
|
+
|
|
107
|
+
host, port = split_bind
|
|
108
|
+
auth = password ? ":#{password}@" : ''
|
|
109
|
+
|
|
110
|
+
"tcp://#{auth}#{Daemon.local_host(host)}:#{port}"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# The address of the HTTP API that the web console reads. It is off
|
|
114
|
+
# unless it is set, or the console runs next to an embedded server.
|
|
115
|
+
def web
|
|
116
|
+
present(@server['web']) || (console? && embedded? ? DEFAULT_WEB : nil)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Without a token in the file, the server and the console that this
|
|
120
|
+
# process starts share a random one.
|
|
121
|
+
def web_token
|
|
122
|
+
present(@server['web_token']) || (web ? (@random_token ||= SecureRandom.hex(16)) : nil)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# A random token changes at each start, so a server or console from an
|
|
126
|
+
# earlier start does not accept it.
|
|
127
|
+
def random_web_token?
|
|
128
|
+
!web.nil? && present(@server['web_token']).nil?
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# The web console binary: ANWAY_CONSOLE_PATH, then console.path.
|
|
132
|
+
def console_path
|
|
133
|
+
path = present(@env['ANWAY_CONSOLE_PATH']) || present(@console['path'])
|
|
134
|
+
|
|
135
|
+
path && File.expand_path(path)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def console?
|
|
139
|
+
!console_path.nil?
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def console_bind
|
|
143
|
+
present(@console['bind']) || DEFAULT_CONSOLE
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# The address where `mount Anway::Web` finds the console.
|
|
147
|
+
def console_url
|
|
148
|
+
host, port = split(console_bind)
|
|
149
|
+
|
|
150
|
+
"http://#{Daemon.local_host(host)}:#{port}"
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# The HTTP API that the console reads: console.api, or the API of the
|
|
154
|
+
# embedded server.
|
|
155
|
+
def console_api
|
|
156
|
+
return present(@console['api']) if present(@console['api'])
|
|
157
|
+
return nil unless web
|
|
158
|
+
|
|
159
|
+
host, port = split(web)
|
|
160
|
+
|
|
161
|
+
"http://#{Daemon.local_host(host)}:#{port}"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def console_env
|
|
165
|
+
host, port = split(console_bind)
|
|
166
|
+
|
|
167
|
+
{ 'HOST' => host, 'PORT' => port, 'ANWAY_API_URL' => console_api, 'WEB_TOKEN' => present(@console['token']) || web_token.to_s }
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# The environment of the server binary.
|
|
171
|
+
def server_env
|
|
172
|
+
env = { 'BIND_ADDR' => bind, 'DATA_DIR' => File.expand_path(data) }
|
|
173
|
+
|
|
174
|
+
env['SERVER_PASSWORD'] = password if password
|
|
175
|
+
env['WEB_ADDR'] = web if web
|
|
176
|
+
env['WEB_TOKEN'] = web_token if web_token
|
|
177
|
+
|
|
178
|
+
env
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
private
|
|
182
|
+
|
|
183
|
+
def section_of(hash, name, keys)
|
|
184
|
+
value = hash[name]
|
|
185
|
+
|
|
186
|
+
return {} if value.nil?
|
|
187
|
+
|
|
188
|
+
raise ArgumentError, "#{name} must be a hash of #{keys.join(', ')}" unless value.is_a?(Hash)
|
|
189
|
+
|
|
190
|
+
value.slice(*keys)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def check!
|
|
194
|
+
check_autoscale!
|
|
195
|
+
raise ArgumentError, "server mode must be embedded or external, not #{mode}" unless MODES.include?(mode)
|
|
196
|
+
raise ArgumentError, 'an external server needs url or ANWAY_URL' if mode == 'external' && url.nil?
|
|
197
|
+
raise ArgumentError, "server bind must be host:port, not #{bind}" unless split_bind.last.to_s.match?(/\A\d+\z/)
|
|
198
|
+
raise ArgumentError, "console bind must be host:port, not #{console_bind}" unless split(console_bind).last.to_s.match?(/\A\d+\z/)
|
|
199
|
+
raise ArgumentError, 'the console needs a server API: set server.web, or console.api' if console? && console_api.nil?
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def check_autoscale!
|
|
203
|
+
value = @values['autoscale']
|
|
204
|
+
|
|
205
|
+
return if value.nil?
|
|
206
|
+
|
|
207
|
+
message = 'autoscale must be a hash with max, and min if you want, as positive numbers'
|
|
208
|
+
|
|
209
|
+
raise ArgumentError, message unless value.is_a?(Hash) && value['max'].is_a?(Integer) && value['max'].positive?
|
|
210
|
+
raise ArgumentError, message unless value['min'].nil? || (value['min'].is_a?(Integer) && value['min'].between?(1, value['max']))
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def split_bind
|
|
214
|
+
split(bind)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def split(address)
|
|
218
|
+
host, _, port = address.rpartition(':')
|
|
219
|
+
|
|
220
|
+
[host, port]
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def present(value)
|
|
224
|
+
text = value.to_s.strip
|
|
225
|
+
|
|
226
|
+
text.empty? ? nil : text
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'anway'
|
|
4
|
+
|
|
5
|
+
module Anway
|
|
6
|
+
# Test modes for apps. No server is necessary.
|
|
7
|
+
#
|
|
8
|
+
# Anway::Testing.fake! keeps pushed jobs in a list
|
|
9
|
+
# Anway::Testing.inline! runs each job at push
|
|
10
|
+
# Anway::Testing.disable! uses the real client
|
|
11
|
+
module Testing
|
|
12
|
+
@mode = nil
|
|
13
|
+
@jobs = []
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
attr_reader :jobs, :mode
|
|
17
|
+
|
|
18
|
+
def fake!
|
|
19
|
+
@mode = :fake
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def inline!
|
|
23
|
+
@mode = :inline
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def disable!
|
|
27
|
+
@mode = nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def clear
|
|
31
|
+
@jobs.clear
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def enqueue(job)
|
|
35
|
+
copy = Job.from_hash(JSON.parse(JSON.generate(job.to_h)))
|
|
36
|
+
|
|
37
|
+
@mode == :inline ? Executor.call(copy) : @jobs << copy
|
|
38
|
+
|
|
39
|
+
job.jid
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Runs and removes all fake jobs. Jobs that they push also run.
|
|
43
|
+
def drain
|
|
44
|
+
Executor.call(@jobs.shift) until @jobs.empty?
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
data/lib/anway/web.rb
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'uri'
|
|
5
|
+
|
|
6
|
+
module Anway
|
|
7
|
+
# Web serves the Anway console inside a Rack app, such as Rails:
|
|
8
|
+
#
|
|
9
|
+
# authenticate :user, ->(user) { user.admin? } do
|
|
10
|
+
# mount Anway::Web => '/anway'
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# It proxies to the console process, which bin/anway starts when
|
|
14
|
+
# config/anway.yml sets console.path. The console gets the mount path in
|
|
15
|
+
# X-Forwarded-Prefix, and the browser host, so its links, redirects, and
|
|
16
|
+
# CSRF check work under the path. Cookies and Authorization stay in the app.
|
|
17
|
+
class Web
|
|
18
|
+
METHODS = %w[GET HEAD POST].freeze
|
|
19
|
+
DROPPED = %w[connection keep-alive proxy-authenticate proxy-authorization te trailer transfer-encoding upgrade content-length].freeze
|
|
20
|
+
PRIVATE = %w[HTTP_COOKIE HTTP_AUTHORIZATION HTTP_HOST HTTP_VERSION HTTP_CONNECTION].freeze
|
|
21
|
+
|
|
22
|
+
def self.call(env)
|
|
23
|
+
new.call(env)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def initialize(target = nil)
|
|
27
|
+
@target = target
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def call(env)
|
|
31
|
+
return [405, { 'content-type' => 'text/plain', 'allow' => METHODS.join(', ') }, ['Method not allowed']] unless METHODS.include?(env['REQUEST_METHOD'])
|
|
32
|
+
|
|
33
|
+
response = forward(env)
|
|
34
|
+
headers = {}
|
|
35
|
+
|
|
36
|
+
response.each_header { |name, value| headers[name] = value unless DROPPED.include?(name) }
|
|
37
|
+
|
|
38
|
+
[response.code.to_i, headers, [response.body.to_s]]
|
|
39
|
+
rescue SystemCallError, IOError, Net::OpenTimeout, Net::ReadTimeout, SocketError
|
|
40
|
+
[502, { 'content-type' => 'text/html; charset=utf-8' }, [unavailable]]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def target
|
|
46
|
+
URI(@target || Anway.config.console_url || Settings.load.console_url)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def forward(env)
|
|
50
|
+
base = target
|
|
51
|
+
path = env['PATH_INFO'].to_s
|
|
52
|
+
path = '/' if path.empty?
|
|
53
|
+
query = env['QUERY_STRING'].to_s
|
|
54
|
+
request = Net::HTTPGenericRequest.new(env['REQUEST_METHOD'], env['REQUEST_METHOD'] == 'POST', env['REQUEST_METHOD'] != 'HEAD', query.empty? ? path : "#{path}?#{query}")
|
|
55
|
+
|
|
56
|
+
copy_headers(env, request)
|
|
57
|
+
|
|
58
|
+
if env['REQUEST_METHOD'] == 'POST'
|
|
59
|
+
request.body = body_of(env)
|
|
60
|
+
request['content-type'] = env['CONTENT_TYPE'] if env['CONTENT_TYPE']
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
Net::HTTP.start(base.host, base.port, open_timeout: 2, read_timeout: 30) { |http| http.request(request) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Rails reads a form post before the mount, in Rack::MethodOverride, so
|
|
67
|
+
# the input is at its end. It rewinds the input, and uses the raw form that
|
|
68
|
+
# Rack keeps when the input cannot rewind.
|
|
69
|
+
def body_of(env)
|
|
70
|
+
input = env['rack.input']
|
|
71
|
+
|
|
72
|
+
input.rewind if input.respond_to?(:rewind)
|
|
73
|
+
|
|
74
|
+
body = input&.read.to_s
|
|
75
|
+
|
|
76
|
+
body.empty? ? env['rack.request.form_vars'].to_s : body
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def copy_headers(env, request)
|
|
80
|
+
env.each do |key, value|
|
|
81
|
+
next unless key.start_with?('HTTP_') && !PRIVATE.include?(key)
|
|
82
|
+
|
|
83
|
+
request[key.delete_prefix('HTTP_').split('_').map(&:capitalize).join('-')] = value
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
request['host'] = env['HTTP_HOST'] || "#{env['SERVER_NAME']}:#{env['SERVER_PORT']}"
|
|
87
|
+
request['x-forwarded-prefix'] = env['SCRIPT_NAME'].to_s
|
|
88
|
+
request['x-forwarded-proto'] = env['rack.url_scheme'].to_s
|
|
89
|
+
request['accept-encoding'] = 'identity'
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def unavailable
|
|
93
|
+
<<~HTML
|
|
94
|
+
<!doctype html>
|
|
95
|
+
<title>Anway console</title>
|
|
96
|
+
<div style="font-family:system-ui,sans-serif;max-width:560px;margin:15vh auto;padding:0 16px;line-height:1.5">
|
|
97
|
+
<h1 style="font-size:20px">The Anway console is not running</h1>
|
|
98
|
+
<p>Set <code>console.path</code> in <code>config/anway.yml</code> to the console binary. Then <code>bin/anway</code>, or Puma with <code>plugin :anway</code>, starts it next to the server.</p>
|
|
99
|
+
<p>To run it apart, use <code>bin/anway console</code>, and set <code>console.bind</code> to its address.</p>
|
|
100
|
+
</div>
|
|
101
|
+
HTML
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
data/lib/anway/worker.rb
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anway
|
|
4
|
+
# Makes a class a job:
|
|
5
|
+
#
|
|
6
|
+
# class WelcomeJob
|
|
7
|
+
# include Anway::Worker
|
|
8
|
+
#
|
|
9
|
+
# job_options queue: 'mailers', retry: 5
|
|
10
|
+
#
|
|
11
|
+
# def perform(user_id); end
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# WelcomeJob.perform_async(42)
|
|
15
|
+
module Worker
|
|
16
|
+
DEFAULT_BACKTRACE = 30
|
|
17
|
+
|
|
18
|
+
@registry = {}
|
|
19
|
+
@lock = Mutex.new
|
|
20
|
+
|
|
21
|
+
class << self
|
|
22
|
+
def included(base)
|
|
23
|
+
base.extend(ClassMethods)
|
|
24
|
+
|
|
25
|
+
register(base) if base.name
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def register(klass, old_type: nil)
|
|
29
|
+
@lock.synchronize do
|
|
30
|
+
@registry.delete(old_type) if old_type
|
|
31
|
+
@registry[klass.job_type] = klass
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def classes
|
|
36
|
+
@lock.synchronize { @registry.values }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Only registered classes run. The job type comes from the network, so it
|
|
40
|
+
# never becomes a constant lookup. The lookup by the class name below
|
|
41
|
+
# uses a registered name, and returns the newest class after a reload.
|
|
42
|
+
def find(type)
|
|
43
|
+
klass = @lock.synchronize { @registry[type] }
|
|
44
|
+
|
|
45
|
+
raise UnknownJob, "no handler for job type #{type.inspect}" unless klass
|
|
46
|
+
|
|
47
|
+
klass.name ? Object.const_get(klass.name) : klass
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
attr_accessor :job
|
|
52
|
+
|
|
53
|
+
def jid
|
|
54
|
+
job&.jid
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Updates the progress of a tracked job.
|
|
58
|
+
def track(percent: nil, desc: nil, reserve_until: nil)
|
|
59
|
+
Anway.client.track_set(jid, percent: percent, desc: desc, reserve_until: reserve_until)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
module ClassMethods
|
|
63
|
+
def inherited(subclass)
|
|
64
|
+
super
|
|
65
|
+
|
|
66
|
+
Worker.register(subclass) if subclass.name
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def job_type(name = nil)
|
|
70
|
+
return @job_type || self.name unless name
|
|
71
|
+
|
|
72
|
+
old_type = job_type
|
|
73
|
+
@job_type = name
|
|
74
|
+
|
|
75
|
+
Worker.register(self, old_type: old_type)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def job_options(options = nil)
|
|
79
|
+
inherited_options = superclass.respond_to?(:job_options) ? superclass.job_options : {}
|
|
80
|
+
|
|
81
|
+
@job_options = (@job_options || {}).merge(options) if options
|
|
82
|
+
|
|
83
|
+
inherited_options.merge(@job_options || {})
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def perform_async(*args)
|
|
87
|
+
enqueue_with(args, {})
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def perform_at(time, *args)
|
|
91
|
+
enqueue_with(args, { at: time })
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def perform_in(seconds, *args)
|
|
95
|
+
enqueue_with(args, { at: Time.now + seconds.to_f })
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Changes the options for one push: WelcomeJob.set(queue: 'low').perform_async(1)
|
|
99
|
+
def set(**options)
|
|
100
|
+
Setter.new(self, options)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def enqueue_with(args, options)
|
|
104
|
+
Anway.enqueue(Job.new(job_type, args, backtrace: DEFAULT_BACKTRACE, **job_options, **options))
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
class Setter
|
|
109
|
+
def initialize(klass, options)
|
|
110
|
+
@klass = klass
|
|
111
|
+
@options = options
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def perform_async(*args)
|
|
115
|
+
@klass.enqueue_with(args, @options)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def perform_at(time, *args)
|
|
119
|
+
@klass.enqueue_with(args, @options.merge(at: time))
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def perform_in(seconds, *args)
|
|
123
|
+
@klass.enqueue_with(args, @options.merge(at: Time.now + seconds.to_f))
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
data/lib/anway.rb
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'anway/version'
|
|
4
|
+
require_relative 'anway/errors'
|
|
5
|
+
require_relative 'anway/configuration'
|
|
6
|
+
require_relative 'anway/job'
|
|
7
|
+
require_relative 'anway/connection'
|
|
8
|
+
require_relative 'anway/pool'
|
|
9
|
+
require_relative 'anway/client'
|
|
10
|
+
require_relative 'anway/worker'
|
|
11
|
+
require_relative 'anway/executor'
|
|
12
|
+
require_relative 'anway/queues'
|
|
13
|
+
require_relative 'anway/runner'
|
|
14
|
+
require_relative 'anway/settings'
|
|
15
|
+
require_relative 'anway/daemon'
|
|
16
|
+
require_relative 'anway/server'
|
|
17
|
+
require_relative 'anway/console'
|
|
18
|
+
require_relative 'anway/web'
|
|
19
|
+
require_relative 'anway/puma/plugin'
|
|
20
|
+
require_relative 'anway/railtie' if defined?(Rails::Railtie)
|
|
21
|
+
|
|
22
|
+
module Anway
|
|
23
|
+
@lock = Mutex.new
|
|
24
|
+
|
|
25
|
+
class << self
|
|
26
|
+
def config
|
|
27
|
+
@config ||= Configuration.new
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def configure
|
|
31
|
+
yield config
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def client
|
|
35
|
+
@lock.synchronize do
|
|
36
|
+
@client ||= Client.new(
|
|
37
|
+
config.url, pool_size: config.pool_size, timeout: config.timeout, tls: config.tls
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Sends a job with no local class, for a worker in any language.
|
|
43
|
+
def push(type, args = [], **options)
|
|
44
|
+
enqueue(Job.new(type, args, **options))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Inside a database transaction, the push waits for the commit.
|
|
48
|
+
def enqueue(job)
|
|
49
|
+
testing = const_defined?(:Testing) ? Testing : nil
|
|
50
|
+
|
|
51
|
+
return testing.enqueue(job) if testing&.mode
|
|
52
|
+
|
|
53
|
+
config.push_wrapper.call { client.push(job) }
|
|
54
|
+
|
|
55
|
+
job.jid
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def reset!
|
|
59
|
+
@lock.synchronize do
|
|
60
|
+
@client&.close
|
|
61
|
+
@client = nil
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rails/generators'
|
|
4
|
+
|
|
5
|
+
module Anway
|
|
6
|
+
module Generators
|
|
7
|
+
# bin/rails generate anway:install [--active-job]
|
|
8
|
+
class InstallGenerator < Rails::Generators::Base
|
|
9
|
+
PUMA_LINE = 'plugin :anway'
|
|
10
|
+
|
|
11
|
+
source_root File.expand_path('templates', __dir__)
|
|
12
|
+
|
|
13
|
+
class_option :active_job, type: :boolean, default: false, desc: 'Use Anway as the Active Job adapter'
|
|
14
|
+
|
|
15
|
+
def create_settings
|
|
16
|
+
copy_file 'anway.yml', 'config/anway.yml'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def create_binstub
|
|
20
|
+
copy_file 'anway', 'bin/anway'
|
|
21
|
+
chmod 'bin/anway', 0o755
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def add_puma_plugin
|
|
25
|
+
path = File.join(destination_root, 'config/puma.rb')
|
|
26
|
+
|
|
27
|
+
return unless File.exist?(path)
|
|
28
|
+
return if File.read(path).include?(PUMA_LINE)
|
|
29
|
+
|
|
30
|
+
append_to_file 'config/puma.rb', <<~RUBY
|
|
31
|
+
|
|
32
|
+
# Runs Anway next to Puma: the embedded server and the workers. ANWAY_IN_PUMA
|
|
33
|
+
# turns it on or off. Without it, it runs only in development.
|
|
34
|
+
#{PUMA_LINE}
|
|
35
|
+
RUBY
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The mount stays a comment: it needs a console binary, and the app must
|
|
39
|
+
# choose who can open it.
|
|
40
|
+
def add_console_route
|
|
41
|
+
path = File.join(destination_root, 'config/routes.rb')
|
|
42
|
+
|
|
43
|
+
return unless File.exist?(path)
|
|
44
|
+
return if File.read(path).include?('Anway::Web')
|
|
45
|
+
|
|
46
|
+
route <<~RUBY.chomp
|
|
47
|
+
# The Anway console. It needs console.path in config/anway.yml. Limit it to admins:
|
|
48
|
+
# authenticate :user, ->(user) { user.admin? } do
|
|
49
|
+
# mount Anway::Web => "/anway"
|
|
50
|
+
# end
|
|
51
|
+
RUBY
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def set_active_job_adapter
|
|
55
|
+
return unless options[:active_job]
|
|
56
|
+
|
|
57
|
+
application 'config.active_job.queue_adapter = :anway'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def show_next_steps
|
|
61
|
+
say <<~TEXT
|
|
62
|
+
|
|
63
|
+
Anway is installed.
|
|
64
|
+
|
|
65
|
+
bin/dev Puma starts the Anway server and workers in development
|
|
66
|
+
bin/anway the server and workers, without Puma
|
|
67
|
+
bin/anway worker only workers, for a server that runs apart
|
|
68
|
+
|
|
69
|
+
Jobs: include Anway::Worker in a class#{options[:active_job] ? ', or use Active Job' : ', or run the generator with --active-job'}.
|
|
70
|
+
TEXT
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# bin/anway the embedded server, when config/anway.yml has no url, and workers
|
|
5
|
+
# bin/anway server only the server
|
|
6
|
+
# bin/anway worker only workers
|
|
7
|
+
Dir.chdir(File.expand_path('..', __dir__))
|
|
8
|
+
|
|
9
|
+
require_relative '../config/boot'
|
|
10
|
+
require 'anway/cli'
|
|
11
|
+
|
|
12
|
+
exit Anway::CLI.new(ARGV).run
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Anway settings. Keys at the top apply to every environment, and an
|
|
2
|
+
# environment section overrides them.
|
|
3
|
+
#
|
|
4
|
+
# Without url, this app runs the Anway server itself (embedded), with its
|
|
5
|
+
# data in storage/anway. Set url, or ANWAY_URL, to use a server that runs
|
|
6
|
+
# apart. On more than one host, run one server and set ANWAY_URL everywhere.
|
|
7
|
+
|
|
8
|
+
queues: [default]
|
|
9
|
+
concurrency: 10
|
|
10
|
+
|
|
11
|
+
# The console shows each worker as hostname:pid with a tag. The tag is the
|
|
12
|
+
# app folder by default. ANWAY_HOSTNAME and ANWAY_TAG override these.
|
|
13
|
+
# hostname: worker.1
|
|
14
|
+
# tag: my_app
|
|
15
|
+
|
|
16
|
+
# Autoscale adds threads while jobs wait, up to max, and removes them when
|
|
17
|
+
# the load drops. Keep max at or below the database pool of the worker.
|
|
18
|
+
# autoscale:
|
|
19
|
+
# max: 20
|
|
20
|
+
|
|
21
|
+
# The web console. bin/anway starts it next to the server, and
|
|
22
|
+
# `mount Anway::Web` in config/routes.rb serves it in the app.
|
|
23
|
+
# console:
|
|
24
|
+
# path: /path/to/anway-webui
|
|
25
|
+
|
|
26
|
+
production:
|
|
27
|
+
# url: <%= ENV["ANWAY_URL"] %>
|
|
28
|
+
#
|
|
29
|
+
# To accept workers from other hosts on the embedded server:
|
|
30
|
+
# server:
|
|
31
|
+
# bind: 0.0.0.0:7419
|
|
32
|
+
# password: <%= ENV["ANWAY_PASSWORD"] %>
|