queued_mail 0.1.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.
Files changed (72) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +118 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/javascripts/queued_mail/application.js +15 -0
  5. data/app/assets/stylesheets/queued_mail/application.css +13 -0
  6. data/app/controllers/queued_mail/application_controller.rb +4 -0
  7. data/app/helpers/queued_mail/application_helper.rb +4 -0
  8. data/app/models/queued_mail/message.rb +18 -0
  9. data/app/views/layouts/queued_mail/application.html.erb +14 -0
  10. data/config/routes.rb +2 -0
  11. data/db/migrate/20120906053621_create_queued_mail_messages.rb +19 -0
  12. data/lib/queued_mail/delivery_method.rb +29 -0
  13. data/lib/queued_mail/engine.rb +14 -0
  14. data/lib/queued_mail/job.rb +15 -0
  15. data/lib/queued_mail/mailer.rb +18 -0
  16. data/lib/queued_mail/queue/amazon_sqs.rb +54 -0
  17. data/lib/queued_mail/queue/resque.rb +22 -0
  18. data/lib/queued_mail/version.rb +3 -0
  19. data/lib/queued_mail.rb +13 -0
  20. data/lib/tasks/queued_mail_tasks.rake +11 -0
  21. data/test/dummy/README.rdoc +261 -0
  22. data/test/dummy/Rakefile +7 -0
  23. data/test/dummy/app/assets/javascripts/application.js +15 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  25. data/test/dummy/app/controllers/application_controller.rb +3 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/mailers/sample_mailer.rb +28 -0
  28. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/test/dummy/app/views/sample_mailer/attachment_test.html.erb +2 -0
  30. data/test/dummy/app/views/sample_mailer/attachment_test.text.erb +1 -0
  31. data/test/dummy/app/views/sample_mailer/inline_attachment_test.html.erb +2 -0
  32. data/test/dummy/app/views/sample_mailer/inline_attachment_test.text.erb +2 -0
  33. data/test/dummy/app/views/sample_mailer/multipart_test.html.erb +1 -0
  34. data/test/dummy/app/views/sample_mailer/multipart_test.text.erb +1 -0
  35. data/test/dummy/app/views/sample_mailer/simple.text.erb +3 -0
  36. data/test/dummy/config/application.rb +65 -0
  37. data/test/dummy/config/aws.yml +8 -0
  38. data/test/dummy/config/boot.rb +10 -0
  39. data/test/dummy/config/database.yml +25 -0
  40. data/test/dummy/config/environment.rb +5 -0
  41. data/test/dummy/config/environments/development.rb +40 -0
  42. data/test/dummy/config/environments/production.rb +67 -0
  43. data/test/dummy/config/environments/test.rb +36 -0
  44. data/test/dummy/config/initializers/aws.rb +7 -0
  45. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  46. data/test/dummy/config/initializers/inflections.rb +15 -0
  47. data/test/dummy/config/initializers/load_rasqueue.rb +8 -0
  48. data/test/dummy/config/initializers/mime_types.rb +5 -0
  49. data/test/dummy/config/initializers/secret_token.rb +7 -0
  50. data/test/dummy/config/initializers/session_store.rb +8 -0
  51. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  52. data/test/dummy/config/locales/en.yml +5 -0
  53. data/test/dummy/config/resque.yml +3 -0
  54. data/test/dummy/config/routes.rb +5 -0
  55. data/test/dummy/config.ru +4 -0
  56. data/test/dummy/db/development.sqlite3 +0 -0
  57. data/test/dummy/db/migrate/20120906073754_create_queued_mail_messages.queued_mail.rb +20 -0
  58. data/test/dummy/db/schema.rb +32 -0
  59. data/test/dummy/file/cat.jpg +0 -0
  60. data/test/dummy/log/development.log +75427 -0
  61. data/test/dummy/public/404.html +26 -0
  62. data/test/dummy/public/422.html +26 -0
  63. data/test/dummy/public/500.html +25 -0
  64. data/test/dummy/public/favicon.ico +0 -0
  65. data/test/dummy/script/rails +6 -0
  66. data/test/dummy/test/functional/sample_mailer_test.rb +12 -0
  67. data/test/fixtures/queued_mail/messages.yml +21 -0
  68. data/test/integration/navigation_test.rb +10 -0
  69. data/test/queued_mail_test.rb +7 -0
  70. data/test/test_helper.rb +15 -0
  71. data/test/unit/queued_mail/message_test.rb +9 -0
  72. metadata +205 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,118 @@
1
+ = QueuedMail
2
+
3
+ Author:: Yuichi Takeuchi uzuki05@takeyu-web.com
4
+ Website:: http://takeyu-web.com/
5
+ Copyright:: Copyright 2012 Yuichi Takeuchi
6
+ License:: MIT-LICENSE.
7
+
8
+
9
+ == 何が嬉しいの?
10
+
11
+ 時間のかかるメール送信処理をバックグラウンドで処理することで、ユーザーに素早いレスポンスを返すことができます。
12
+
13
+ ActionMailer::Base#deliverでを実際のメール送信ではなくジョブキューへの登録を行うようになります。
14
+ 特徴はActionMailerの設定を変更するだけよく、アプリケーションコードへの変更が不要なことです。
15
+ このため、とても簡単に導入することができます。
16
+
17
+ なおジョブキューは、選べる2タイプ。
18
+
19
+ - Amazon SQS (要 aws-sdk gem)
20
+ - Resque (要 resque gem & Redis)
21
+
22
+ == 環境
23
+
24
+ - Ruby 1.9.3
25
+ - Ruby on Rails 3.2.8
26
+
27
+ これより古い環境は今のところ試してないのでわかりません。
28
+
29
+
30
+ == インストール
31
+
32
+ Gemfile
33
+
34
+ gem 'queued_mail', git: 'git://github.com/uzuki05/queued_mail.git'
35
+
36
+ bundle install
37
+
38
+ マイグレーション。
39
+
40
+ rake queued_mail:install:migrations
41
+ rake db:migrate
42
+
43
+ 設定変更。mail_queue_outbound_delivery_method には :test 含め、delivery_methodに指定可能なものならなんでも大丈夫、のはず。
44
+
45
+ # in config/environments/production.rb (or other environment)
46
+
47
+ config.action_mailer.delivery_method = :queued
48
+ config.mail_queue_service = :resque # or :amazon_sqs
49
+ config.mail_queue_name = :mail_queue
50
+ config.mail_queue_outbound_delivery_method = :smtp # default: :sendmail
51
+ config.action_mailer.smtp_settings = {
52
+ address: "smtp.sendgrid.net",
53
+ port: 25,
54
+ domain: "site.com",
55
+ user_name: "sendgrid@site.com",
56
+ password: "smtp-password"
57
+ }
58
+
59
+ ワーカー起動
60
+
61
+ PIDFILE=/tmp/queued_mail.pid rake environment queued_mail:work
62
+
63
+ もちろんPIDFILEは省略できます。
64
+
65
+ またはResqueであれば以下のようにもできます。
66
+
67
+ QUEUE=mail_queue rake environment resque:work
68
+
69
+ 実運用では適当にdaemon化すればよいと思います。
70
+
71
+
72
+ == キューの設定
73
+
74
+ 高速性優先のResqueと手軽さ優先のSQSが選択できます。
75
+
76
+ === Resqueを使う(デフォルト)
77
+
78
+ (1) Redisサーバーを準備する
79
+
80
+ (2) Railsでresqueを使用する設定を行う
81
+
82
+ (3) config/application.rb等でキューとしてResqueを使うことを設定する(省略可能)
83
+ config.mail_queue_service = :resque
84
+
85
+
86
+ === Amazon SQSを使う
87
+
88
+ (1) AWS ManagementConsoleなどでキューを作成
89
+
90
+ (2) Railsでaws-sdkを使用する設定を行う(config/aws.yml など。必要に応じてsqs_endpoint も設定する。)
91
+
92
+ (3) config/application.rb等でキューとしてAmazonSQSを使うことを設定する
93
+ config.mail_queue_service = :amazon_sqs
94
+ config.mail_queue_name = '1で作成したキューの名前'
95
+
96
+
97
+ == TODO
98
+
99
+ - Workerとかめんどくさい人用 cronで叩けるコントローラ
100
+
101
+ - テスト
102
+
103
+
104
+ == 元ネタ
105
+
106
+ こういうのを作りたいなと思ったらもうあったので参考にさせて戴きました。
107
+
108
+ Customizing ActionMailer delivery methods
109
+ https://gist.github.com/1176236
110
+
111
+
112
+ == 注意
113
+
114
+ - 仕様変更があっても泣かない。
115
+
116
+ - バグっても泣かない。
117
+
118
+ - 何が起きても責任はとれません。
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'QueuedMail'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module QueuedMail
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module QueuedMail
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,18 @@
1
+ require 'mail/elements/address'
2
+
3
+ module QueuedMail
4
+ class Message < ActiveRecord::Base
5
+ attr_accessible :body, :recipient_address, :recipient_name, :reply_to_address, :reply_to_name, :sender_address, :sender_name, :subject, :content_type, :mime_version, :content_transfer_encoding
6
+
7
+ %w(sender recipient reply_to).each do |key|
8
+ class_eval %Q{
9
+ def formatted_#{key}
10
+ address = Mail::Address.new
11
+ address.address = #{key}_address
12
+ address.display_name = #{key}_name
13
+ address.to_s
14
+ end
15
+ }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>QueuedMail</title>
5
+ <%= stylesheet_link_tag "queued_mail/application", :media => "all" %>
6
+ <%= javascript_include_tag "queued_mail/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ QueuedMail::Engine.routes.draw do
2
+ end
@@ -0,0 +1,19 @@
1
+ class CreateQueuedMailMessages < ActiveRecord::Migration
2
+ def change
3
+ create_table :queued_mail_messages do |t|
4
+ t.string :subject
5
+ t.binary :body
6
+ t.string :recipient_address
7
+ t.string :recipient_name
8
+ t.string :sender_address
9
+ t.string :sender_name
10
+ t.string :reply_to_address
11
+ t.string :reply_to_name
12
+ t.string :content_type
13
+ t.string :mime_version
14
+ t.string :content_transfer_encoding
15
+
16
+ t.timestamps
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ module QueuedMail
2
+ class DeliveryMethod
3
+ def initialize(options)
4
+ @options = options
5
+ end
6
+
7
+ def deliver!(mail)
8
+ message = QueuedMail::Message.new(:subject => mail.subject,
9
+ :body => mail.body.encoded(mail.content_transfer_encoding),
10
+ :recipient_address => mail.to.first,
11
+ :recipient_name => mail[:to].display_names.first,
12
+ :sender_address => mail.from.first,
13
+ :sender_name => mail[:from].display_names.first,
14
+ :reply_to_address => mail.reply_to ? mail.reply_to.first : nil,
15
+ :reply_to_name => mail.reply_to ? mail[:reply_to].display_names.first : nil,
16
+ :content_type => mail.content_type,
17
+ :mime_version => mail.mime_version,
18
+ :content_transfer_encoding => mail.content_transfer_encoding)
19
+ message.save
20
+ enqueue(message.id)
21
+ end
22
+
23
+ private
24
+ def enqueue(message_id)
25
+ service = instance_eval("QueuedMail::Queue::#{ Rails.application.config.mail_queue_service.to_s.camelcase }")
26
+ service.enqueue(message_id)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ require 'queued_mail/delivery_method'
2
+ module QueuedMail
3
+ class Engine < ::Rails::Engine
4
+ engine_name "queued_mail"
5
+ isolate_namespace QueuedMail
6
+
7
+ ActionMailer::Base.add_delivery_method :queued, QueuedMail::DeliveryMethod
8
+
9
+ # default configurations
10
+ config.mail_queue_service = :resque
11
+ config.mail_queue_name = :mail_queue
12
+ config.mail_queue_outbound_delivery_method = :sendmail
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module QueuedMail
2
+ class Job
3
+ @queue = Rails.application.config.mail_queue_name.to_sym
4
+
5
+ def self.perform(args)
6
+ message = QueuedMail::Message.find(args["message_id"].to_i)
7
+
8
+ message.lock!
9
+ QueuedMail::Mailer.original_email(message).deliver
10
+ message.destroy
11
+ rescue ActiveRecord::RecordNotFound => e
12
+ # nothing raises
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module QueuedMail
2
+ class Mailer < ActionMailer::Base
3
+ self.delivery_method = Rails.application.config.mail_queue_outbound_delivery_method
4
+ layout nil
5
+
6
+ def original_email(message)
7
+ mail = mail(:from => message.formatted_sender,
8
+ :to => message.formatted_recipient,
9
+ :reply_to => message.formatted_reply_to,
10
+ :subject => message.subject,
11
+ :content_type => message.content_type,
12
+ :mime_version => message.mime_version,
13
+ :content_transfer_encoding => message.content_transfer_encoding)
14
+
15
+ mail.body = message.body
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,54 @@
1
+ module QueuedMail
2
+ module Queue
3
+ module AmazonSqs
4
+ module ModuleMethods
5
+ def enqueue(message_id)
6
+ begin
7
+ sent_message = queue.send_message(JSON.dump('message_id' => message_id))
8
+ Rails.logger.info "send message_id:#{sent_message.message_id} md5:#{sent_message.md5} (id:#{message_id})"
9
+ rescue => e
10
+ Rails.logger.error e
11
+ end
12
+ end
13
+
14
+ def dequeue
15
+ raise "TODO"
16
+ end
17
+
18
+ def task
19
+ if ENV['PIDFILE']
20
+ File.open(ENV['PIDFILE'], 'w'){ |f| f << Process.pid }
21
+ end
22
+
23
+ begin
24
+ queue.poll(poll_interval: 5, batch_size: 1) do |received_message|
25
+ begin
26
+ Rails.logger.info "receive message_id:#{received_message.id} md5:#{received_message.md5}"
27
+ args = JSON.parse(received_message.body)
28
+ QueuedMail::Job.perform(args)
29
+ rescue => e
30
+ received_message.visibility_timeout = 60
31
+ raise e
32
+ end
33
+ end
34
+ rescue SignalException
35
+ # C-c
36
+ rescue Exception => e
37
+ Rails.logger.error e
38
+ retry
39
+ end
40
+ end
41
+
42
+ private
43
+ def sqs
44
+ @sqs ||= AWS::SQS.new
45
+ end
46
+
47
+ def queue
48
+ sqs.queues.named(Rails.application.config.mail_queue_name.to_s)
49
+ end
50
+ end
51
+ extend ModuleMethods
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ module QueuedMail
2
+ module Queue
3
+ module Resque
4
+ module ModuleMethods
5
+ def enqueue(message_id)
6
+ ::Resque.enqueue(QueuedMail::Job, message_id: message_id)
7
+ end
8
+
9
+ def dequeue
10
+ raise "TODO"
11
+ end
12
+
13
+ def task
14
+ require 'resque/tasks'
15
+ ENV['QUEUES'] = Rails.application.config.mail_queue_name.to_s
16
+ Rake::Task['resque:work'].invoke
17
+ end
18
+ end
19
+ extend ModuleMethods
20
+ end
21
+ end
22
+ end