ResqueMailer 技术文档
1. 安装指南
1.1 安装 Gem
首先,您需要安装 resque_mailer 这个 Gem。您可以通过以下命令直接安装:
gem install resque_mailer
如果您使用 Bundler 来管理依赖,可以在 Gemfile 中添加以下内容:
gem 'resque' # 或者使用兼容的替代品/分支
gem 'resque_mailer'
然后运行 bundle install 来安装依赖。
1.2 配置 Resque
确保您已经安装并配置了 Resque,因为 resque_mailer 依赖于 Resque 来处理异步邮件发送任务。
2. 项目的使用说明
2.1 在 ActionMailer 中使用 Resque::Mailer
在您的 ActionMailer 子类中,包含 Resque::Mailer 模块:
class MyMailer < ActionMailer::Base
include Resque::Mailer
end
2.2 异步发送邮件
当您调用 MyMailer.subject_email(params).deliver 时,邮件发送任务会被添加到 Resque 的任务队列中。Resque 的 worker 会处理这些任务并发送邮件。默认情况下,任务会被添加到名为 mailer 的队列中。
启动 Resque worker 时,确保指定 mailer 队列:
QUEUE=mailer rake environment resque:work
2.3 同步发送邮件
如果您需要同步发送邮件,可以使用 deliver! 方法:
MyMailer.subject_email(params).deliver!
2.4 自定义队列名称
如果您希望使用不同的默认队列名称,可以在初始化文件中设置:
# config/initializers/resque_mailer.rb
Resque::Mailer.default_queue_name = 'application_specific_mailer'
启动 worker 时,使用新的队列名称:
QUEUE=application_specific_mailer rake environment resque:work
2.5 错误处理
您可以通过设置 error_handler 来处理邮件发送过程中出现的错误:
Resque::Mailer.error_handler = lambda { |mailer, message, error, action, args|
if error.is_a?(Resque::TermException)
Resque.enqueue(mailer, action, *args)
else
raise error
end
}
3. 项目API使用文档
3.1 自定义参数序列化器
默认情况下,传递给 mailer 的参数会直接传递给 Resque。如果您希望传递数据库对象,可以编写自定义的序列化器:
class MySerializer
def self.serialize(*args)
# 自定义序列化逻辑
end
def self.deserialize(data)
# 自定义反序列化逻辑
end
end
Resque::Mailer.argument_serializer = MySerializer
3.2 使用 Active Record 序列化器
resque_mailer 提供了 Active Record 序列化器,允许您直接传递 AR 模型作为参数:
Resque::Mailer.argument_serializer = Resque::Mailer::Serializers::ActiveRecordSerializer
3.3 使用 Resque Scheduler
如果您安装了 resque-scheduler,可以使用 deliver_at 和 deliver_in 方法来安排邮件在未来某个时间发送:
# 在 2014 年 12 月 25 日发送
MyMailer.reminder_email(params).deliver_at(Time.parse('2014-12-25'))
# 在 7 天后发送
MyMailer.reminder_email(params).deliver_in(7.days)
# 取消计划发送
MyMailer.reminder_email(params).unschedule_delivery
4. 项目安装方式
4.1 使用 Bundler
在 Gemfile 中添加以下内容:
gem 'resque'
gem 'resque_mailer'
然后运行 bundle install。
4.2 直接安装 Gem
使用以下命令直接安装 resque_mailer:
gem install resque_mailer
4.3 配置环境
在测试环境中,您可能不希望实际发送邮件。可以通过以下配置来排除特定环境:
# config/initializers/resque_mailer.rb
Resque::Mailer.excluded_environments = [:test, :cucumber]
如果您在非 Rails 项目中使用 resque_mailer,可以定义 current_env:
Resque::Mailer.current_env = :production
5. 贡献与反馈
如果您希望为项目贡献代码,请遵循以下步骤:
- Fork 项目。
- 添加新功能或修复 bug。
- 添加测试。
- 提交 pull request。
感谢您的贡献!
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0228
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0148
uni-appA cross-platform framework using Vue.jsJavaScript010
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook04