fastly_nsq 1.16.0 → 1.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.env +2 -2
  3. data/.git-blame-ignore-revs +6 -0
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +4 -3
  6. data/ChangeLog.md +31 -1
  7. data/Gemfile +8 -8
  8. data/README.md +84 -6
  9. data/Rakefile +10 -11
  10. data/fastly_nsq.gemspec +26 -26
  11. data/lib/fastly_nsq/cli.rb +43 -50
  12. data/lib/fastly_nsq/consumer.rb +27 -14
  13. data/lib/fastly_nsq/feeder.rb +5 -7
  14. data/lib/fastly_nsq/http/nsqd.rb +28 -28
  15. data/lib/fastly_nsq/http/nsqlookupd.rb +11 -11
  16. data/lib/fastly_nsq/http.rb +4 -4
  17. data/lib/fastly_nsq/launcher.rb +16 -16
  18. data/lib/fastly_nsq/listener.rb +16 -16
  19. data/lib/fastly_nsq/manager.rb +13 -12
  20. data/lib/fastly_nsq/message.rb +4 -4
  21. data/lib/fastly_nsq/messenger.rb +25 -15
  22. data/lib/fastly_nsq/new_relic.rb +8 -8
  23. data/lib/fastly_nsq/priority_queue.rb +2 -2
  24. data/lib/fastly_nsq/priority_thread_pool.rb +3 -3
  25. data/lib/fastly_nsq/producer.rb +23 -14
  26. data/lib/fastly_nsq/safe_thread.rb +1 -1
  27. data/lib/fastly_nsq/testing.rb +4 -3
  28. data/lib/fastly_nsq/tls_options.rb +6 -6
  29. data/lib/fastly_nsq/version.rb +1 -1
  30. data/lib/fastly_nsq.rb +64 -29
  31. data/spec/cli_spec.rb +2 -2
  32. data/spec/consumer_spec.rb +53 -12
  33. data/spec/fastly_nsq_spec.rb +108 -32
  34. data/spec/feeder_spec.rb +4 -4
  35. data/spec/http/nsqd_spec.rb +23 -23
  36. data/spec/http/nsqlookupd_spec.rb +19 -19
  37. data/spec/http_spec.rb +22 -22
  38. data/spec/integration_spec.rb +10 -10
  39. data/spec/launcher_spec.rb +21 -21
  40. data/spec/listener_spec.rb +50 -50
  41. data/spec/manager_spec.rb +27 -27
  42. data/spec/matchers/delegate.rb +4 -4
  43. data/spec/message_spec.rb +19 -19
  44. data/spec/messenger_spec.rb +71 -59
  45. data/spec/new_relic.rb +27 -27
  46. data/spec/priority_thread_pool_spec.rb +2 -2
  47. data/spec/producer_spec.rb +70 -31
  48. data/spec/spec_helper.rb +12 -12
  49. data/spec/support/http.rb +2 -2
  50. data/spec/support/webmock.rb +1 -1
  51. data/spec/testing_spec.rb +12 -12
  52. data/spec/tls_options_spec.rb +47 -47
  53. metadata +10 -11
  54. data/.rubocop.yml +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7ddfaca7ffa4121e56b173447b114ccd61ea104bc3804f2833920438492f4da
4
- data.tar.gz: 570739450b84d445d190cb29b6d2ce7f22287bb4bde73f2af75e533eb00b2fc3
3
+ metadata.gz: 7c499298a1a690208afc03693e6bdae6730408f7e61544033f5b6fdbab778364
4
+ data.tar.gz: 3c346c1bcb3a7d44508144beb14b91edad7f8bcb2cb07e35cd8a301a67069e57
5
5
  SHA512:
6
- metadata.gz: 9b9b61eb6bfb2cc6453f259887e4b045368fc87ab4d88f05ef1591c83c1a488d36bf555034990495221a92ef697933a9ed0d007f650f0f11ee8bf4357f0b826d
7
- data.tar.gz: 9e0202b599608733735097989d7f853b0e98def7299f366321509550ce9ea850ac23d757f81505bc2c7df1a73d3f742200b078733a9ba8036f6e96b175d7a4a5
6
+ metadata.gz: b350e9fd2f0d2c993aa3d3a84b24935137faa7cbf65967f0342434c108e7b13213b3dd45b753c2e73de3e487729f2038a8a07045ef1987c1d3e5d55d78797ee3
7
+ data.tar.gz: a2f4494d86c8d5d37db0ed59e149edebfecee56888971cc2e1cfecff5d88fbcbd05e65a72cfe5d1d1e6d8758f88296a20b877d43a8ebed06d663519070e6f7fd
data/.env CHANGED
@@ -1,4 +1,4 @@
1
- NSQD_TCP_ADDRESS=127.0.0.1:4150
2
1
  NSQD_HTTP_ADDRESS=127.0.0.1:4151
3
- NSQLOOKUPD_TCP_ADDRESS=127.0.0.1:4160
4
2
  NSQLOOKUPD_HTTP_ADDRESS=127.0.0.1:4161
3
+ NSQD_CONSUMERS=127.0.0.1:4150
4
+ NSQD_PRODUCERS=127.0.0.1:4150
@@ -0,0 +1,6 @@
1
+ 25bc1d8cd908a86e8f7536192541a0d3b66f07db
2
+ 7c4cf244c4586d3bd280fee7909b6bcabda34998
3
+ 2e09ec7d8205b3ed3c2093777ae5382723919787
4
+ 5381db0ed1493235a7095a51cdbd67b88caa47d2
5
+ d77aee840f7affb6ccdeb3748e0c50ccea96f7ef
6
+ ca9a1aa122ead418879ecb6a104fb61054fe675e
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.5.1
1
+ 3.0.3
data/.travis.yml CHANGED
@@ -2,14 +2,15 @@ language: ruby
2
2
  cache: bundler
3
3
  sudo: false
4
4
  rvm:
5
- - 2.3.5
6
- - 2.4.2
7
- - 2.5.1
5
+ - 2.6.9
6
+ - 2.7.5
7
+ - 3.0.3
8
8
  script:
9
9
  - bundle exec rake
10
10
  services:
11
11
  - docker
12
12
  before_install:
13
+ - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
13
14
  - docker-compose up -d
14
15
  before_script:
15
16
  - gem update --system
data/ChangeLog.md CHANGED
@@ -1,7 +1,37 @@
1
1
  # Change Log
2
2
 
3
- ## [v1.15.0](https://github.com/fastly/fastly_nsq/tree/v1.15.0)
3
+ ## [v1.18.0](https://github.com/fastly/fastly_nsq/tree/v1.18.0) (2022-03-04)
4
+ [Full Changelog](https://github.com/fastly/fastly_nsq/compare/v1.17.1...v1.18.0)
4
5
 
6
+ **Merged pull requests:**
7
+
8
+ - Configurable NSQDs for Consumers and Producers [\#105](https://github.com/fastly/fastly_nsq/pull/105)
9
+
10
+ ## [v1.17.1](https://github.com/fastly/fastly_nsq/tree/v1.17.1) (2022-02-23)
11
+ [Full Changelog](https://github.com/fastly/fastly_nsq/compare/v1.17.0...v1.17.1)
12
+
13
+ **Merged pull requests:**
14
+
15
+ - Use standardrb for style and formatting [\#104](https://github.com/fastly/fastly_nsq/pull/104)
16
+
17
+ ## [v1.17.0](https://github.com/fastly/fastly_nsq/tree/v1.17.0) (2021-02-11)
18
+ [Full Changelog](https://github.com/fastly/fastly_nsq/compare/v1.16.0...v1.17.0)
19
+
20
+ **Merged pull requests:**
21
+
22
+ - Support for arbitary sent\_at Time [\#99](https://github.com/fastly/fastly_nsq/pull/99) ([leklund](https://github.com/leklund))
23
+ - remove version pin on rake for development [\#97](https://github.com/fastly/fastly_nsq/pull/97) ([leklund](https://github.com/leklund))
24
+ - Ignore from Humane Registry [\#96](https://github.com/fastly/fastly_nsq/pull/96) ([leklund](https://github.com/leklund))
25
+
26
+ ## [v1.16.0](https://github.com/fastly/fastly_nsq/tree/v1.16.0) (2019-08-16)
27
+ [Full Changelog](https://github.com/fastly/fastly_nsq/compare/v1.15.0...v1.16.0)
28
+
29
+ **Merged pull requests:**
30
+
31
+ - Log the internal NSQ id [\#95](https://github.com/fastly/fastly_nsq/pull/95) ([leklund](https://github.com/leklund))
32
+ - Consumer still attempts connections in Test mode [\#93](https://github.com/fastly/fastly_nsq/pull/93) ([alieander](https://github.com/alieander))
33
+
34
+ ## [v1.15.0](https://github.com/fastly/fastly_nsq/tree/v1.15.0) (2018-10-05)
5
35
  [Full Changelog](https://github.com/fastly/fastly_nsq/compare/v1.14.0...v1.15.0)
6
36
 
7
37
  **Merged pull requests:**
data/Gemfile CHANGED
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source 'https://rubygems.org'
3
+ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
6
 
7
- gem 'bundler-audit'
8
- gem 'newrelic_rpm', require: false
9
- gem 'overcommit', '~> 0.32.0'
10
- gem 'rake', '~> 11.1.2'
11
- gem 'rdoc', '~> 4.2.2'
12
- gem 'rubocop', '~> 0.51.0'
13
- gem 'rubygems-tasks', '~> 0.2'
7
+ gem "bundler-audit"
8
+ gem "newrelic_rpm", require: false
9
+ gem "overcommit", "~> 0.32.0"
10
+ gem "rake"
11
+ gem "rdoc"
12
+ gem "standard"
13
+ gem "rubygems-tasks", "~> 0.2"
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # fastly_nsq [![Build Status](https://travis-ci.org/fastly/fastly_nsq.svg?branch=master)](https://travis-ci.org/fastly/fastly_nsq)
1
+ # fastly_nsq [![Build Status](https://travis-ci.com/fastly/fastly_nsq.svg?branch=main)](https://travis-ci.com/fastly/fastly_nsq)
2
2
 
3
3
  NSQ adapter and testing objects
4
4
  for using the NSQ messaging system
@@ -12,9 +12,9 @@ We also include fakes
12
12
  to make testing easier.
13
13
 
14
14
  This library is dependent
15
- on the [`nsq-ruby`] gem.
15
+ on the [`nsq-ruby-fastly`] gem.
16
16
 
17
- [`nsq-ruby`]: https://github.com/wistia/nsq-ruby
17
+ [`nsq-ruby-fastly`]: https://github.com/fastly/nsq-ruby
18
18
 
19
19
  Please use [GitHub Issues] to report bugs.
20
20
 
@@ -34,6 +34,65 @@ and `bundle install`.
34
34
 
35
35
  ## Usage
36
36
 
37
+ ### Connections
38
+
39
+ NSQD cconnections can be discovered via nsqlookupd's or
40
+ specified explicity for consumers and producers.
41
+
42
+ #### Using nsqlookup:
43
+
44
+ Set the ENV variable to a comma sepearated string of lookups:
45
+
46
+ ```
47
+ ENV['NSQLOOKUPD_HTTP_ADDRESS'] = "lookup01:1234,lookup01:1234"
48
+ ```
49
+
50
+ Or configure them directly:
51
+
52
+ ```
53
+ FastlyNsq.configure do |config|
54
+ config.lookupd_http_addresses = ["lookup01:1234", "lookup02:1234"]
55
+ end
56
+ ```
57
+
58
+ #### Using nsqd directly:
59
+
60
+ NSQD connections can be specified for consumers and producers. Being able to set
61
+ different sets for consumers and producers facilitates removing and adding new instances
62
+ without downtime.
63
+
64
+ Set the following ENV variables to a comma sepearted string of nsqds:
65
+
66
+ ```
67
+ ENV['NSQD_CONSUMERS']="nsqd01:4150,nsd02:4150"
68
+ ENV['NSQD_PRODUCERS']="nsqd01:4150,nsd02:4150"
69
+ ```
70
+
71
+ Or configure them directly:
72
+
73
+ ```
74
+ FastlyNsq.configure do |config|
75
+ config.consumer_nsqds = ["nsqd01:4150", "nsqd02:4150"]
76
+ config.producer_nsqds = ["nsqd01:4150", "nsqd02:4150"]
77
+ end
78
+ ```
79
+
80
+ ### Connection Priority
81
+
82
+ When `FastlyNsq.consumer_nsqds` or `FastlyNsq.producer_nsqds` are set they
83
+ will be used instead of `FastlyNsq.lookupd_http_addresses`.
84
+
85
+ ### TLS
86
+
87
+ Set the following ENV variables to enable TLS support:
88
+
89
+ ```
90
+ NSQ_SSL_KEY
91
+ NSQ_SSL_CERTIFICATE
92
+ NSQ_SSL_CA_CERTIFICATE
93
+ NSQ_SSL_VERIFY_MODE (optional)
94
+ ```
95
+
37
96
  ### `FastlyNsq::Producer`
38
97
 
39
98
  This is a class
@@ -50,7 +109,6 @@ message_data = {
50
109
  }
51
110
 
52
111
  producer = FastlyNsq::Producer.new(
53
- nsqd: ENV.fetch('NSQD_TCP_ADDRESS'),
54
112
  topic: topic,
55
113
  )
56
114
 
@@ -281,6 +339,22 @@ expect(some_result)
281
339
 
282
340
  ## Configuration
283
341
 
342
+ See the [documentation](https://www.rubydoc.info/gems/fastly_nsq/FastlyNsq) for additional settings
343
+
344
+ Example:
345
+
346
+ ```
347
+ FastlyNsq.configure do |config|
348
+ config.channel = "z"
349
+ config.producer_nsqds = ["nsqd01:4150", "nsqd02:4150"]
350
+ config.lookupd_http_addresses = ["lookupd01:4161", "lookupd02:4161"]
351
+ config.logger = Logger.new(STDOUT)
352
+ config.max_attempts = 10
353
+ config.max_req_timeout = 10_000
354
+ config.max_processing_pool_threads = 42
355
+ end
356
+ ```
357
+
284
358
  ### Environment Variables
285
359
 
286
360
  The URLs for the various
@@ -293,10 +367,10 @@ stock NSQ on OS X,
293
367
  installed via Homebrew:
294
368
 
295
369
  ```shell
296
- NSQD_TCP_ADDRESS='127.0.0.1:4150'
297
370
  NSQD_HTTP_ADDRESS='127.0.0.1:4151'
298
- NSQLOOKUPD_TCP_ADDRESS='127.0.0.1:4160'
299
371
  NSQLOOKUPD_HTTP_ADDRESS='127.0.0.1:4161, 10.1.1.101:4161'
372
+ NSQD_CONSUMERS='127.0.0.1:4150'
373
+ NSQD_PRODUCERS='127.0.0.1:4150'
300
374
  ```
301
375
 
302
376
  See the [`.sample.env`](examples/.sample.env) file
@@ -334,3 +408,7 @@ You will still need the `ENV` variables as defined above.
334
408
  Copyright (c) 2016 [Fastly, Inc](https://fastly.com) under an MIT license.
335
409
 
336
410
  See [LICENSE.txt](LICENSE.txt) for details.
411
+
412
+ # Metadata
413
+
414
+ - Ignore
data/Rakefile CHANGED
@@ -1,28 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rubygems'
3
+ require "rubygems"
4
4
 
5
5
  begin
6
- require 'bundler/setup'
6
+ require "bundler/setup"
7
7
  rescue LoadError => e
8
8
  abort e.message
9
9
  end
10
10
 
11
- require 'rake'
11
+ require "rake"
12
12
 
13
- require 'rubygems/tasks'
13
+ require "rubygems/tasks"
14
14
  Gem::Tasks.new
15
15
 
16
- require 'rdoc/task'
16
+ require "rdoc/task"
17
17
  RDoc::Task.new
18
18
  task doc: :rdoc
19
19
 
20
- require 'rspec/core/rake_task'
20
+ require "rspec/core/rake_task"
21
21
  RSpec::Core::RakeTask.new(:spec)
22
22
 
23
- require 'bundler/audit/cli'
23
+ require "bundler/audit/cli"
24
24
  namespace :bundler do
25
- desc 'Updates the ruby-advisory-db and runs audit'
25
+ desc "Updates the ruby-advisory-db and runs audit"
26
26
  task :audit do
27
27
  %w[update check].each do |command|
28
28
  Bundler::Audit::CLI.start [command]
@@ -30,8 +30,7 @@ namespace :bundler do
30
30
  end
31
31
  end
32
32
 
33
- require 'rubocop/rake_task'
34
- RuboCop::RakeTask.new
33
+ require "standard/rake"
35
34
 
36
35
  task(:default).clear
37
- task default: ['spec', 'rubocop', 'bundler:audit']
36
+ task default: ["spec", "standard", "bundler:audit"]
data/fastly_nsq.gemspec CHANGED
@@ -1,36 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path('../lib', __FILE__)
3
+ lib = File.expand_path("../lib", __FILE__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'fastly_nsq/version'
5
+ require "fastly_nsq/version"
6
6
 
7
7
  Gem::Specification.new do |gem|
8
- gem.name = 'fastly_nsq'
9
- gem.version = FastlyNsq::VERSION
10
- gem.summary = 'Fastly NSQ Adapter'
11
- gem.description = "Helper classes for Fastly's NSQ Services"
12
- gem.license = 'MIT'
13
- gem.authors = ["Tommy O'Neil", 'Adarsh Pandit', 'Joshua Wehner', 'Lukas Eklund', 'Josh Lane', 'Hassan Shahid']
14
- gem.email = 'tommy@fastly.com'
15
- gem.homepage = 'https://github.com/fastly/fastly_nsq'
8
+ gem.name = "fastly_nsq"
9
+ gem.version = FastlyNsq::VERSION
10
+ gem.summary = "Fastly NSQ Adapter"
11
+ gem.description = "Helper classes for Fastly's NSQ Services"
12
+ gem.license = "MIT"
13
+ gem.authors = ["Tommy O'Neil", "Adarsh Pandit", "Joshua Wehner", "Lukas Eklund", "Josh Lane", "Hassan Shahid"]
14
+ gem.email = "tommy@fastly.com"
15
+ gem.homepage = "https://github.com/fastly/fastly_nsq"
16
16
 
17
- gem.files = `git ls-files`.split("\n")
17
+ gem.files = `git ls-files`.split("\n")
18
18
 
19
- gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
20
- gem.test_files = gem.files.grep(%r{^(test|features)/})
21
- gem.require_paths = ['lib']
19
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|features)/})
21
+ gem.require_paths = ["lib"]
22
22
 
23
- gem.add_development_dependency 'awesome_print', '~> 1.6'
24
- gem.add_development_dependency 'bundler'
25
- gem.add_development_dependency 'dotenv'
26
- gem.add_development_dependency 'pry-byebug', '~> 3.3'
27
- gem.add_development_dependency 'rspec', '~> 3.4'
28
- gem.add_development_dependency 'rspec-eventually', '0.2'
29
- gem.add_development_dependency 'timecop'
30
- gem.add_development_dependency 'webmock', '~> 3.0'
31
- gem.add_development_dependency 'yard'
23
+ gem.add_development_dependency "awesome_print", "~> 1.6"
24
+ gem.add_development_dependency "bundler"
25
+ gem.add_development_dependency "dotenv"
26
+ gem.add_development_dependency "pry-byebug", "~> 3.3"
27
+ gem.add_development_dependency "rspec", "~> 3.4"
28
+ gem.add_development_dependency "rspec-eventually", "0.2"
29
+ gem.add_development_dependency "timecop"
30
+ gem.add_development_dependency "webmock", "~> 3.0"
31
+ gem.add_development_dependency "yard"
32
32
 
33
- gem.add_dependency 'concurrent-ruby', '~> 1.0'
34
- gem.add_dependency 'nsq-ruby', '~> 2.3'
35
- gem.add_dependency 'priority_queue_cxx', '~> 0.3'
33
+ gem.add_dependency "concurrent-ruby", "~> 1.0"
34
+ gem.add_dependency "nsq-ruby-fastly", "~> 2.4"
35
+ gem.add_dependency "priority_queue_cxx", "~> 0.3"
36
36
  end
@@ -1,14 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/ClassLength
4
3
  $stdout.sync = true
5
4
 
6
- require 'fastly_nsq'
7
- require 'fastly_nsq/launcher'
8
- require 'fastly_nsq/manager'
9
- require 'fileutils'
10
- require 'optparse'
11
- require 'singleton'
5
+ require "fastly_nsq"
6
+ require "fastly_nsq/launcher"
7
+ require "fastly_nsq/manager"
8
+ require "fileutils"
9
+ require "optparse"
10
+ require "singleton"
12
11
 
13
12
  # Provides functionality to support running FastlyNsq as a command line
14
13
  # application that listens and processes NSQ messages.
@@ -44,26 +43,24 @@ class FastlyNsq::CLI
44
43
 
45
44
  read_loop
46
45
  rescue Interrupt
47
- FastlyNsq.logger.info 'Shutting down'
46
+ FastlyNsq.logger.info "Shutting down"
48
47
  launcher.stop
49
48
  # Explicitly exit so busy Processor threads can't block
50
49
  # process shutdown.
51
- FastlyNsq.logger.info 'Bye!'
50
+ FastlyNsq.logger.info "Bye!"
52
51
  exit(0)
53
52
  end
54
53
 
55
54
  private
56
55
 
57
56
  def launcher
58
- @launcher ||= FastlyNsq::Launcher.new(options)
57
+ @launcher ||= FastlyNsq::Launcher.new(**options)
59
58
  end
60
59
 
61
60
  def read_loop
62
61
  trapped_read_io = trap_signals
63
- loop do
64
- readable_io = IO.select([trapped_read_io])
65
- break unless readable_io
66
- signal = readable_io.first[0].gets.strip
62
+ while trapped_read_io.wait_readable
63
+ signal = trapped_read_io.gets.strip
67
64
  handle_signal signal
68
65
  end
69
66
  end
@@ -72,51 +69,51 @@ class FastlyNsq::CLI
72
69
  boot_rails
73
70
  require options[:require] if options[:require]
74
71
  FastlyNsq.logger.info "Running in #{RUBY_DESCRIPTION}"
75
- FastlyNsq.logger.info 'Starting processing, hit Ctrl-C to stop' unless options[:daemon]
72
+ FastlyNsq.logger.info "Starting processing, hit Ctrl-C to stop" unless options[:daemon]
76
73
  end
77
74
 
78
75
  def boot_rails
79
- return unless ENV['RAILS_ENV']
76
+ return unless ENV["RAILS_ENV"]
80
77
 
81
- require 'rails'
82
- require File.expand_path('./config/application.rb')
83
- require File.expand_path('./config/environment.rb')
78
+ require "rails"
79
+ require File.expand_path("./config/application.rb")
80
+ require File.expand_path("./config/environment.rb")
84
81
  end
85
82
 
86
83
  def parse(args)
87
84
  opts = {}
88
85
 
89
86
  @parser = OptionParser.new do |o|
90
- o.on '-c', '--concurrency COUNT', 'Number of threads used to process messages' do |arg|
87
+ o.on "-c", "--concurrency COUNT", "Number of threads used to process messages" do |arg|
91
88
  opts[:max_threads] = arg
92
89
  end
93
90
 
94
- o.on '-d', '--daemon', 'Daemonize process' do |arg|
91
+ o.on "-d", "--daemon", "Daemonize process" do |arg|
95
92
  opts[:daemonize] = arg
96
93
  end
97
94
 
98
- o.on '-L', '--logfile PATH', 'path to writable logfile' do |arg|
95
+ o.on "-L", "--logfile PATH", "path to writable logfile" do |arg|
99
96
  opts[:logfile] = arg
100
97
  end
101
98
 
102
- o.on '-P', '--pidfile PATH', 'path to pidfile' do |arg|
99
+ o.on "-P", "--pidfile PATH", "path to pidfile" do |arg|
103
100
  opts[:pidfile] = arg
104
101
  end
105
102
 
106
- o.on '-r', '--require [PATH|DIR]', 'Location of message_processor definition' do |arg|
103
+ o.on "-r", "--require [PATH|DIR]", "Location of message_processor definition" do |arg|
107
104
  opts[:require] = arg
108
105
  end
109
106
 
110
- o.on '-v', '--verbose', 'enable verbose logging output' do |arg|
107
+ o.on "-v", "--verbose", "enable verbose logging output" do |arg|
111
108
  opts[:verbose] = arg
112
109
  end
113
110
 
114
- o.on '-t', '--timeout SECONDS', 'shutdown deadline timeout' do |arg|
111
+ o.on "-t", "--timeout SECONDS", "shutdown deadline timeout" do |arg|
115
112
  opts[:timeout] = arg
116
113
  end
117
114
  end
118
115
 
119
- @parser.banner = 'fastly_nsq [options]'
116
+ @parser.banner = "fastly_nsq [options]"
120
117
  @parser.parse!(args)
121
118
 
122
119
  @options = opts
@@ -171,13 +168,11 @@ class FastlyNsq::CLI
171
168
  sigs = %w[INT TERM TTIN USR1]
172
169
 
173
170
  sigs.each do |sig|
174
- begin
175
- trap sig do
176
- self_write.puts(sig)
177
- end
178
- rescue ArgumentError
179
- puts "Signal #{sig} not supported"
171
+ trap sig do
172
+ self_write.puts(sig)
180
173
  end
174
+ rescue ArgumentError
175
+ puts "Signal #{sig} not supported"
181
176
  end
182
177
  self_read
183
178
  end
@@ -185,28 +180,28 @@ class FastlyNsq::CLI
185
180
  def handle_signal(sig)
186
181
  FastlyNsq.logger.debug "Got #{sig} signal"
187
182
  case sig
188
- when 'INT'
183
+ when "INT"
189
184
  # Handle Ctrl-C in JRuby like MRI
190
185
  # http://jira.codehaus.org/browse/JRUBY-4637
191
186
  raise Interrupt
192
- when 'TERM'
187
+ when "TERM"
193
188
  # Heroku sends TERM and then waits 10 seconds for process to exit.
194
189
  raise Interrupt
195
- when 'USR1'
196
- FastlyNsq.logger.info 'Received USR1, no longer accepting new work'
190
+ when "USR1"
191
+ FastlyNsq.logger.info "Received USR1, no longer accepting new work"
197
192
  launcher.stop_listeners
198
- when 'TTIN'
193
+ when "TTIN"
199
194
  handle_ttin
200
195
  end
201
196
  end
202
197
 
203
198
  def handle_ttin
204
199
  Thread.list.each do |thread|
205
- FastlyNsq.logger.warn "Thread TID-#{thread.object_id.to_s(36)} #{thread['fastly_nsq_label']}"
200
+ FastlyNsq.logger.warn "Thread TID-#{thread.object_id.to_s(36)} #{thread["fastly_nsq_label"]}"
206
201
  if thread.backtrace
207
202
  FastlyNsq.logger.warn thread.backtrace.join("\n")
208
203
  else
209
- FastlyNsq.logger.warn '<no backtrace available>'
204
+ FastlyNsq.logger.warn "<no backtrace available>"
210
205
  end
211
206
  end
212
207
  end
@@ -224,26 +219,24 @@ class FastlyNsq::CLI
224
219
  reopen(files_to_reopen)
225
220
 
226
221
  [$stdout, $stderr].each do |io|
227
- File.open(options.fetch(:logfile, '/dev/null'), 'ab') do |f|
222
+ File.open(options.fetch(:logfile, "/dev/null"), "ab") do |f|
228
223
  io.reopen(f)
229
224
  end
230
225
  io.sync = true
231
226
  end
232
- $stdin.reopen('/dev/null')
227
+ $stdin.reopen("/dev/null")
233
228
 
234
229
  setup_logger
235
230
  end
236
231
 
237
232
  def reopen(files)
238
233
  files.each do |file|
239
- begin
240
- file.reopen file.path, 'a+'
241
- file.sync = true
242
- rescue IOError => e
243
- FastlyNsq.logger.warn "IOError reopening file: #{e.message}"
244
- rescue StandardError => e
245
- FastlyNsq.logger.error "Non IOError reopening file: #{e.message}"
246
- end
234
+ file.reopen file.path, "a+"
235
+ file.sync = true
236
+ rescue IOError => e
237
+ FastlyNsq.logger.warn "IOError reopening file: #{e.message}"
238
+ rescue => e
239
+ FastlyNsq.logger.error "Non IOError reopening file: #{e.message}"
247
240
  end
248
241
  end
249
242
 
@@ -61,6 +61,9 @@ class FastlyNsq::Consumer
61
61
  ##
62
62
  # Create a FastlyNsq::Consumer
63
63
  #
64
+ # Will connect to NSQDs in this priority: 1. direct from FastlyNsq.consumer_nsqds 2. discovered via FastlyNsq.lookupd_http_addresses.
65
+ # If both `consumer_nsqds` and `lookupd_http_addresses` are set only the FastlyNsq.consumer_nsqds will be used.
66
+ #
64
67
  # @param topic [String] NSQ topic from which to consume
65
68
  # @param channel [String] NSQ channel from which to consume
66
69
  # @param queue [#pop, #size] Queue object, most likely an instance of {FastlyNsq::Feeder}
@@ -78,11 +81,11 @@ class FastlyNsq::Consumer
78
81
  # channel: 'channel'
79
82
  # )
80
83
  def initialize(topic:, channel:, queue: nil, tls_options: nil, connect_timeout: DEFAULT_CONNECTION_TIMEOUT, max_attempts: FastlyNsq.max_attempts, **options)
81
- @topic = topic
82
- @channel = channel
83
- @tls_options = FastlyNsq::TlsOptions.as_hash(tls_options)
84
+ @topic = topic
85
+ @channel = channel
86
+ @tls_options = FastlyNsq::TlsOptions.as_hash(tls_options)
84
87
  @connect_timeout = connect_timeout
85
- @max_attempts = max_attempts
88
+ @max_attempts = max_attempts
86
89
 
87
90
  @connection = connect(queue, **options)
88
91
  end
@@ -99,15 +102,25 @@ class FastlyNsq::Consumer
99
102
  attr_reader :tls_options
100
103
 
101
104
  def connect(queue, **options)
102
- Nsq::Consumer.new(
103
- {
104
- nsqlookupd: FastlyNsq.lookupd_http_addresses,
105
- topic: topic,
106
- channel: channel,
107
- queue: queue,
108
- max_attempts: max_attempts,
109
- **options,
110
- }.merge(tls_options),
111
- )
105
+ consumers = FastlyNsq.consumer_nsqds
106
+ lookupd = FastlyNsq.lookupd_http_addresses
107
+
108
+ opts = {
109
+ topic: topic,
110
+ channel: channel,
111
+ queue: queue,
112
+ max_attempts: max_attempts,
113
+ **options
114
+ }.merge(tls_options)
115
+
116
+ if !consumers.empty?
117
+ opts[:nsqd] = consumers
118
+ elsif !lookupd.empty?
119
+ opts[:nsqlookupd] = lookupd
120
+ else
121
+ raise FastlyNsq::ConnectionFailed, "One of FastlyNsq.consumer_nsqds or FastlyNsq.lookupd_http_addresses must be present"
122
+ end
123
+
124
+ Nsq::Consumer.new(opts)
112
125
  end
113
126
  end
@@ -36,13 +36,11 @@ class FastlyNsq::Feeder
36
36
  # @see Nsq::Connection#read_loop
37
37
  def push(message)
38
38
  FastlyNsq.manager.pool.post(priority) do
39
- begin
40
- processor.call(message)
41
- rescue => ex
42
- FastlyNsq.logger.error ex
43
- FastlyNsq.tracer.notice_error ex
44
- raise ex
45
- end
39
+ processor.call(message)
40
+ rescue => ex
41
+ FastlyNsq.logger.error ex
42
+ FastlyNsq.tracer.notice_error ex
43
+ raise ex
46
44
  end
47
45
  end
48
46
  end