bender 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 580e6b56f042783fddf80a417c3002560a418321
4
- data.tar.gz: bb53551dcde5a039f504022b09c0182cca4cc5e9
3
+ metadata.gz: 455bba4c31cabaaed55f5b025c56bcf1317c837c
4
+ data.tar.gz: 2c71da1de9f69476af33984647a5cbf4c14612cc
5
5
  SHA512:
6
- metadata.gz: 9731aff9b37a7e4c02cdb47664657312d66f7fcc7a6e7804e5e3fdda2222e69844c732a08788326eecb28dffdab4668a2eda6fbf7a21b62b0445dfb0c3ec7679
7
- data.tar.gz: eb50f5f57ca44478c66ff809e0e4ada7be3a49be81de2227d30ce47b57d6d604b47910273e777a6453d272aee9bbd76eb8dbbe55a291c2a3c13bb17b7860c230
6
+ metadata.gz: 7310497f017bc92f6e7e2127a3e051a536fd91b7b03aa82d8826cc7420dbf34b170e296623ed4536affbd0be9fb321ca40dc9971125eec9774dbe1a58d440d76
7
+ data.tar.gz: ba4f850715b3bcd5e9af539fea50330392b006114cbafc2960bfa63fc02bea3ba1999ecbd675b9bfb5f90abf7c1d5b9dc70af26ac7114f42070f71a2588eca3f
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.join(File.dirname(File.dirname(__FILE__)), 'lib'))
2
+
3
+ require 'bender'
4
+
5
+ hostname = Socket.gethostname
6
+ message = {:hello => :you}
7
+
8
+ config = { :queue_prefix => 'my-custom-prefix', :watchers => [{:name => :watcher_life_cycle_hook}] }
9
+
10
+ bender = Bender::Client.new(hostname, config)
11
+ bender.publish(:watcher_life_cycle_hook, message)
data/lib/bender/cli.rb CHANGED
@@ -20,6 +20,7 @@ module Bender
20
20
  end
21
21
 
22
22
  desc "start", "Start processing watchers."
23
+ option :hostname, :aliases => ["-h"], :type => :string
23
24
  option :require, :aliases => ["-r"], :type => :string
24
25
  option :pid_file, :aliases => ["-p"], :type => :string
25
26
  option :interval, :aliases => ["-i"], :type => :numeric
@@ -30,18 +31,23 @@ module Bender
30
31
  def start
31
32
  load_enviroment(options[:require])
32
33
  opts = @options.symbolize_keys.slice(:timeout, :interval, :daemon, :pid_file)
33
- Bender::Client.new(@options).start_watchers
34
+ Bender::Client.new(options[:hostname] || Socket.gethostname, @options).start_watchers
34
35
  end
35
36
 
36
37
  desc "publish", "Publish a message."
37
- option :watcher, :aliases => ["-w"], :type => :string, :required => true
38
- option :message, :aliases => ["-m"], :type => :string, :required => true
39
- option :ack, :aliases => ["-a"], :type => :boolean, :required => false, :default => false
38
+ option :hostname, :aliases => ["-h"], :type => :string
39
+ option :watcher, :aliases => ["-w"], :type => :string, :required => true
40
+ option :message, :aliases => ["-m"], :type => :string, :required => true
41
+ option :ack, :aliases => ["-a"], :type => :boolean, :required => false, :default => false
40
42
 
41
43
  def publish
42
44
  load_enviroment(options[:require])
43
45
  opts = @options.symbolize_keys.slice(:timeout, :interval, :daemon, :pid_file)
44
- Bender::Client.new(@options).publish(options[:watcher], options[:message], options[:ack])
46
+ Bender::Client.new(options[:hostname] || Socket.gethostname, @options).publish(
47
+ options[:watcher],
48
+ options[:message],
49
+ options[:ack]
50
+ )
45
51
  end
46
52
 
47
53
  protected
data/lib/bender/client.rb CHANGED
@@ -20,10 +20,6 @@ module Bender
20
20
  @@keep_running
21
21
  end
22
22
 
23
- def queue_prefix
24
- @@queue_prefix ||= "#{ENV['QUEUE_PREFIX']}-#{Socket.gethostname}"
25
- end
26
-
27
23
  def sqs
28
24
  @@sqs ||= AWS::SQS.new({
29
25
  :access_key_id => ENV['AWS_ACCESS_KEY'],
@@ -33,9 +29,24 @@ module Bender
33
29
  end
34
30
  end
35
31
 
36
- def initialize(config)
32
+ def initialize(hostname, config = {})
37
33
  @config = config
38
- initialize_watchers
34
+ @queue_name = "#{self.queue_prefix}-#{hostname}"
35
+
36
+ @config[:create_options] ||= {
37
+ :visibility_timeout => 90,
38
+ :maximum_message_size => 262144
39
+ }
40
+
41
+ @config[:poll_options] ||= {
42
+ :wait_time_seconds => 10,
43
+ :idle_timeout => 5
44
+ }
45
+ initialize_watchers if @config[:watchers]
46
+ end
47
+
48
+ def queue_prefix
49
+ @queue_prefix ||= self.config[:queue_prefix] || "#{ENV['QUEUE_PREFIX']}"
39
50
  end
40
51
 
41
52
  def trap_signals
@@ -67,7 +78,7 @@ module Bender
67
78
  end
68
79
 
69
80
  def publish(watcher, message, ack = false)
70
- sent = @watchers.select{|w| w.class.to_s.underscore == watcher}.collect do |watcher|
81
+ @watchers.select{|w| w.class.to_s.underscore == watcher.to_s}.collect do |watcher|
71
82
  if ack
72
83
  with_confirmation do |cq|
73
84
  watcher.publish(message, cq)
@@ -75,8 +86,8 @@ module Bender
75
86
  else
76
87
  watcher.publish(message)
77
88
  end
89
+ Bender.logger.info("Sent message to #{watcher.name}")
78
90
  end
79
- Bender.logger.info("Sent #{sent.size} message(s)")
80
91
  end
81
92
 
82
93
  def config
@@ -92,12 +103,12 @@ module Bender
92
103
  def initialize_watchers
93
104
  @watchers = @config[:watchers].collect do |watcher_config|
94
105
  Bender.logger.info("Loading #{watcher_config[:name]}")
95
- WatcherFactory.create(watcher_config, self.config)
106
+ WatcherFactory.create(@queue_name, watcher_config, self.config)
96
107
  end
97
108
  end
98
109
 
99
110
  def with_confirmation
100
- name = "#{Bender::Client.queue_prefix}-cq-#{SecureRandom.uuid}"
111
+ name = "#{@queue_name}-cq-#{SecureRandom.uuid}"
101
112
  cq = Bender::Client.sqs.queues.create(name, self.config[:create_options])
102
113
 
103
114
  # send message
@@ -1,3 +1,3 @@
1
1
  module Bender
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,18 +1,19 @@
1
1
  module WatcherFactory
2
- def self.create(config, default_config)
3
- watcher_class = config[:name]
2
+ def self.create(queue_name, config, default_config)
3
+ watcher_class = config[:name].to_s
4
4
  require "bender/watchers/#{watcher_class}"
5
- watcher_class.classify.constantize.new(default_config)
5
+ watcher_class.classify.constantize.new(queue_name, default_config)
6
6
  end
7
7
  end
8
8
 
9
9
  class Watcher
10
10
 
11
11
  def name
12
- @name ||= "#{Bender::Client.queue_prefix}-#{self.class.to_s.underscore}"
12
+ @name ||= "#{@queue_name}-#{self.class.to_s.underscore}"
13
13
  end
14
14
 
15
- def initialize(options)
15
+ def initialize(queue_name, options)
16
+ @queue_name = queue_name
16
17
  @options = options
17
18
  load_queue
18
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Leak
@@ -154,6 +154,7 @@ files:
154
154
  - Rakefile
155
155
  - bender.gemspec
156
156
  - bin/bender
157
+ - examples/send_message.rb
157
158
  - lib/bender.rb
158
159
  - lib/bender/cli.rb
159
160
  - lib/bender/client.rb