sidekiq-mailer 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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +46 -0
- data/Rakefile +2 -0
- data/lib/sidekiq/mailer.rb +25 -0
- data/lib/sidekiq/mailer/proxy.rb +25 -0
- data/lib/sidekiq/mailer/version.rb +5 -0
- data/lib/sidekiq/mailer/worker.rb +24 -0
- data/sidekiq-mailer.gemspec +21 -0
- data/spec/sidekiq/mailer/proxy_spec.rb +36 -0
- data/spec/sidekiq/mailer/worker_spec.rb +18 -0
- data/spec/sidekiq/mailer_spec.rb +26 -0
- data/spec/spec_helper.rb +11 -0
- metadata +87 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Andre Arko
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Sidekiq::Mailer
|
2
|
+
|
3
|
+
This gem lets you send mail in the background using Sidekiq.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add the gem to your Gemfile:
|
8
|
+
|
9
|
+
gem 'sidekiq-mailer'
|
10
|
+
|
11
|
+
If you're using Bundler 1.0, you'll need to tell Bundler the library file name:
|
12
|
+
|
13
|
+
gem 'sidekiq-mailer', :require => 'sidekiq/mailer'
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Install into your mailer:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class BackgroundMailer < ActionMailer::Base
|
21
|
+
extend Sidekiq::Mailer
|
22
|
+
|
23
|
+
# optional queue name
|
24
|
+
# queue :mail
|
25
|
+
|
26
|
+
def send_mail(record)
|
27
|
+
mail :to => record.email
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
Queue mail jobs using the DelayedJob background mail style:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
BackgroundMailer.delay.send_mail(record)
|
36
|
+
```
|
37
|
+
|
38
|
+
To actually your mail, run `sidekiq`. If you set your mail queue name to "mail", run `sidekiq -q mail` instead.
|
39
|
+
|
40
|
+
## Caveats
|
41
|
+
|
42
|
+
Right now, it only expects a single ActiveRecord instance as the argument passed to whatever email method you call. This could become a problem in the future, but it's working for me. If it's a problem for you, see the next section. :)
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Please do, pull requests happily discussed and accepted.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "sidekiq/mailer/version"
|
2
|
+
require "sidekiq/mailer/proxy"
|
3
|
+
require "sidekiq/mailer/worker"
|
4
|
+
|
5
|
+
module Sidekiq
|
6
|
+
module Mailer
|
7
|
+
|
8
|
+
def delay
|
9
|
+
Proxy.new(self, @queue)
|
10
|
+
end
|
11
|
+
|
12
|
+
def queue(name)
|
13
|
+
@queue = name
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.logger
|
17
|
+
@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.logger=(logger)
|
21
|
+
@logger = logger
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "sidekiq/client"
|
2
|
+
|
3
|
+
module Sidekiq
|
4
|
+
module Mailer
|
5
|
+
class Proxy
|
6
|
+
|
7
|
+
def initialize(mailer, queue = "default")
|
8
|
+
@mailer = mailer
|
9
|
+
@queue = queue
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(method, *args)
|
13
|
+
# assuming that mailer methods take a single AR record may bite us later
|
14
|
+
record = args.first
|
15
|
+
if @mailer.respond_to?(method)
|
16
|
+
Sidekiq::Client.push(@queue, "class" => "Sidekiq::Mailer::Worker",
|
17
|
+
"args" => [@mailer.to_s, method, record.class.to_s, record.id])
|
18
|
+
else
|
19
|
+
raise NoMethodError, "undefined method `#{method}' for #{@mailer}:#{@mailer.class}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "sidekiq/worker"
|
2
|
+
require "sidekiq/util"
|
3
|
+
|
4
|
+
module Sidekiq
|
5
|
+
module Mailer
|
6
|
+
class Worker
|
7
|
+
include Sidekiq::Worker
|
8
|
+
include Sidekiq::Util
|
9
|
+
|
10
|
+
def perform(mailer_name, method_name, record_class, record_id)
|
11
|
+
record = constantize(record_class).find(record_id)
|
12
|
+
mailer = constantize(mailer_name)
|
13
|
+
mailer.send(:new, method_name, record).message.deliver
|
14
|
+
rescue => e
|
15
|
+
logger.warn "Could not call #{mailer_name}.#{method_name}. " \
|
16
|
+
"#{e.class}: #{e.message}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def logger
|
20
|
+
Sidekiq::Mailer.logger
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/sidekiq/mailer/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["André Arko"]
|
6
|
+
gem.email = ["andre@arko.net"]
|
7
|
+
gem.description = %q{Module for Rails mailers that sends the mail in the background. Adapted from the resque_mailer gem.}
|
8
|
+
gem.summary = %q{Use Sidekiq to send Rails emails in the background}
|
9
|
+
gem.homepage = "http://github.com/indirect/sidekiq-mailer"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "sidekiq-mailer"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Sidekiq::Mailer::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "sidekiq", "~> 0.6"
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec", "~> 2.8.0"
|
21
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "sidekiq/mailer/proxy"
|
3
|
+
|
4
|
+
class FakeMailer
|
5
|
+
def self.send_mail(record)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Sidekiq::Mailer::Proxy do
|
10
|
+
let(:record){ mock("AR record", :class => "User", :id => 1) }
|
11
|
+
|
12
|
+
context "when called with an unknown method" do
|
13
|
+
it "raises an exception" do
|
14
|
+
expect { Sidekiq::Mailer::Proxy.new(FakeMailer).wot }.
|
15
|
+
should raise_error(NoMethodError)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "when called with a mailer method" do
|
20
|
+
it "queues a job to call that mailer later" do
|
21
|
+
Sidekiq::Client.should_receive(:push).with("default",
|
22
|
+
"class" => "Sidekiq::Mailer::Worker",
|
23
|
+
"args" => ["FakeMailer", :send_mail, "User", 1])
|
24
|
+
Sidekiq::Mailer::Proxy.new(FakeMailer).send_mail(record)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when given a queue" do
|
29
|
+
it "adds the job to the given queue" do
|
30
|
+
Sidekiq::Client.should_receive(:push).with("mail",
|
31
|
+
"class" => "Sidekiq::Mailer::Worker",
|
32
|
+
"args" => ["FakeMailer", :send_mail, "User", 1])
|
33
|
+
Sidekiq::Mailer::Proxy.new(FakeMailer, "mail").send_mail(record)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "sidekiq/mailer/worker"
|
3
|
+
|
4
|
+
class FakeMailer; end
|
5
|
+
class FakeUser; end
|
6
|
+
|
7
|
+
describe Sidekiq::Mailer::Worker do
|
8
|
+
describe "#perform" do
|
9
|
+
let(:record){ mock("record") }
|
10
|
+
|
11
|
+
it "creates a mailer and sends the message" do
|
12
|
+
FakeUser.should_receive(:find).with(1).and_return(record)
|
13
|
+
FakeMailer.should_receive(:new).with(:send_mail, record)
|
14
|
+
|
15
|
+
subject.perform("FakeMailer", :send_mail, "FakeUser", 1)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "sidekiq/mailer"
|
3
|
+
|
4
|
+
class FakeMailer
|
5
|
+
extend Sidekiq::Mailer
|
6
|
+
end
|
7
|
+
|
8
|
+
describe Sidekiq::Mailer do
|
9
|
+
|
10
|
+
describe "delay method" do
|
11
|
+
it "creates a new proxy" do
|
12
|
+
FakeMailer.delay.should be_a(Sidekiq::Mailer::Proxy)
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when queue is set" do
|
16
|
+
before do
|
17
|
+
FakeMailer.queue(:mail)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "passes proxy the queue name" do
|
21
|
+
Sidekiq::Mailer::Proxy.should_receive(:new).with(FakeMailer, :mail)
|
22
|
+
FakeMailer.delay
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
3
|
+
config.run_all_when_everything_filtered = true
|
4
|
+
config.filter_run :focus
|
5
|
+
end
|
6
|
+
|
7
|
+
require "sidekiq/mailer"
|
8
|
+
require "stringio"
|
9
|
+
require "logger"
|
10
|
+
|
11
|
+
Sidekiq::Mailer.logger = Logger.new(StringIO.new)
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sidekiq-mailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- André Arko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sidekiq
|
16
|
+
requirement: &70121008484240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.6'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70121008484240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70121008483300 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.8.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70121008483300
|
36
|
+
description: Module for Rails mailers that sends the mail in the background. Adapted
|
37
|
+
from the resque_mailer gem.
|
38
|
+
email:
|
39
|
+
- andre@arko.net
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- .rspec
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/sidekiq/mailer.rb
|
51
|
+
- lib/sidekiq/mailer/proxy.rb
|
52
|
+
- lib/sidekiq/mailer/version.rb
|
53
|
+
- lib/sidekiq/mailer/worker.rb
|
54
|
+
- sidekiq-mailer.gemspec
|
55
|
+
- spec/sidekiq/mailer/proxy_spec.rb
|
56
|
+
- spec/sidekiq/mailer/worker_spec.rb
|
57
|
+
- spec/sidekiq/mailer_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: http://github.com/indirect/sidekiq-mailer
|
60
|
+
licenses: []
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.10
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Use Sidekiq to send Rails emails in the background
|
83
|
+
test_files:
|
84
|
+
- spec/sidekiq/mailer/proxy_spec.rb
|
85
|
+
- spec/sidekiq/mailer/worker_spec.rb
|
86
|
+
- spec/sidekiq/mailer_spec.rb
|
87
|
+
- spec/spec_helper.rb
|