Anwaydocsv0.5.3

Configuration

Every key of config/anway.yml, the Ruby configuration block, and the environment variables of the gem.

The gem reads its settings from config/anway.yml, from the flags of the anway command, and from environment variables. In Ruby code, Anway.configure sets the client.

config/anway.yml
queues:
  - [critical, 4]
  - [mailers, 2]
  - [default, 1]
concurrency: <%= ENV.fetch('WORKER_THREADS', 10) %>
timeout: 25

development:
  server:
    web: 127.0.0.1:7420
    web_token: dev

production:
  url: <%= ENV["ANWAY_URL"] %>
  concurrency: 20

How the file works

  • The gem runs the file through ERB, then reads it as YAML.
  • The keys at the top apply to every environment.
  • The section of the current environment, such as production:, overrides them.
  • The environment comes from -e, then RAILS_ENV, then APP_ENV. The default is development.
  • A missing file is not an error, unless you name it with -C.
  • Flags have priority over the environment section.

Worker keys

KeyTypeDefaultMeaning
queuesListThe queues of the job classes, of their subscriptions, and of the Active Job classes, and defaultQueues in order. An item is a name, or [name, weight]. A list without weights uses strict order.
concurrencyInteger10The number of job threads. With autoscale, the start count and the default min.
timeoutNumber25Seconds for jobs to finish at a stop.
labelsList[]Worker labels. The gem always adds ruby.
hostnameStringThe machine nameThe worker hostname in the console.
tagStringThe app folderThe name of the app in the console.
autoscaleHashOffmax, and min if you want. Both are positive integers, and min is at most max.
requireStringconfig/environment.rbThe file that loads the jobs.
urlStringNoneThe url of a server that runs apart. With a url, the app does not run the server.
log_tagsBooleantrueTag each log line of a job with its class, jid and try. See Debugging jobs.
config/anway.yml
concurrency: 10
autoscale:
  max: 40
  min: 5
hostname: worker.1
tag: shop

Refer to Workers for the autoscale rules.

Server keys

Without a url, the app runs the Anway server itself: the server is embedded. The server keys set the embedded server.

config/anway.yml
production:
  server:
    bind: 0.0.0.0:7419
    password: <%= ENV["ANWAY_PASSWORD"] %>
server keyDefaultMeaning
modeembedded, or external with a urlembedded or external. Set it only to override the url rule. external needs a url.
bind127.0.0.1:7419The address of the embedded server. Use 0.0.0.0:7419 and a password for workers on other hosts.
datastorage/anwayThe data folder of the embedded server.
passwordNoneThe server password.
web127.0.0.1:7420 for an embedded server, else offThe address of the HTTP API, for the web console and anway jobs.
web_tokenA random tokenThe token of the HTTP API. Without it, the embedded server makes one and keeps it in data/web_token, readable only by your user, so every process of the app uses the same.

An embedded server opens its HTTP API on the loopback by default. Refer to Run the server and HTTP API.

Console

config/anway.yml
console:
  path: /path/to/anway-webui
console keyDefaultMeaning
pathNoneThe console binary. Without it, no console starts.
bind127.0.0.1:7421The console address, and where Anway::Web finds it.
apiThe API of the embedded serverThe server API that the console reads, for a server that runs apart.
tokenThe server web_tokenThe token for that API.

With console.path, bin/anway and the Puma plugin start the console next to the server. With an embedded server, they also open the server API on 127.0.0.1:7420, with a random token that only these two processes know. bin/anway console runs only the console.

A console for a server that runs apart needs server.web or console.api. Refer to Console.

Ruby configuration

Set the client in an initializer:

config/initializers/anway.rb
Anway.configure do |config|
  config.pool_size = 8
  config.timeout = 10
  config.tls = { ca_file: 'ca.pem' }

  config.use MyMiddleware.new
  config.on_error { |error, job| report(error, job) }
end
SettingDefaultMeaning
urlFrom config/anway.yml or ANWAY_URLThe server url. It overrides the file and the env var.
pool_size8The connections of the shared client.
timeout10Seconds for a server call.
tlsNoneOptions of the TLS context, for a tcp+tls:// url.
loggerLogger to $stdout, or Rails.logger in RailsThe logger of the gem.
use(middleware)NoneAdds a middleware around each job.
on_errorIn Rails, Rails.error.reportAdds a handler that runs when a job fails. The block gets the error and the job.

A middleware has call(job) and yields to run the next one. The first middleware that you add is the outermost:

class TimingMiddleware
  def call(job)
    started = Time.now

    yield

    Rails.logger.info("#{job.type} took #{Time.now - started}s")
  end
end

The server url

The url has the form tcp://:password@localhost:7419. Use tcp+tls:// for TLS. Refer to TLS.

The gem takes the url in this order:

  1. Anway.config.url in an initializer.
  2. url in config/anway.yml.
  3. ANWAY_URL.
  4. With no url, the embedded server on its local address.

Environment variables

VariableMeaning
ANWAY_URLThe url of a server that runs apart.
ANWAY_SERVERembedded or external. It has priority over server.mode.
ANWAY_SERVER_PATHAnother server binary for the embedded server. The gem for the ruby platform has no server, so it needs this variable or a url.
ANWAY_HOSTNAMEThe worker hostname. It has priority over hostname in the file.
ANWAY_TAGThe worker tag. It has priority over tag in the file.
ANWAY_CONSOLE_PATHThe console binary. It has priority over console.path.
ANWAY_IN_PUMAtrue or false: turns plugin :anway on or off. Without it, the plugin runs only in development.
ANWAY_WORKERThe worker process sets it to 1 before it loads the app. Read it in your app, for example to set the database pool.
RAILS_ENV, APP_ENVThe environment section of config/anway.yml.

Flags

The flags of the anway command have priority over the file. Refer to Workers for the full list.

bundle exec anway -C config/anway.yml -q critical,mailers,default -c 10

On this page