nsq-ruby-fastly 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +42 -0
- data/LICENSE.txt +22 -0
- data/README.md +421 -0
- data/lib/nsq/client_base.rb +120 -0
- data/lib/nsq/connection.rb +466 -0
- data/lib/nsq/consumer.rb +100 -0
- data/lib/nsq/discovery.rb +98 -0
- data/lib/nsq/exceptions.rb +5 -0
- data/lib/nsq/frames/error.rb +6 -0
- data/lib/nsq/frames/frame.rb +16 -0
- data/lib/nsq/frames/message.rb +33 -0
- data/lib/nsq/frames/response.rb +6 -0
- data/lib/nsq/logger.rb +38 -0
- data/lib/nsq/producer.rb +94 -0
- data/lib/nsq.rb +13 -0
- data/lib/version.rb +9 -0
- metadata +117 -0
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'frame'
|
2
|
+
|
3
|
+
module Nsq
|
4
|
+
class Message < Frame
|
5
|
+
|
6
|
+
attr_reader :attempts
|
7
|
+
attr_reader :id
|
8
|
+
attr_reader :body
|
9
|
+
|
10
|
+
def initialize(data, connection)
|
11
|
+
super
|
12
|
+
@timestamp_in_nanoseconds, @attempts, @id, @body = @data.unpack('Q>S>a16a*')
|
13
|
+
@body.force_encoding('UTF-8')
|
14
|
+
end
|
15
|
+
|
16
|
+
def finish
|
17
|
+
connection.fin(id)
|
18
|
+
end
|
19
|
+
|
20
|
+
def requeue(timeout = 0)
|
21
|
+
connection.req(id, timeout)
|
22
|
+
end
|
23
|
+
|
24
|
+
def touch
|
25
|
+
connection.touch(id)
|
26
|
+
end
|
27
|
+
|
28
|
+
def timestamp
|
29
|
+
Time.at(@timestamp_in_nanoseconds / 1_000_000_000.0)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/nsq/logger.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'logger'
|
2
|
+
module Nsq
|
3
|
+
@@logger = Logger.new(nil)
|
4
|
+
|
5
|
+
|
6
|
+
def self.logger
|
7
|
+
@@logger
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def self.logger=(new_logger)
|
12
|
+
@@logger = new_logger
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
module AttributeLogger
|
17
|
+
def self.included(klass)
|
18
|
+
klass.send :class_variable_set, :@@log_attributes, []
|
19
|
+
end
|
20
|
+
|
21
|
+
%w(fatal error warn info debug).map{|m| m.to_sym}.each do |level|
|
22
|
+
define_method level do |msg|
|
23
|
+
Nsq.logger.send(level, "#{prefix} #{msg}")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
private
|
29
|
+
def prefix
|
30
|
+
attrs = self.class.send(:class_variable_get, :@@log_attributes)
|
31
|
+
if attrs.count > 0
|
32
|
+
"[#{attrs.map{|a| "#{a.to_s}: #{self.send(a)}"}.join(' ')}] "
|
33
|
+
else
|
34
|
+
''
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/nsq/producer.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require_relative 'client_base'
|
2
|
+
|
3
|
+
module Nsq
|
4
|
+
class Producer < ClientBase
|
5
|
+
attr_reader :topic
|
6
|
+
|
7
|
+
def initialize(opts = {})
|
8
|
+
@connections = {}
|
9
|
+
@topic = opts[:topic]
|
10
|
+
@discovery_interval = opts[:discovery_interval] || 60
|
11
|
+
@ssl_context = opts[:ssl_context]
|
12
|
+
@tls_options = opts[:tls_options]
|
13
|
+
@tls_v1 = opts[:tls_v1]
|
14
|
+
|
15
|
+
nsqlookupds = []
|
16
|
+
if opts[:nsqlookupd]
|
17
|
+
nsqlookupds = [opts[:nsqlookupd]].flatten
|
18
|
+
discover_repeatedly(
|
19
|
+
nsqlookupds: nsqlookupds,
|
20
|
+
interval: @discovery_interval
|
21
|
+
)
|
22
|
+
|
23
|
+
elsif opts[:nsqd]
|
24
|
+
nsqds = [opts[:nsqd]].flatten
|
25
|
+
nsqds.each{|d| add_connection(d)}
|
26
|
+
|
27
|
+
else
|
28
|
+
add_connection('127.0.0.1:4150')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def write(*raw_messages)
|
33
|
+
if !@topic
|
34
|
+
raise 'No topic specified. Either specify a topic when instantiating the Producer or use write_to_topic.'
|
35
|
+
end
|
36
|
+
|
37
|
+
write_to_topic(@topic, *raw_messages)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Arg 'delay' in seconds
|
41
|
+
def deferred_write(delay, *raw_messages)
|
42
|
+
if !@topic
|
43
|
+
raise 'No topic specified. Either specify a topic when instantiating the Producer or use write_to_topic.'
|
44
|
+
end
|
45
|
+
if delay < 0.0
|
46
|
+
raise "Delay can't be negative, use a positive float."
|
47
|
+
end
|
48
|
+
|
49
|
+
deferred_write_to_topic(@topic, delay, *raw_messages)
|
50
|
+
end
|
51
|
+
|
52
|
+
def write_to_topic(topic, *raw_messages)
|
53
|
+
# return error if message(s) not provided
|
54
|
+
raise ArgumentError, 'message not provided' if raw_messages.empty?
|
55
|
+
|
56
|
+
# stringify the messages
|
57
|
+
messages = raw_messages.map(&:to_s)
|
58
|
+
|
59
|
+
# get a suitable connection to write to
|
60
|
+
connection = connection_for_write
|
61
|
+
|
62
|
+
if messages.length > 1
|
63
|
+
connection.mpub(topic, messages)
|
64
|
+
else
|
65
|
+
connection.pub(topic, messages.first)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def deferred_write_to_topic(topic, delay, *raw_messages)
|
70
|
+
raise ArgumentError, 'message not provided' if raw_messages.empty?
|
71
|
+
messages = raw_messages.map(&:to_s)
|
72
|
+
connection = connection_for_write
|
73
|
+
messages.each do |msg|
|
74
|
+
connection.dpub(topic, (delay * 1000).to_i, msg)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
def connection_for_write
|
80
|
+
# Choose a random Connection that's currently connected
|
81
|
+
# Or, if there's nothing connected, just take any random one
|
82
|
+
connections_currently_connected = connections.select{|_,c| c.connected?}
|
83
|
+
connection = connections_currently_connected.values.sample || connections.values.sample
|
84
|
+
|
85
|
+
# Raise an exception if there's no connection available
|
86
|
+
unless connection
|
87
|
+
raise 'No connections available'
|
88
|
+
end
|
89
|
+
|
90
|
+
connection
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
data/lib/nsq.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative 'version'
|
2
|
+
|
3
|
+
require_relative 'nsq/logger'
|
4
|
+
|
5
|
+
require_relative 'nsq/exceptions'
|
6
|
+
|
7
|
+
require_relative 'nsq/frames/frame'
|
8
|
+
require_relative 'nsq/frames/error'
|
9
|
+
require_relative 'nsq/frames/response'
|
10
|
+
require_relative 'nsq/frames/message'
|
11
|
+
|
12
|
+
require_relative 'nsq/consumer'
|
13
|
+
require_relative 'nsq/producer'
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nsq-ruby-fastly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wistia, Fastly
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jeweler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: nsq-cluster
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: ''
|
70
|
+
email: team-internaleng@fastly.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files:
|
74
|
+
- CHANGELOG.md
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.md
|
77
|
+
files:
|
78
|
+
- CHANGELOG.md
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- lib/nsq.rb
|
82
|
+
- lib/nsq/client_base.rb
|
83
|
+
- lib/nsq/connection.rb
|
84
|
+
- lib/nsq/consumer.rb
|
85
|
+
- lib/nsq/discovery.rb
|
86
|
+
- lib/nsq/exceptions.rb
|
87
|
+
- lib/nsq/frames/error.rb
|
88
|
+
- lib/nsq/frames/frame.rb
|
89
|
+
- lib/nsq/frames/message.rb
|
90
|
+
- lib/nsq/frames/response.rb
|
91
|
+
- lib/nsq/logger.rb
|
92
|
+
- lib/nsq/producer.rb
|
93
|
+
- lib/version.rb
|
94
|
+
homepage: http://github.com/fastly/nsq-ruby
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubygems_version: 3.1.6
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Ruby client library for NSQ
|
117
|
+
test_files: []
|