anway 0.2.0-aarch64-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
data/lib/anway/client.rb
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'uri'
|
|
5
|
+
|
|
6
|
+
module Anway
|
|
7
|
+
# Same operations as the anway-go client. It is safe for concurrent use.
|
|
8
|
+
class Client
|
|
9
|
+
DEFAULT_URL = 'tcp://127.0.0.1:7419'
|
|
10
|
+
DEFAULT_PORT = 7419
|
|
11
|
+
FETCH_WAIT = 2
|
|
12
|
+
|
|
13
|
+
Endpoint = Data.define(:host, :port, :password, :tls)
|
|
14
|
+
|
|
15
|
+
# The BEAT reply: the state (nil, "quiet", or "terminate"), and the thread
|
|
16
|
+
# count that the server wants, or nil.
|
|
17
|
+
Heartbeat = Data.define(:state, :concurrency)
|
|
18
|
+
|
|
19
|
+
# A blank URL uses ANWAY_URL, then the local default.
|
|
20
|
+
def initialize(url = nil, pool_size: 8, timeout: 10, tls: nil, wid: nil, labels: [], identity: {})
|
|
21
|
+
@endpoint = parse_url(url || ENV.fetch('ANWAY_URL', nil) || DEFAULT_URL)
|
|
22
|
+
@timeout = timeout
|
|
23
|
+
@wid = wid
|
|
24
|
+
|
|
25
|
+
@pool = Pool.new(pool_size) do
|
|
26
|
+
Connection.new(@endpoint, timeout: timeout, tls: tls, wid: wid, labels: labels, identity: identity)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def close
|
|
31
|
+
@pool.close
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Raises NotUnique when a unique lock blocks the job.
|
|
35
|
+
def push(job)
|
|
36
|
+
call('PUSH', job.to_h)
|
|
37
|
+
|
|
38
|
+
job.jid
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Returns the next job, or nil when no job arrives in about 2 seconds.
|
|
42
|
+
def fetch(*queues)
|
|
43
|
+
reply = call('FETCH', queues.join(' '), wait: FETCH_WAIT)
|
|
44
|
+
|
|
45
|
+
reply && Job.from_hash(JSON.parse(reply))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def ack(jid)
|
|
49
|
+
call('ACK', { jid: jid })
|
|
50
|
+
|
|
51
|
+
true
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def fail(jid, errtype:, message:, backtrace: [])
|
|
55
|
+
call('FAIL', { jid: jid, errtype: errtype, message: message, backtrace: backtrace })
|
|
56
|
+
|
|
57
|
+
true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Returns nil, "quiet", or "terminate".
|
|
61
|
+
def beat(current_state = nil)
|
|
62
|
+
heartbeat(current_state).state
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Sends BEAT with the thread count of the worker, so the server can ask for
|
|
66
|
+
# another count. Returns a Heartbeat.
|
|
67
|
+
def heartbeat(current_state = nil, concurrency: nil)
|
|
68
|
+
reply = call('BEAT', { wid: @wid, current_state: current_state, concurrency: concurrency }.compact)
|
|
69
|
+
|
|
70
|
+
return Heartbeat.new(state: nil, concurrency: nil) if reply == 'OK'
|
|
71
|
+
|
|
72
|
+
data = JSON.parse(reply)
|
|
73
|
+
|
|
74
|
+
Heartbeat.new(state: data['state'], concurrency: data['concurrency'])
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Lets the client hold size connections at once, for a worker that adds
|
|
78
|
+
# threads.
|
|
79
|
+
def grow_pool(size)
|
|
80
|
+
@pool.grow(size)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def info
|
|
84
|
+
JSON.parse(call('INFO'))
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def queue_sizes
|
|
88
|
+
info.dig('faktory', 'queues') || {}
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def pause_queues(*queues)
|
|
92
|
+
queue_command('PAUSE', queues)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def resume_queues(*queues)
|
|
96
|
+
queue_command('RESUME', queues)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def remove_queues(*queues)
|
|
100
|
+
queue_command('REMOVE', queues)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# The command is "kill", "discard", "requeue", or "clear".
|
|
104
|
+
# The target is "retries", "scheduled", or "dead".
|
|
105
|
+
def mutate(command, target, jobtype: nil, jids: nil, regexp: nil)
|
|
106
|
+
filter = { jobtype: jobtype, jids: jids, regexp: regexp }.compact
|
|
107
|
+
|
|
108
|
+
call('MUTATE', { cmd: command, target: target, filter: filter })
|
|
109
|
+
|
|
110
|
+
true
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def batch_new(success: nil, complete: nil, parent_bid: nil, description: nil)
|
|
114
|
+
definition = {
|
|
115
|
+
success: success&.to_h, complete: complete&.to_h, parent_bid: parent_bid, description: description
|
|
116
|
+
}.compact
|
|
117
|
+
|
|
118
|
+
call('BATCH', "NEW #{JSON.generate(definition)}")
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def batch_open(bid)
|
|
122
|
+
call('BATCH', "OPEN #{bid}")
|
|
123
|
+
|
|
124
|
+
true
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def batch_commit(bid)
|
|
128
|
+
call('BATCH', "COMMIT #{bid}")
|
|
129
|
+
|
|
130
|
+
true
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def batch_status(bid)
|
|
134
|
+
JSON.parse(call('BATCH', "STATUS #{bid}"))
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def track_get(jid)
|
|
138
|
+
JSON.parse(call('TRACK', "GET #{jid}"))
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def track_set(jid, percent: nil, desc: nil, reserve_until: nil)
|
|
142
|
+
update = { jid: jid, percent: percent, desc: desc, reserve_until: reserve_until&.to_time&.utc&.iso8601(9) }
|
|
143
|
+
|
|
144
|
+
call('TRACK', "SET #{JSON.generate(update.compact)}")
|
|
145
|
+
|
|
146
|
+
true
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
private
|
|
150
|
+
|
|
151
|
+
def queue_command(action, queues)
|
|
152
|
+
call('QUEUE', "#{action} #{queues.join(' ')}")
|
|
153
|
+
|
|
154
|
+
true
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def call(verb, argument = nil, wait: 0)
|
|
158
|
+
@pool.with(timeout: @timeout) { |connection| connection.command(verb, argument, wait: wait) }
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def parse_url(url)
|
|
162
|
+
uri = URI.parse(url)
|
|
163
|
+
|
|
164
|
+
raise ArgumentError, "unsupported scheme: #{uri.scheme}" unless %w[tcp tcp+tls].include?(uri.scheme)
|
|
165
|
+
|
|
166
|
+
password = uri.password && URI.decode_www_form_component(uri.password)
|
|
167
|
+
|
|
168
|
+
Endpoint.new(host: uri.hostname, port: uri.port || DEFAULT_PORT, password: password, tls: uri.scheme == 'tcp+tls')
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'logger'
|
|
4
|
+
|
|
5
|
+
module Anway
|
|
6
|
+
class Configuration
|
|
7
|
+
attr_accessor :url, :console_url, :pool_size, :timeout, :tls, :logger, :push_wrapper, :reloader
|
|
8
|
+
attr_reader :middleware, :error_handlers
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@url = nil
|
|
12
|
+
@console_url = nil
|
|
13
|
+
@pool_size = 8
|
|
14
|
+
@timeout = 10
|
|
15
|
+
@tls = nil
|
|
16
|
+
@logger = Logger.new($stdout, level: Logger::INFO)
|
|
17
|
+
@push_wrapper = ->(&block) { block.call }
|
|
18
|
+
@reloader = ->(&block) { block.call }
|
|
19
|
+
@middleware = []
|
|
20
|
+
@error_handlers = []
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The first middleware added is the outermost. An object has `call(job)`
|
|
24
|
+
# and yields to run the next one.
|
|
25
|
+
def use(middleware)
|
|
26
|
+
@middleware << middleware
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def on_error(&block)
|
|
30
|
+
@error_handlers << block
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'digest'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'openssl'
|
|
6
|
+
require 'socket'
|
|
7
|
+
|
|
8
|
+
module Anway
|
|
9
|
+
# One socket. Timeouts use nonblocking calls, so that plain and TLS sockets
|
|
10
|
+
# follow the same path.
|
|
11
|
+
class Connection
|
|
12
|
+
PROTOCOL_VERSION = 2
|
|
13
|
+
READ_SIZE = 16_384
|
|
14
|
+
|
|
15
|
+
# identity holds more HELLO fields of a worker, such as tag, queues, and
|
|
16
|
+
# concurrency. Its hostname replaces the machine name.
|
|
17
|
+
def initialize(endpoint, timeout:, tls: nil, wid: nil, labels: [], identity: {})
|
|
18
|
+
@timeout = timeout
|
|
19
|
+
@buffer = String.new(encoding: Encoding::BINARY)
|
|
20
|
+
@io = open_socket(endpoint, tls)
|
|
21
|
+
|
|
22
|
+
handshake(endpoint, wid, labels, identity)
|
|
23
|
+
rescue StandardError
|
|
24
|
+
close
|
|
25
|
+
|
|
26
|
+
raise
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The argument is a string, or an object that becomes JSON.
|
|
30
|
+
def command(verb, argument = nil, wait: 0)
|
|
31
|
+
deadline = monotonic + @timeout + wait
|
|
32
|
+
line = argument.nil? ? verb : "#{verb} #{argument.is_a?(String) ? argument : JSON.generate(argument)}"
|
|
33
|
+
|
|
34
|
+
write_all("#{line}\r\n", deadline)
|
|
35
|
+
read_reply(deadline)
|
|
36
|
+
rescue SystemCallError, IOError, SocketError, OpenSSL::SSL::SSLError => e
|
|
37
|
+
raise ConnectionError, e.message
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def close
|
|
41
|
+
@io&.close
|
|
42
|
+
rescue SystemCallError, IOError, OpenSSL::SSL::SSLError
|
|
43
|
+
nil
|
|
44
|
+
ensure
|
|
45
|
+
@io = nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def open_socket(endpoint, tls)
|
|
51
|
+
socket = Socket.tcp(endpoint.host, endpoint.port, connect_timeout: @timeout, resolv_timeout: @timeout)
|
|
52
|
+
|
|
53
|
+
return socket unless endpoint.tls
|
|
54
|
+
|
|
55
|
+
wrap_tls(socket, endpoint.host, tls)
|
|
56
|
+
rescue SystemCallError, SocketError, OpenSSL::SSL::SSLError => e
|
|
57
|
+
raise ConnectionError, e.message
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def wrap_tls(socket, host, tls)
|
|
61
|
+
context = OpenSSL::SSL::SSLContext.new
|
|
62
|
+
|
|
63
|
+
context.set_params(tls || {})
|
|
64
|
+
|
|
65
|
+
secure = OpenSSL::SSL::SSLSocket.new(socket, context)
|
|
66
|
+
|
|
67
|
+
secure.hostname = host
|
|
68
|
+
secure.sync_close = true
|
|
69
|
+
|
|
70
|
+
connect_tls(secure)
|
|
71
|
+
|
|
72
|
+
secure
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def connect_tls(secure)
|
|
76
|
+
deadline = monotonic + @timeout
|
|
77
|
+
|
|
78
|
+
loop do
|
|
79
|
+
case secure.connect_nonblock(exception: false)
|
|
80
|
+
when :wait_readable then wait(secure, deadline, :read)
|
|
81
|
+
when :wait_writable then wait(secure, deadline, :write)
|
|
82
|
+
else return
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def handshake(endpoint, wid, labels, identity)
|
|
88
|
+
deadline = monotonic + @timeout
|
|
89
|
+
greeting = JSON.parse(read_reply(deadline).delete_prefix('HI '))
|
|
90
|
+
|
|
91
|
+
raise Error, "unsupported protocol version #{greeting['v']}" unless greeting['v'] == PROTOCOL_VERSION
|
|
92
|
+
|
|
93
|
+
hello = { v: PROTOCOL_VERSION }
|
|
94
|
+
|
|
95
|
+
if greeting['s']
|
|
96
|
+
hello[:pwdhash] = password_hash(endpoint.password.to_s, greeting['s'], greeting['i'])
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
if wid
|
|
100
|
+
hello.merge!(wid: wid, hostname: Socket.gethostname, pid: Process.pid, labels: labels, **identity.compact)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
command('HELLO', hello)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def password_hash(password, salt, iterations)
|
|
107
|
+
digest = Digest::SHA256.digest(password + salt)
|
|
108
|
+
|
|
109
|
+
(iterations - 1).times { digest = Digest::SHA256.digest(digest) }
|
|
110
|
+
|
|
111
|
+
digest.unpack1('H*')
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def write_all(data, deadline)
|
|
115
|
+
data = data.b
|
|
116
|
+
|
|
117
|
+
until data.empty?
|
|
118
|
+
written = @io.write_nonblock(data, exception: false)
|
|
119
|
+
|
|
120
|
+
case written
|
|
121
|
+
when :wait_readable then wait(@io, deadline, :read)
|
|
122
|
+
when :wait_writable then wait(@io, deadline, :write)
|
|
123
|
+
else data = data.byteslice(written..)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def read_reply(deadline)
|
|
129
|
+
line = read_line(deadline)
|
|
130
|
+
|
|
131
|
+
raise ConnectionError, 'empty reply' if line.empty?
|
|
132
|
+
|
|
133
|
+
body = line[1..]
|
|
134
|
+
|
|
135
|
+
case line[0]
|
|
136
|
+
when '+' then body.force_encoding(Encoding::UTF_8)
|
|
137
|
+
when '-' then raise server_error(body)
|
|
138
|
+
when '$' then read_bulk(Integer(body), deadline)
|
|
139
|
+
else raise ConnectionError, "unexpected reply: #{line.inspect}"
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def server_error(body)
|
|
144
|
+
code, message = body.force_encoding(Encoding::UTF_8).split(' ', 2)
|
|
145
|
+
|
|
146
|
+
return NotUnique.new if code == 'NOTUNIQUE'
|
|
147
|
+
|
|
148
|
+
ServerError.new(code, message)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def read_line(deadline)
|
|
152
|
+
until (index = @buffer.index("\n"))
|
|
153
|
+
fill(deadline)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
@buffer.slice!(0..index).chomp
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def read_bulk(size, deadline)
|
|
160
|
+
return nil if size.negative?
|
|
161
|
+
|
|
162
|
+
fill(deadline) while @buffer.bytesize < size + 2
|
|
163
|
+
|
|
164
|
+
body = @buffer.slice!(0, size)
|
|
165
|
+
|
|
166
|
+
@buffer.slice!(0, 2)
|
|
167
|
+
|
|
168
|
+
body.force_encoding(Encoding::UTF_8)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def fill(deadline)
|
|
172
|
+
chunk = @io.read_nonblock(READ_SIZE, exception: false)
|
|
173
|
+
|
|
174
|
+
case chunk
|
|
175
|
+
when :wait_readable then wait(@io, deadline, :read)
|
|
176
|
+
when :wait_writable then wait(@io, deadline, :write)
|
|
177
|
+
when nil then raise ConnectionError, 'server closed the connection'
|
|
178
|
+
else @buffer << chunk
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def wait(io, deadline, mode)
|
|
183
|
+
remaining = deadline - monotonic
|
|
184
|
+
|
|
185
|
+
raise ConnectionError, 'timeout' unless remaining.positive?
|
|
186
|
+
|
|
187
|
+
readers = mode == :read ? [io] : nil
|
|
188
|
+
writers = mode == :write ? [io] : nil
|
|
189
|
+
|
|
190
|
+
IO.select(readers, writers, nil, remaining)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def monotonic
|
|
194
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anway
|
|
4
|
+
# Console runs the web console binary, which reads the HTTP API of the
|
|
5
|
+
# server. `mount Anway::Web` serves it inside the app. console.path in
|
|
6
|
+
# config/anway.yml, or ANWAY_CONSOLE_PATH, names the binary.
|
|
7
|
+
class Console < Daemon
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def name
|
|
11
|
+
'console'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def address
|
|
15
|
+
@settings.console_bind
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def env
|
|
19
|
+
@settings.console_env
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def executable
|
|
23
|
+
path = @settings.console_path
|
|
24
|
+
|
|
25
|
+
raise Error, 'the console has no binary. Set console.path in config/anway.yml, or ANWAY_CONSOLE_PATH.' unless path
|
|
26
|
+
raise Error, "the console binary is not executable: #{path}" unless File.executable?(path)
|
|
27
|
+
|
|
28
|
+
path
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def announce
|
|
32
|
+
@logger.info("anway: console on #{@settings.console_url}. Mount Anway::Web in config/routes.rb to open it in the app.")
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/anway/daemon.rb
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'socket'
|
|
4
|
+
|
|
5
|
+
module Anway
|
|
6
|
+
# Daemon runs a binary as a child process: the server or the console. A
|
|
7
|
+
# subclass names the binary, its address, and its environment.
|
|
8
|
+
class Daemon
|
|
9
|
+
START_TIMEOUT = 15
|
|
10
|
+
STOP_TIMEOUT = 40
|
|
11
|
+
LOCAL_HOSTS = ['', '0.0.0.0', '::', '[::]'].freeze
|
|
12
|
+
|
|
13
|
+
attr_reader :pid
|
|
14
|
+
|
|
15
|
+
def self.local_host(host)
|
|
16
|
+
LOCAL_HOSTS.include?(host) ? '127.0.0.1' : host
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initialize(settings, logger: Anway.config.logger)
|
|
20
|
+
@settings = settings
|
|
21
|
+
@logger = logger
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Starts the binary and waits for its port. When a process already
|
|
25
|
+
# listens on the address, it uses that one and returns false. With a
|
|
26
|
+
# random web token, that process has another token, so the start fails.
|
|
27
|
+
def start
|
|
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?
|
|
30
|
+
|
|
31
|
+
@logger.info("anway: a #{name} already listens on #{address}, so this process uses it")
|
|
32
|
+
|
|
33
|
+
return false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
prepare
|
|
37
|
+
|
|
38
|
+
@pid = Process.spawn(env, executable)
|
|
39
|
+
|
|
40
|
+
wait_for_port
|
|
41
|
+
announce
|
|
42
|
+
|
|
43
|
+
true
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Runs the binary in this process. It does not return.
|
|
47
|
+
def exec
|
|
48
|
+
prepare
|
|
49
|
+
announce
|
|
50
|
+
|
|
51
|
+
Process.exec(env, executable)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Asks the process to stop, and kills it after STOP_TIMEOUT.
|
|
55
|
+
def stop
|
|
56
|
+
return unless @pid
|
|
57
|
+
|
|
58
|
+
Process.kill('TERM', @pid)
|
|
59
|
+
|
|
60
|
+
return if wait(STOP_TIMEOUT)
|
|
61
|
+
|
|
62
|
+
Process.kill('KILL', @pid)
|
|
63
|
+
wait(5)
|
|
64
|
+
rescue Errno::ESRCH, Errno::ECHILD
|
|
65
|
+
nil
|
|
66
|
+
ensure
|
|
67
|
+
@pid = nil
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def prepare; end
|
|
73
|
+
|
|
74
|
+
def wait_for_port
|
|
75
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + START_TIMEOUT
|
|
76
|
+
|
|
77
|
+
until listening?
|
|
78
|
+
raise Error, "the anway #{name} stopped at start with #{$? || 'an error'}" if exited?
|
|
79
|
+
raise Error, "the anway #{name} did not open #{address} in #{START_TIMEOUT} seconds" if Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
|
|
80
|
+
|
|
81
|
+
sleep 0.05
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def listening?
|
|
86
|
+
host, _, port = address.rpartition(':')
|
|
87
|
+
|
|
88
|
+
Socket.tcp(self.class.local_host(host), port.to_i, connect_timeout: 0.2).close
|
|
89
|
+
|
|
90
|
+
true
|
|
91
|
+
rescue SystemCallError, SocketError
|
|
92
|
+
false
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def exited?
|
|
96
|
+
Process.wait(@pid, Process::WNOHANG) == @pid
|
|
97
|
+
rescue Errno::ECHILD
|
|
98
|
+
true
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def wait(seconds)
|
|
102
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds
|
|
103
|
+
|
|
104
|
+
while Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
|
|
105
|
+
return true if Process.wait(@pid, Process::WNOHANG)
|
|
106
|
+
|
|
107
|
+
sleep 0.05
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
false
|
|
111
|
+
rescue Errno::ECHILD
|
|
112
|
+
true
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
data/lib/anway/errors.rb
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anway
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
|
|
6
|
+
class NotUnique < Error
|
|
7
|
+
def initialize(message = 'job not unique')
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class Closed < Error
|
|
13
|
+
def initialize(message = 'client is closed')
|
|
14
|
+
super
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class ConnectionError < Error; end
|
|
19
|
+
|
|
20
|
+
class UnknownJob < Error; end
|
|
21
|
+
|
|
22
|
+
class ServerError < Error
|
|
23
|
+
attr_reader :code
|
|
24
|
+
|
|
25
|
+
def initialize(code, message)
|
|
26
|
+
@code = code
|
|
27
|
+
|
|
28
|
+
super("#{code} #{message}")
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Inherits from Exception, so that `rescue => e` in a job does not catch it.
|
|
33
|
+
class Shutdown < Exception; end # rubocop:disable Lint/InheritException
|
|
34
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anway
|
|
4
|
+
# Runs one job through the reloader and the middleware.
|
|
5
|
+
module Executor
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def call(job)
|
|
9
|
+
Anway.config.reloader.call do
|
|
10
|
+
klass = Worker.find(job.type)
|
|
11
|
+
|
|
12
|
+
chain(job) do
|
|
13
|
+
instance = klass.new
|
|
14
|
+
|
|
15
|
+
instance.job = job
|
|
16
|
+
instance.perform(*job.args)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def chain(job, &block)
|
|
22
|
+
runner = Anway.config.middleware.reverse.reduce(block) do |nextone, middleware|
|
|
23
|
+
-> { middleware.call(job) { nextone.call } }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
runner.call
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/anway/job.rb
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require 'time'
|
|
6
|
+
|
|
7
|
+
module Anway
|
|
8
|
+
class Job
|
|
9
|
+
ATTRIBUTES = %w[queue reserve_for at retry backtrace created_at enqueued_at failure].freeze
|
|
10
|
+
|
|
11
|
+
attr_accessor :jid, :type, :args, :custom, *ATTRIBUTES.map(&:to_sym)
|
|
12
|
+
|
|
13
|
+
def self.from_hash(hash)
|
|
14
|
+
job = allocate
|
|
15
|
+
|
|
16
|
+
job.jid = hash['jid']
|
|
17
|
+
job.type = hash['jobtype']
|
|
18
|
+
job.args = hash['args'] || []
|
|
19
|
+
job.custom = hash['custom']
|
|
20
|
+
|
|
21
|
+
ATTRIBUTES.each { |name| job.public_send("#{name}=", hash[name]) }
|
|
22
|
+
|
|
23
|
+
job
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Options: queue, retry, reserve_for, backtrace, at, custom, jid.
|
|
27
|
+
def initialize(type, args = [], **options)
|
|
28
|
+
validate_json!(args)
|
|
29
|
+
|
|
30
|
+
@jid = options[:jid] || SecureRandom.hex(12)
|
|
31
|
+
@type = type
|
|
32
|
+
@args = args
|
|
33
|
+
@queue = options[:queue]&.to_s
|
|
34
|
+
@retry = options[:retry]
|
|
35
|
+
@reserve_for = options[:reserve_for]
|
|
36
|
+
@backtrace = options[:backtrace]
|
|
37
|
+
@custom = options[:custom]&.transform_keys(&:to_s)
|
|
38
|
+
|
|
39
|
+
run_at(options[:at]) if options[:at]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def run_at(time)
|
|
43
|
+
@at = time.to_time.utc.iso8601(9)
|
|
44
|
+
|
|
45
|
+
self
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def set_custom(key, value)
|
|
49
|
+
@custom = (@custom || {}).merge(key.to_s => value)
|
|
50
|
+
|
|
51
|
+
self
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def to_h
|
|
55
|
+
{
|
|
56
|
+
'jid' => jid, 'jobtype' => type, 'args' => args, 'custom' => custom,
|
|
57
|
+
**ATTRIBUTES.to_h { |name| [name, public_send(name)] }
|
|
58
|
+
}.compact
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
# Other languages read these args, so only JSON types are valid.
|
|
64
|
+
def validate_json!(value)
|
|
65
|
+
case value
|
|
66
|
+
when nil, true, false, String, Integer, Float
|
|
67
|
+
nil
|
|
68
|
+
when Array
|
|
69
|
+
value.each { |item| validate_json!(item) }
|
|
70
|
+
when Hash
|
|
71
|
+
value.each do |key, item|
|
|
72
|
+
raise ArgumentError, "job hash keys must be strings: #{key.inspect}" unless key.is_a?(String)
|
|
73
|
+
|
|
74
|
+
validate_json!(item)
|
|
75
|
+
end
|
|
76
|
+
else
|
|
77
|
+
raise ArgumentError, "job args must be JSON types: #{value.inspect}"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|