simple_message_queue 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,12 +25,14 @@ You can also pass in a few other variables to the configure block such as an idl
25
25
 
26
26
  ```ruby
27
27
  SimpleMessageQueue.configure do |config|
28
- config.access_key_id = 'your_access_key_id'
29
- config.secret_access_key= 'your_secret_access_key'
30
- config.environment = Rails.env
31
- config.idle_timeout = 10 # optional
32
- config.wait_time_seconds = 20 # optional
33
- config.logger = Logger.new('simple_message_queue.log') # optional
28
+ config.access_key_id = 'your_access_key_id' # String (required)
29
+ config.secret_access_key= 'your_secret_access_key' # String (required)
30
+ config.environment = Rails.env # String (required)
31
+ config.idle_timeout = 10 # Integer (optional)
32
+ config.wait_time_seconds = 20 # Integer (optional)
33
+ config.logger = Logger.new('simple_message_queue.log') # Logger (optional)
34
+ config.sns_notifications = true # Boolean (optional)
35
+ config.sns_notification_prefix = 'my_prefix' # String (alphanumeric, hyphen and underscore only) (optional)
34
36
  end
35
37
  ```
36
38
 
@@ -54,7 +56,7 @@ By default, your SQS queue will be named after the model you created and appende
54
56
  class TestQueue
55
57
  extend SimpleMessageQueue
56
58
 
57
- @queue_name = 'super_awesome_queue'
59
+ @queue_name = 'super_awesome_queue' # String (alphanumeric, hyphen and underscore only) (optional)
58
60
  end
59
61
  ```
60
62
 
@@ -125,6 +127,26 @@ This is the default [name]_daemon.rb file generated, with out code placed in the
125
127
 
126
128
  **NOTE:** You will want to monitor these daemons and have something to restart them if they stop. Something like God (http://godrb.com/) or Monit (http://mmonit.com/monit/) will work nicely.
127
129
 
130
+ ### SNS Notifications
131
+
132
+ Simple Message Queue allows you to send notifications via Amazon's SNS. Notifications will be sent when your queue fails to send a message.
133
+
134
+ In order to set up notifications, add sns_notifications = true to your config. If you would like to prefix your queues with a name, add sns_notification_prefix = 'prefix'.
135
+
136
+ ```ruby
137
+ SimpleMessageQueue.configure do |config|
138
+ ...
139
+ config.sns_notifications = true
140
+ config.sns_notification_prefix = 'my_prefix'
141
+ end
142
+ ```
143
+
144
+ After the configure block is parsed (usually during initialization), the SNS Topics are automatically created, allowing you to subscribe to those topics before a message is ever sent.
145
+
146
+ **Note:** Although the SNS Topics are created automatically, and messages will be sent to those topics, you will not receive any notifications until you create subscriptions to those topics. After your topics have been created, log into your AWS account and navigate to the SNS page. For each topic here, you will need to create at least one subscription in order to receive notifications.
147
+
148
+ **Note:** SNS Topics are created for each environment (similar to the queues). You will need to subscribe to the topics after each environment is initialized in order to make sure you receive messages for each environment. You can also set up different subscriptions for each environment (e.g. maybe you only want to receive email notifications in development, but would like to receive email and SMS in production).
149
+
128
150
  ### Single Site Communication
129
151
 
130
152
  If you are using Simple Message Queue for background processing on a single site, all you need to do is create your model and extend SimpleMessageQueue. You will then be able to call the send and receive methods on that model.
@@ -1,6 +1,6 @@
1
1
  module SimpleMessageQueue
2
2
  class Configuration
3
- attr_accessor :access_key_id, :secret_access_key, :logger, :idle_timeout, :wait_time_seconds, :environment
3
+ attr_accessor :access_key_id, :secret_access_key, :logger, :idle_timeout, :wait_time_seconds, :environment, :sns_notifications, :sns_notification_prefix
4
4
 
5
5
  def initialize
6
6
  @access_key_id = nil
@@ -9,6 +9,8 @@ module SimpleMessageQueue
9
9
  @idle_timeout = 10
10
10
  @wait_time_seconds = 20
11
11
  @environment = nil
12
+ @sns_notifications = false
13
+ @sns_notification_prefix = nil
12
14
  end
13
15
  end
14
16
  end
@@ -0,0 +1,72 @@
1
+ module SimpleMessageQueue
2
+ class Notification
3
+
4
+ class << self
5
+
6
+ def sns
7
+ raise SimpleMessageQueue::ConfigurationError unless SimpleMessageQueue.configuration
8
+ raise SimpleMessageQueue::EnvironmentError unless defined?(SimpleMessageQueue.configuration.environment)
9
+ @@sns ||= AWS::SNS.new(:access_key_id => SimpleMessageQueue.configuration.access_key_id, :secret_access_key => SimpleMessageQueue.configuration.secret_access_key)
10
+ end
11
+
12
+ end
13
+
14
+ class Topic
15
+
16
+ attr_reader :sns_topic
17
+
18
+ class << self
19
+ def find(name)
20
+ topic_name = topic_name(name)
21
+ sns_topic = SimpleMessageQueue::Notification.sns.topics.find { |t| t.name == topic_name }
22
+ topic = (sns_topic) ? Topic.new(sns_topic.name, false) : nil
23
+ end
24
+
25
+ def find_by_full_name(full_name)
26
+ sns_topic = SimpleMessageQueue::Notification.sns.topics.find { |t| t.name == full_name }
27
+ topic = (sns_topic) ? Topic.new(sns_topic.name, false) : nil
28
+ end
29
+
30
+ def topic_name(name)
31
+ if defined?(SimpleMessageQueue.configuration.sns_notification_prefix) && !SimpleMessageQueue.configuration.sns_notification_prefix.nil?
32
+ topic_name = "#{SimpleMessageQueue.configuration.sns_notification_prefix}_#{name}_#{SimpleMessageQueue.configuration.environment}"
33
+ else
34
+ topic_name = "#{name}_#{SimpleMessageQueue.configuration.environment}"
35
+ end
36
+ topic_name
37
+ end
38
+ end
39
+
40
+ def sns
41
+ SimpleMessageQueue::Notification.sns
42
+ end
43
+
44
+ def initialize(name, generate_topic_name=true)
45
+ raise SimpleMessageQueue::ConfigurationError unless SimpleMessageQueue.configuration
46
+ raise SimpleMessageQueue::EnvironmentError unless defined?(SimpleMessageQueue.configuration.environment)
47
+
48
+ topic_name = (generate_topic_name) ? self.class.topic_name(name) : name
49
+ @sns_topic = sns.topics.create(topic_name)
50
+ end
51
+
52
+ def name
53
+ @sns_topic.name
54
+ end
55
+
56
+ def send(message, subject=nil)
57
+ message_hash = {
58
+ topic_arn: @sns_topic.arn,
59
+ message: message
60
+ }
61
+ message_hash[:subject] = subject if subject
62
+ sns.client.publish(message_hash)
63
+ end
64
+
65
+ def delete
66
+ @sns_topic.delete
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleMessageQueue
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'simple_message_queue/configuration'
2
2
  require 'simple_message_queue/core_ext/string'
3
3
  require 'simple_message_queue/errors'
4
+ require 'simple_message_queue/notification'
4
5
  require 'aws'
5
6
 
6
7
  module SimpleMessageQueue
@@ -11,6 +12,14 @@ module SimpleMessageQueue
11
12
  def configure
12
13
  self.configuration ||= Configuration.new
13
14
  yield(configuration)
15
+
16
+ if self.configuration.sns_notifications
17
+ topics = ['send_message_failure']
18
+ topics.each do |topic|
19
+ SimpleMessageQueue::Notification::Topic.new(topic)
20
+ end
21
+ end
22
+
14
23
  end
15
24
 
16
25
  end
@@ -51,12 +60,21 @@ module SimpleMessageQueue
51
60
  end
52
61
 
53
62
  def send(message)
54
- queue.send_message(message)
63
+ begin
64
+ queue.send_message(message)
65
+ rescue
66
+ logger.error "There was an error when sending an item to #{queue_name} at #{DateTime.now}."
67
+
68
+ if defined?(SimpleMessageQueue.configuration.sns_notifications) && SimpleMessageQueue.configuration.sns_notifications == true
69
+ topic = SimpleMessageQueue::Notification::Topic.new('send_message_failure')
70
+ topic.send("There was an error when sending an item to #{queue_name} at #{DateTime.now}.", "SimpleMessageQueue: Send Message Failure")
71
+ end
72
+ end
55
73
  end
56
74
 
57
75
  def logger
58
76
  if SimpleMessageQueue.configuration.logger
59
- @logger ||= SimpleMessageQueue.configuration.logger
77
+ @logger = SimpleMessageQueue.configuration.logger
60
78
  else
61
79
  @logger ||= Logger.new(STDOUT)
62
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_message_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-07 00:00:00.000000000 Z
12
+ date: 2013-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
16
- requirement: &70130285737680 !ruby/object:Gem::Requirement
16
+ requirement: &70212523769780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70130285737680
24
+ version_requirements: *70212523769780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &70130285736440 !ruby/object:Gem::Requirement
27
+ requirement: &70212523769360 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70130285736440
35
+ version_requirements: *70212523769360
36
36
  description: SimpleMessageQueue is a simple interface for Amazon Web Service's SQS.
37
37
  email:
38
38
  - jim@jimsmithdesign.com
@@ -45,6 +45,7 @@ files:
45
45
  - lib/simple_message_queue/configuration.rb
46
46
  - lib/simple_message_queue/core_ext/string.rb
47
47
  - lib/simple_message_queue/errors.rb
48
+ - lib/simple_message_queue/notification.rb
48
49
  - lib/simple_message_queue/version.rb
49
50
  - lib/simple_message_queue.rb
50
51
  - README.md