que_mailer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +26 -0
  3. data/Gemfile +17 -0
  4. data/LICENSE +22 -0
  5. data/Readme.md +100 -0
  6. data/lib/que_mailer.rb +108 -0
  7. data/lib/que_mailer/version.rb +5 -0
  8. data/que-mailer.gemspec +27 -0
  9. data/rails/init.rb +1 -0
  10. data/spec/dummy/.DS_Store +0 -0
  11. data/spec/dummy/.gitignore +16 -0
  12. data/spec/dummy/Gemfile +8 -0
  13. data/spec/dummy/Rakefile +6 -0
  14. data/spec/dummy/app/mailers/.keep +0 -0
  15. data/spec/dummy/app/mailers/test_mailer.rb +9 -0
  16. data/spec/dummy/app/models/.keep +0 -0
  17. data/spec/dummy/app/models/concerns/.keep +0 -0
  18. data/spec/dummy/app/models/que_job.rb +3 -0
  19. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  20. data/spec/dummy/app/views/test_mailer/test_message.html.erb +3 -0
  21. data/spec/dummy/app/views/test_mailer/test_message.text.erb +3 -0
  22. data/spec/dummy/bin/bundle +3 -0
  23. data/spec/dummy/bin/rails +4 -0
  24. data/spec/dummy/bin/rake +4 -0
  25. data/spec/dummy/config.ru +4 -0
  26. data/spec/dummy/config/application.rb +23 -0
  27. data/spec/dummy/config/boot.rb +4 -0
  28. data/spec/dummy/config/database.yml +5 -0
  29. data/spec/dummy/config/environment.rb +5 -0
  30. data/spec/dummy/config/environments/.DS_Store +0 -0
  31. data/spec/dummy/config/environments/test.rb +37 -0
  32. data/spec/dummy/config/initializers/que.rb +1 -0
  33. data/spec/dummy/config/initializers/que_mailer.rb +1 -0
  34. data/spec/dummy/config/locales/en.yml +23 -0
  35. data/spec/dummy/config/routes.rb +56 -0
  36. data/spec/dummy/db/migrate/20140128035854_add_que.rb +11 -0
  37. data/spec/dummy/db/schema.rb +29 -0
  38. data/spec/dummy/spec/mailers/test_mailer_spec.rb +119 -0
  39. data/spec/dummy/spec/spec_helper.rb +42 -0
  40. data/spec/spec_helper.rb +42 -0
  41. metadata +226 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76950c41a8f0731d5dfb9913246696b6bc0d6e40
4
+ data.tar.gz: 1db73a38c00d32ce502b2f0c29ca942af51e4d1c
5
+ SHA512:
6
+ metadata.gz: 062160386829c352eca887911dd365859941cdd4677901b9b1ff487ce7f06d9afcf1d18fefb767a64cb74ca0d4cb5f2d8ed05c15724cc986a6a239a2e3609177
7
+ data.tar.gz: ec25d54dafc0c92a96a09fc4b764239fa66c7917a59ba3861b400930abfebae313c83a8ada43cad03d38d2c2da5b66de5eaab450d873ca3551047908ea04e709
data/.gitignore ADDED
@@ -0,0 +1,26 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /log
7
+ /tmp
8
+ /db/*.sqlite3
9
+ /public/system
10
+ /coverage/
11
+ /spec/tmp
12
+ **.orig
13
+ rerun.txt
14
+ pickle-email-*.html
15
+ config/initializers/secret_token.rb
16
+ config/secrets.yml
17
+
18
+ ## Environment normalisation:
19
+ /.bundle
20
+ /vendor/bundle
21
+
22
+ # these should all be checked in to normalise the environment:
23
+ # Gemfile.lock, .ruby-version, .ruby-gemset
24
+
25
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
26
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in que_mailer.gemspec
4
+ gemspec
5
+
6
+ rails_version = ENV["RAILS_VERSION"] || "default"
7
+
8
+ rails = case rails_version
9
+ when "master"
10
+ {github: "rails/rails"}
11
+ when "default"
12
+ "~> 4.0.0"
13
+ else
14
+ "~> #{rails_version}"
15
+ end
16
+
17
+ gem 'actionmailer', rails
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Robert Prehn
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,100 @@
1
+ # Que-Mailer
2
+
3
+ Asynchronously send mail using ActionMailer and [Que](https://github.com/chanks/que).
4
+
5
+ So far, this has been tested only in Rails 4.
6
+
7
+ Que is an alternative DelayedJob or QueueClassic and is a queue for
8
+ Ruby. It uses PostgreSQL's [advisory locks]
9
+ (http://www.postgresql.org/docs/current/static/explicit-locking.html#ADVISORY-LOCKS)
10
+ to manage jobs. See the [que repo](https://github.com/chanks/que) for more details.
11
+
12
+ ### Why Que-Mailer?
13
+
14
+ * Que-Mailer uses Postgres rather than Redis, RabbitMQ or other message queue.
15
+ If you already have Postgres on a project, now you have 1 fewer dependency!
16
+ * Que-Mailer can create background workers within your existing process. This means,
17
+ for instance, that your background workers and web server can share a Heroku dyno.
18
+ It also means that you don't have to remember to launch separate workers.
19
+ * All of the benefits of using advisory locks and Postgres (like safety,
20
+ security, and atomic backups.)
21
+
22
+ ### Warning
23
+
24
+ Que and Que-Mailer are fairly new compared to other queue
25
+ solutions. We're still finding bugs and have only tested
26
+ in a limited set of configurations.
27
+
28
+ If you have problems, please [post an issue](https://github.com/prehnRA/que-mailer/issues).
29
+
30
+ ## Installation
31
+
32
+ Right now it is best to use the github master version.
33
+
34
+ Add this line to your application's Gemfile:
35
+
36
+ gem 'que_mailer', :git => 'git://github.com/prehnRA/que-mailer.git', :branch => 'master'
37
+
38
+ And then execute:
39
+
40
+ $ bundle
41
+
42
+ Additionally, you need to follow the steps for installing
43
+ and using Que. Remember to:
44
+
45
+ $ rails generate que:install
46
+ $ rake db:migrate
47
+
48
+ Which will get your database ready to store jobs.
49
+
50
+ ## Use
51
+
52
+ You use Que-Mailer by including it in your mailers, like this:
53
+
54
+ class ExampleMailer < ActionMailer::Base
55
+ include Que::Mailer
56
+ default from: 'from@example.com'
57
+
58
+ def example_message(*args)
59
+ @args = *args
60
+ mail(to: "to@example.com", subject: "Hello World")
61
+ end
62
+ end
63
+
64
+ Then,
65
+
66
+ ExampleMailer.example_message.deliver
67
+
68
+ will send mail using the background workers.
69
+
70
+ ExampleMailer.example_message.deliver!
71
+
72
+ will bypass Que and send the mail directly.
73
+
74
+ ## Scheduling Mail
75
+
76
+ Additionally, you can schedule mail to be sent at a later
77
+ time.
78
+
79
+ ExampleMailer.deliver_in(2.days)
80
+
81
+ will send an email two days from now.
82
+
83
+ ExampleMailer.deliver_at(time)
84
+
85
+ will deliver the email at `time`.
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create new Pull Request
94
+
95
+ You can also help by testing this in your application and
96
+ reporting any issues you encounter.
97
+
98
+ ## License
99
+
100
+ MIT
data/lib/que_mailer.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'que'
2
+
3
+ module Que
4
+ module Mailer
5
+ class << self
6
+ def included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ def deliver?
11
+ true
12
+ end
13
+ end
14
+ module ClassMethods
15
+ def current_env
16
+ if defined?(Rails)
17
+ ::Que::Mailer.current_env || ::Rails.env
18
+ else
19
+ ::Que::Mailer.current_env
20
+ end
21
+ end
22
+
23
+ def method_missing(method_name, *args)
24
+ if action_methods.include?(method_name.to_s)
25
+ MessageDecoy.new(self, method_name, *args)
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ def perform(action, *args)
32
+ begin
33
+ message = self.send(:new, action, *args).message
34
+ message.deliver
35
+ rescue Exception => ex
36
+ if logger
37
+ logger.error "Unable to deliver email [#{action}]: #{ex}"
38
+ logger.error ex.backtrace.join("\n\t")
39
+ end
40
+ end
41
+
42
+ raise ex
43
+ end
44
+
45
+ def deliver?
46
+ true
47
+ end
48
+ end
49
+
50
+ class MailJob < Que::Job
51
+ def run(mailer_class, method_name, *args)
52
+ mailer = Kernel.const_get(mailer_class)
53
+ mailer.send(method_name, *args).deliver!
54
+ end
55
+ end
56
+
57
+ class MessageDecoy
58
+ delegate :to_s, :to => :actual_message
59
+
60
+ def initialize(mailer_class, method_name, *args)
61
+ @mailer_class = mailer_class
62
+ @method_name = method_name
63
+ *@args = *args
64
+ @actual_message
65
+ end
66
+
67
+ def current_env
68
+ if defined?(Rails)
69
+ ::Que::Mailer.current_env || ::Rails.env
70
+ else
71
+ ::Que::Mailer.current_env
72
+ end
73
+ end
74
+
75
+ def actual_message
76
+ @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
77
+ end
78
+
79
+ def deliver
80
+ if @mailer_class.deliver?
81
+ MailJob.queue(@mailer_class.to_s, @method_name, *@args)
82
+ end
83
+ end
84
+
85
+ def deliver_at(time)
86
+ if @mailer_class.deliver?
87
+ MailJob.queue(@mailer_class.to_s, @method_name, *@args, {:run_at => time})
88
+ end
89
+ end
90
+
91
+ def deliver_in(time)
92
+ deliver_at(time.from_now)
93
+ end
94
+
95
+ def deliver!
96
+ actual_message.deliver
97
+ end
98
+
99
+ def method_missing(method_name, *args)
100
+ actual_message.send(method_name, *args)
101
+ end
102
+
103
+ def logger
104
+ @mailer_class.logger
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,5 @@
1
+ module Que
2
+ module Mailer
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/que_mailer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Robert Prehn"]
6
+ gem.email = ["robert@revelry.co"]
7
+ gem.description = %q{Asynchronous mail delivery using que}
8
+ gem.summary = %q{Using Que to delivery mail asynchronously}
9
+ gem.homepage = "http://github.com/prehnra/que-mailer"
10
+ gem.license = "MIT"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "que_mailer"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Que::Mailer::VERSION
18
+
19
+ gem.add_dependency("rails", ">= 4.0")
20
+ gem.add_dependency("activesupport", ">= 4.0")
21
+ gem.add_dependency("actionmailer", ">= 4.0")
22
+ gem.add_dependency("que", "~> 0.5")
23
+ gem.add_dependency('pg', "~> 0")
24
+ gem.add_development_dependency('rake')
25
+ gem.add_development_dependency('rspec')
26
+ gem.add_development_dependency('rspec-rails')
27
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib', 'que_mailer'))
Binary file
@@ -0,0 +1,16 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
4
+ gem 'rails', '4.0.0'
5
+ gem 'rspec-rails'
6
+ gem 'pg'
7
+ gem 'que'
8
+ gem 'que_mailer', :path => "../../"
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,9 @@
1
+ class TestMailer < ActionMailer::Base
2
+ include Que::Mailer
3
+ default from: 'from@example.com'
4
+
5
+ def test_message(*args)
6
+ @args = *args
7
+ mail(to: "joe@example.com", subject: "A Test Message")
8
+ end
9
+ end
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ class QueJob < ActiveRecord::Base
2
+
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ It worked!
2
+
3
+ <%= @args.to_s.html_safe %>
@@ -0,0 +1,3 @@
1
+ It worked!
2
+
3
+ <%= @args %>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ # Require the gems listed in Gemfile, including any gems
6
+ # you've limited to :test, :development, or :production.
7
+ Bundler.require(:default, Rails.env)
8
+
9
+ module Dummy
10
+ class Application < Rails::Application
11
+ # Settings in config/environments/* take precedence over those specified here.
12
+ # Application configuration should go into files in config/initializers
13
+ # -- all .rb files in that directory are automatically loaded.
14
+
15
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
16
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
17
+ # config.time_zone = 'Central Time (US & Canada)'
18
+
19
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
20
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
21
+ # config.i18n.default_locale = :de
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,5 @@
1
+ test:
2
+ adapter: postgresql
3
+ database: que_mailer_test
4
+ pool: 5
5
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the Rails application.
5
+ Dummy::Application.initialize!
@@ -0,0 +1,37 @@
1
+ Dummy::Application.configure do
2
+ config.que.mode = :async
3
+ # Settings specified here will take precedence over those in config/application.rb.
4
+
5
+ # The test environment is used exclusively to run your application's
6
+ # test suite. You never need to work with it otherwise. Remember that
7
+ # your test database is "scratch space" for the test suite and is wiped
8
+ # and recreated between test runs. Don't rely on the data there!
9
+ config.cache_classes = true
10
+
11
+ # Do not eager load code on boot. This avoids loading your whole application
12
+ # just for the purpose of running a single test. If you are using a tool that
13
+ # preloads Rails for running tests, you may have to set it to true.
14
+ config.eager_load = false
15
+
16
+ # Configure static asset server for tests with Cache-Control for performance.
17
+ config.serve_static_assets = true
18
+ config.static_cache_control = "public, max-age=3600"
19
+
20
+ # Show full error reports and disable caching.
21
+ config.consider_all_requests_local = true
22
+ config.action_controller.perform_caching = false
23
+
24
+ # Raise exceptions instead of rendering exception templates.
25
+ config.action_dispatch.show_exceptions = false
26
+
27
+ # Disable request forgery protection in test environment.
28
+ config.action_controller.allow_forgery_protection = false
29
+
30
+ # Tell Action Mailer not to deliver emails to the real world.
31
+ # The :test delivery method accumulates sent emails in the
32
+ # ActionMailer::Base.deliveries array.
33
+ config.action_mailer.delivery_method = :test
34
+
35
+ # Print deprecation notices to the stderr.
36
+ config.active_support.deprecation = :stderr
37
+ end
@@ -0,0 +1 @@
1
+ require 'que'
@@ -0,0 +1 @@
1
+ require 'que_mailer'
@@ -0,0 +1,23 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # To learn more, please read the Rails Internationalization guide
20
+ # available at http://guides.rubyonrails.org/i18n.html.
21
+
22
+ en:
23
+ hello: "Hello world"
@@ -0,0 +1,56 @@
1
+ Dummy::Application.routes.draw do
2
+ # The priority is based upon order of creation: first created -> highest priority.
3
+ # See how all your routes lay out with "rake routes".
4
+
5
+ # You can have the root of your site routed with "root"
6
+ # root 'welcome#index'
7
+
8
+ # Example of regular route:
9
+ # get 'products/:id' => 'catalog#view'
10
+
11
+ # Example of named route that can be invoked with purchase_url(id: product.id)
12
+ # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
13
+
14
+ # Example resource route (maps HTTP verbs to controller actions automatically):
15
+ # resources :products
16
+
17
+ # Example resource route with options:
18
+ # resources :products do
19
+ # member do
20
+ # get 'short'
21
+ # post 'toggle'
22
+ # end
23
+ #
24
+ # collection do
25
+ # get 'sold'
26
+ # end
27
+ # end
28
+
29
+ # Example resource route with sub-resources:
30
+ # resources :products do
31
+ # resources :comments, :sales
32
+ # resource :seller
33
+ # end
34
+
35
+ # Example resource route with more complex sub-resources:
36
+ # resources :products do
37
+ # resources :comments
38
+ # resources :sales do
39
+ # get 'recent', on: :collection
40
+ # end
41
+ # end
42
+
43
+ # Example resource route with concerns:
44
+ # concern :toggleable do
45
+ # post 'toggle'
46
+ # end
47
+ # resources :posts, concerns: :toggleable
48
+ # resources :photos, concerns: :toggleable
49
+
50
+ # Example resource route within a namespace:
51
+ # namespace :admin do
52
+ # # Directs /admin/products/* to Admin::ProductsController
53
+ # # (app/controllers/admin/products_controller.rb)
54
+ # resources :products
55
+ # end
56
+ end
@@ -0,0 +1,11 @@
1
+ class AddQue < ActiveRecord::Migration
2
+ def self.up
3
+ # The current version as of this migration's creation.
4
+ Que.migrate! :version => 2
5
+ end
6
+
7
+ def self.down
8
+ # Completely removes Que's job queue.
9
+ Que.migrate! :version => 0
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended that you check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(version: 20140128035854) do
15
+
16
+ # These are extensions that must be enabled in order to support this database
17
+ enable_extension "plpgsql"
18
+
19
+ create_table "que_jobs", id: false, force: true do |t|
20
+ t.integer "priority", default: 100, null: false
21
+ t.datetime "run_at", null: false
22
+ t.integer "job_id", limit: 8, null: false
23
+ t.text "job_class", null: false
24
+ t.json "args", default: [], null: false
25
+ t.integer "error_count", default: 0, null: false
26
+ t.text "last_error"
27
+ end
28
+
29
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ # Set up a dummy logger.
4
+ Que.logger = $logger = Object.new
5
+ $logger_mutex = Mutex.new # Protect against rare errors on Rubinius/JRuby.
6
+
7
+ def $logger.messages
8
+ @messages ||= []
9
+ end
10
+
11
+ def $logger.method_missing(m, message)
12
+ $logger_mutex.synchronize { messages << message }
13
+ end
14
+
15
+ # Helper for testing threaded code.
16
+ QUE_TEST_TIMEOUT ||= 2
17
+ def sleep_until(timeout = QUE_TEST_TIMEOUT)
18
+ deadline = Time.now + timeout
19
+ loop do
20
+ break if yield
21
+ raise "Thing never happened!" if Time.now > deadline
22
+ sleep 0.01
23
+ end
24
+ end
25
+
26
+ describe Que::Mailer do
27
+ it "should send mail" do
28
+ QueJob.delete_all
29
+ ActionMailer::Base.deliveries = []
30
+ QueJob.count.should be 0
31
+
32
+ ActionMailer::Base.deliveries.length.should be 0
33
+ TestMailer.test_message.deliver
34
+ QueJob.count.should be 1
35
+
36
+ TestMailer::MailJob.work
37
+
38
+ Que::Worker.workers.each do |worker|
39
+ worker.wake!
40
+ end
41
+ sleep_until { Que::Worker.workers.all?(&:sleeping?) }
42
+ QueJob.count.should be 0
43
+ ActionMailer::Base.deliveries.length.should be 1
44
+ QueJob.count.should be 0
45
+ end
46
+
47
+ it "should send delayed mail" do
48
+ QueJob.delete_all
49
+ ActionMailer::Base.deliveries = []
50
+ QueJob.count.should be 0
51
+
52
+ ActionMailer::Base.deliveries.length.should be 0
53
+ TestMailer.test_message.deliver_in(60)
54
+ QueJob.count.should be 1
55
+ ActionMailer::Base.deliveries.length.should be 0
56
+ QueJob.first.run_at.should be_within(3).of Time.now + 60
57
+ QueJob.delete_all
58
+ end
59
+
60
+ it "should send scheduled mail" do
61
+ QueJob.delete_all
62
+ ActionMailer::Base.deliveries = []
63
+ QueJob.count.should be 0
64
+
65
+ ActionMailer::Base.deliveries.length.should be 0
66
+ TestMailer.test_message.deliver_at(Time.now+60)
67
+ QueJob.count.should be 1
68
+ ActionMailer::Base.deliveries.length.should be 0
69
+ QueJob.first.run_at.should be_within(3).of Time.now + 60
70
+ QueJob.delete_all
71
+ end
72
+
73
+ it "should work in async mode" do
74
+ Rails.application.config.mode = :async
75
+ QueJob.delete_all
76
+ ActionMailer::Base.deliveries = []
77
+ QueJob.count.should be 0
78
+ ActionMailer::Base.deliveries.length.should be 0
79
+
80
+ TestMailer.test_message.deliver
81
+ QueJob.count.should be 1
82
+
83
+ #Wake all the workers up, then wait until they all sleep
84
+ Que::Worker.workers.each do |worker|
85
+ worker.wake!
86
+ end
87
+ sleep_until { Que::Worker.workers.all?(&:sleeping?) }
88
+
89
+ QueJob.count.should be 0
90
+
91
+ ActionMailer::Base.deliveries.length.should be 1
92
+ Rails.application.config.mode = :sync
93
+ end
94
+
95
+ it "should accept arguments" do
96
+ QueJob.delete_all
97
+ ActionMailer::Base.deliveries = []
98
+ QueJob.count.should be 0
99
+
100
+ secret1 = (0..10).map {|x| ('a'..'z').to_a[rand(26)]}.join
101
+ secret2 = (0..10).map {|x| ('a'..'z').to_a[rand(26)]}.join
102
+
103
+ ActionMailer::Base.deliveries.length.should be 0
104
+ TestMailer.test_message({:arg1=>secret1, :arg2=>secret2}).deliver
105
+ QueJob.count.should be 1
106
+
107
+ TestMailer::MailJob.work
108
+
109
+ Que::Worker.workers.each do |worker|
110
+ worker.wake!
111
+ end
112
+ sleep_until { Que::Worker.workers.all?(&:sleeping?) }
113
+ QueJob.count.should be 0
114
+ ActionMailer::Base.deliveries.length.should be 1
115
+ ActionMailer::Base.deliveries[0].text_part.body.should include(secret1)
116
+ ActionMailer::Base.deliveries[0].text_part.body.should include(secret2)
117
+ QueJob.count.should be 0
118
+ end
119
+ end
@@ -0,0 +1,42 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+ require File.expand_path("../../config/environment", __FILE__)
4
+ require 'rspec/rails'
5
+ require 'rspec/autorun'
6
+
7
+ # Requires supporting ruby files with custom matchers and macros, etc,
8
+ # in spec/support/ and its subdirectories.
9
+ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
10
+
11
+ # Checks for pending migrations before tests are run.
12
+ # If you are not using ActiveRecord, you can remove this line.
13
+ ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
14
+
15
+ RSpec.configure do |config|
16
+ # ## Mock Framework
17
+ #
18
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
19
+ #
20
+ # config.mock_with :mocha
21
+ # config.mock_with :flexmock
22
+ # config.mock_with :rr
23
+
24
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
25
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
26
+
27
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
28
+ # examples within a transaction, remove the following line or assign false
29
+ # instead of true.
30
+ config.use_transactional_fixtures = false
31
+
32
+ # If true, the base class of anonymous controllers will be inferred
33
+ # automatically. This will be the default behavior in future versions of
34
+ # rspec-rails.
35
+ config.infer_base_class_for_anonymous_controllers = false
36
+
37
+ # Run specs in random order to surface order dependencies. If you find an
38
+ # order dependency and want to debug it, you can fix the order by providing
39
+ # the seed, which is printed after each run.
40
+ # --seed 1234
41
+ config.order = "random"
42
+ end
@@ -0,0 +1,42 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+ require File.expand_path("../dummy/config/environment", __FILE__)
4
+ require 'rspec/rails'
5
+ require 'rspec/autorun'
6
+
7
+ # Requires supporting ruby files with custom matchers and macros, etc,
8
+ # in spec/support/ and its subdirectories.
9
+ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
10
+
11
+ # Checks for pending migrations before tests are run.
12
+ # If you are not using ActiveRecord, you can remove this line.
13
+ ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
14
+
15
+ RSpec.configure do |config|
16
+ # ## Mock Framework
17
+ #
18
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
19
+ #
20
+ # config.mock_with :mocha
21
+ # config.mock_with :flexmock
22
+ # config.mock_with :rr
23
+
24
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
25
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
26
+
27
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
28
+ # examples within a transaction, remove the following line or assign false
29
+ # instead of true.
30
+ config.use_transactional_fixtures = false
31
+
32
+ # If true, the base class of anonymous controllers will be inferred
33
+ # automatically. This will be the default behavior in future versions of
34
+ # rspec-rails.
35
+ config.infer_base_class_for_anonymous_controllers = false
36
+
37
+ # Run specs in random order to surface order dependencies. If you find an
38
+ # order dependency and want to debug it, you can fix the order by providing
39
+ # the seed, which is printed after each run.
40
+ # --seed 1234
41
+ config.order = "random"
42
+ end
metadata ADDED
@@ -0,0 +1,226 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: que_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Robert Prehn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionmailer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: que
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Asynchronous mail delivery using que
126
+ email:
127
+ - robert@revelry.co
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - Gemfile
134
+ - LICENSE
135
+ - Readme.md
136
+ - lib/que_mailer.rb
137
+ - lib/que_mailer/version.rb
138
+ - que-mailer.gemspec
139
+ - rails/init.rb
140
+ - spec/dummy/.DS_Store
141
+ - spec/dummy/.gitignore
142
+ - spec/dummy/Gemfile
143
+ - spec/dummy/Rakefile
144
+ - spec/dummy/app/mailers/.keep
145
+ - spec/dummy/app/mailers/test_mailer.rb
146
+ - spec/dummy/app/models/.keep
147
+ - spec/dummy/app/models/concerns/.keep
148
+ - spec/dummy/app/models/que_job.rb
149
+ - spec/dummy/app/views/layouts/application.html.erb
150
+ - spec/dummy/app/views/test_mailer/test_message.html.erb
151
+ - spec/dummy/app/views/test_mailer/test_message.text.erb
152
+ - spec/dummy/bin/bundle
153
+ - spec/dummy/bin/rails
154
+ - spec/dummy/bin/rake
155
+ - spec/dummy/config.ru
156
+ - spec/dummy/config/application.rb
157
+ - spec/dummy/config/boot.rb
158
+ - spec/dummy/config/database.yml
159
+ - spec/dummy/config/environment.rb
160
+ - spec/dummy/config/environments/.DS_Store
161
+ - spec/dummy/config/environments/test.rb
162
+ - spec/dummy/config/initializers/que.rb
163
+ - spec/dummy/config/initializers/que_mailer.rb
164
+ - spec/dummy/config/locales/en.yml
165
+ - spec/dummy/config/routes.rb
166
+ - spec/dummy/db/migrate/20140128035854_add_que.rb
167
+ - spec/dummy/db/schema.rb
168
+ - spec/dummy/spec/mailers/test_mailer_spec.rb
169
+ - spec/dummy/spec/spec_helper.rb
170
+ - spec/spec_helper.rb
171
+ homepage: http://github.com/prehnra/que-mailer
172
+ licenses:
173
+ - MIT
174
+ metadata: {}
175
+ post_install_message:
176
+ rdoc_options: []
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - '>='
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - '>='
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubyforge_project:
191
+ rubygems_version: 2.2.1
192
+ signing_key:
193
+ specification_version: 4
194
+ summary: Using Que to delivery mail asynchronously
195
+ test_files:
196
+ - spec/dummy/.DS_Store
197
+ - spec/dummy/.gitignore
198
+ - spec/dummy/Gemfile
199
+ - spec/dummy/Rakefile
200
+ - spec/dummy/app/mailers/.keep
201
+ - spec/dummy/app/mailers/test_mailer.rb
202
+ - spec/dummy/app/models/.keep
203
+ - spec/dummy/app/models/concerns/.keep
204
+ - spec/dummy/app/models/que_job.rb
205
+ - spec/dummy/app/views/layouts/application.html.erb
206
+ - spec/dummy/app/views/test_mailer/test_message.html.erb
207
+ - spec/dummy/app/views/test_mailer/test_message.text.erb
208
+ - spec/dummy/bin/bundle
209
+ - spec/dummy/bin/rails
210
+ - spec/dummy/bin/rake
211
+ - spec/dummy/config.ru
212
+ - spec/dummy/config/application.rb
213
+ - spec/dummy/config/boot.rb
214
+ - spec/dummy/config/database.yml
215
+ - spec/dummy/config/environment.rb
216
+ - spec/dummy/config/environments/.DS_Store
217
+ - spec/dummy/config/environments/test.rb
218
+ - spec/dummy/config/initializers/que.rb
219
+ - spec/dummy/config/initializers/que_mailer.rb
220
+ - spec/dummy/config/locales/en.yml
221
+ - spec/dummy/config/routes.rb
222
+ - spec/dummy/db/migrate/20140128035854_add_que.rb
223
+ - spec/dummy/db/schema.rb
224
+ - spec/dummy/spec/mailers/test_mailer_spec.rb
225
+ - spec/dummy/spec/spec_helper.rb
226
+ - spec/spec_helper.rb