All Projects → zapnap → Resque_mailer

zapnap / Resque_mailer

Licence: mit
Rails plugin for sending asynchronous email with ActionMailer and Resque

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Resque mailer

Autolab
Course management service that enables auto-graded programming assignments.
Stars: ✭ 528 (-13.73%)
Mutual labels:  rails
Social Share Button
Helper for add social share feature in your Rails app. Twitter, Facebook, Weibo, Douban ...
Stars: ✭ 567 (-7.35%)
Mutual labels:  rails
Gdpr rails
Rails Engine for the GDPR compliance
Stars: ✭ 580 (-5.23%)
Mutual labels:  rails
Annict
The platform for anime addicts built with Rails and Stimulus.js.
Stars: ✭ 542 (-11.44%)
Mutual labels:  rails
Gitlabhq
GitLab CE Mirror | Please open new issues in our issue tracker on GitLab.com
Stars: ✭ 22,798 (+3625.16%)
Mutual labels:  rails
Administrate
A Rails engine that helps you put together a super-flexible admin dashboard.
Stars: ✭ 5,250 (+757.84%)
Mutual labels:  rails
Rails email preview
Preview and edit app mailer templates in Rails.
Stars: ✭ 524 (-14.38%)
Mutual labels:  rails
Arbre
An Object Oriented DOM Tree in Ruby
Stars: ✭ 610 (-0.33%)
Mutual labels:  rails
Prosopite
🔍 Rails N+1 queries auto-detection with zero false positives / false negatives
Stars: ✭ 555 (-9.31%)
Mutual labels:  rails
Bluedoc
An open-source document management tool for enterprise self host.
Stars: ✭ 579 (-5.39%)
Mutual labels:  rails
Stateful enum
A very simple state machine plugin built on top of ActiveRecord::Enum
Stars: ✭ 546 (-10.78%)
Mutual labels:  rails
Devise
Flexible authentication solution for Rails with Warden.
Stars: ✭ 22,088 (+3509.15%)
Mutual labels:  rails
Activeadmin addons
Extends ActiveAdmin to enable a set of great optional UX improving add-ons
Stars: ✭ 574 (-6.21%)
Mutual labels:  rails
Serviceworker Rails
Use Service Worker with the Rails asset pipeline
Stars: ✭ 535 (-12.58%)
Mutual labels:  rails
Github Ds
A collection of Ruby libraries for working with SQL on top of ActiveRecord's connection
Stars: ✭ 597 (-2.45%)
Mutual labels:  rails
Activerecord Multi Tenant
Rails/ActiveRecord support for distributed multi-tenant databases like Postgres+Citus
Stars: ✭ 526 (-14.05%)
Mutual labels:  rails
Webpacker
Use Webpack to manage app-like JavaScript modules in Rails
Stars: ✭ 5,282 (+763.07%)
Mutual labels:  rails
Mailer
The Mailer component helps sending emails
Stars: ✭ 609 (-0.49%)
Mutual labels:  mailer
Charting Library Examples
Examples of Charting Library integrations with other libraries, frameworks and data transports
Stars: ✭ 608 (-0.65%)
Mutual labels:  rails
Angular Rails Templates
Use your angular templates with rails' asset pipeline
Stars: ✭ 576 (-5.88%)
Mutual labels:  rails

ResqueMailer

Gem Version Build Status

A gem plugin which allows messages prepared by ActionMailer to be delivered asynchronously. Assumes you're using Resque for your background jobs.

Note that recent (2.0+) versions of Resque::Mailer only work with Rails 3.x or later. For a version compatible with Rails 2, specify v1.x in your Gemfile.

Installation

Install the gem:

gem install resque_mailer

If you're using Bundler to manage your dependencies, you should add it to your Gemfile:

gem 'resque' # or a compatible alternative / fork
gem 'resque_mailer'

Usage

Include Resque::Mailer in your ActionMailer subclass(es) like this:

class MyMailer < ActionMailer::Base
  include Resque::Mailer
end

Now, when MyMailer.subject_email(params).deliver is called, an entry will be created in the job queue. Your Resque workers will be able to deliver this message for you. The queue we're using is imaginatively named mailer, so just make sure your workers know about it and are loading your environment:

QUEUE=mailer rake environment resque:work

Note that you can still have mail delivered synchronously by using the bang method variant:

MyMailer.subject_email(params).deliver!

Oh, by the way. Don't forget that your async mailer jobs will be processed by a separate worker. This means that you should resist the temptation to pass database-backed objects as parameters in your mailer and instead pass record identifiers. Then, in your delivery method, you can look up the record from the id and use it as needed. If you'd like, you can write your own serializer to automate such things; see the section on serializers below.

If you want to set a different default queue name for your mailer, you can change the default_queue_name property like so:

# config/initializers/resque_mailer.rb
Resque::Mailer.default_queue_name = 'application_specific_mailer'

This is useful when you are running more than one application using resque_mailer in a shared environment. You will need to use the new queue name when starting your workers.

QUEUE=application_specific_mailer rake environment resque:work

Custom handling of errors that arise when sending a message is possible by assigning a lambda to the error_handler attribute. There are two supported lambdas for backwards compatiability.

The first lamba will be deprecated in a future release:

Resque::Mailer.error_handler = lambda { |mailer, message, error|
  # some custom error handling code here in which you optionally re-raise the error
}

The new lamba contains two other arguments, action and args, which allows mailers to be requeued on failure:

Resque::Mailer.error_handler = lambda { |mailer, message, error, action, args|
  # Necessary to re-enqueue jobs that receieve the SIGTERM signal
  if error.is_a?(Resque::TermException)
    Resque.enqueue(mailer, action, *args)
  else
    raise error
  end
}

Resque::Mailer as a Project Default

If you have a variety of mailers in your application and want all of them to use Resque::Mailer by default, you can subclass ActionMailer::Base and have your other mailers inherit from an AsyncMailer:

# config/initializers/resque_mailer.rb
class AsyncMailer < ActionMailer::Base
  include Resque::Mailer
end

# app/mailers/example_mailer.rb
class ExampleMailer < AsyncMailer
  def say_hello(user_id)
    # ...
  end
end

Writing an Argument Serializer

By default, the arguments you pass to your mailer are passed as-is to Resque. This means you cannot pass things like database-backed objects. If you'd like to write your own serializer to enable such things, simply write a class that implements the class methods self.serialize(*args) and self.deserialize(data) and set Resque::Mailer.argument_serializer = YourSerializerClass in your resque_mailer initializer.

There's also Active Record serializer which allows you to pass AR models directly as arguments. To use it just do: Resque::Mailer.argument_serializer = Resque::Mailer::Serializers::ActiveRecordSerializer

Using with Resque Scheduler

If resque-scheduler is installed, two extra methods will be available: deliver_at and deliver_in. These will enqueue mail for delivery at a specified time in the future.

# Delivers on the 25th of December, 2014
MyMailer.reminder_email(params).deliver_at(Time.parse('2014-12-25'))

# Delivers in 7 days
MyMailer.reminder_email(params).deliver_in(7.days)

# Unschedule delivery
MyMailer.reminder_email(params).unschedule_delivery

Testing

You don't want to be sending actual emails in the test environment, so you can configure the environments that should be excluded like so:

# config/initializers/resque_mailer.rb
Resque::Mailer.excluded_environments = [:test, :cucumber]

Note: Define current_env if using Resque::Mailer in a non-Rails project:

Resque::Mailer.current_env = :production

Note on Patches / Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Credits

Developed by Nick Plante with help from a number of contributors.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].