将邮件发送放到后台进程中

This commit is contained in:
guange 2015-03-31 11:42:36 +08:00
parent efdfa67c64
commit 02e83ea776
29 changed files with 106 additions and 68 deletions

View File

@ -8,6 +8,8 @@ unless RUBY_PLATFORM =~ /w32/
gem 'zip-zip'
end
gem 'delayed_job_active_record'#, :group => :production
gem 'daemons'
gem 'grape', '~> 0.9.0'
gem 'grape-entity'
gem 'seems_rateable', '~> 1.0.13'

View File

@ -88,9 +88,7 @@ class AccountController < ApplicationController
# create a new token for password recovery
token = Token.new(:user => user, :action => "recovery")
if token.save
Thread.new do
Mailer.lost_password(token).deliver
end
Mailer.run.lost_password(token)
flash[:notice] = l(:notice_account_lost_email_sent)
redirect_to signin_url
return
@ -228,7 +226,7 @@ class AccountController < ApplicationController
user = User.find(params[:user]) if params[:user]
token = Token.new(:user => user, :action => "register")
if token.save
Mailer.register(token).deliver
Mailer.run.register(token)
else
yield if block_given?
@ -366,7 +364,7 @@ class AccountController < ApplicationController
token = Token.new(:user => user, :action => "register")
if user.save and token.save
UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0)
Mailer.register(token).deliver
Mailer.run.register(token)
flash[:notice] = l(:notice_account_register_done)
@ -401,7 +399,7 @@ class AccountController < ApplicationController
if user.save
UserStatus.create(:user_id => user.id ,:changsets_count => 0, :watchers_count => 0)
# Sends an email to the administrators
Mailer.account_activation_request(user).deliver
Mailer.run.account_activation_request(user)
account_pending
else
yield if block_given?

View File

@ -13,7 +13,7 @@ class AppliedProjectController < ApplicationController
@applieds = AppliedProject.where("user_id = ? and project_id = ?", params[:user_id],params[:project_id])
if @applieds.count == 0
appliedproject = AppliedProject.create(:user_id => params[:user_id], :project_id => params[:project_id])
Mailer.applied_project(appliedproject).deliver
Mailer.run.applied_project(appliedproject)
@status = 2
else
@status = 1
@ -31,7 +31,7 @@ class AppliedProjectController < ApplicationController
@applieds = AppliedProject.where("user_id = ? and project_id = ?", params[:user_id],params[:project_id])
if @applieds.count == 0
appliedproject = AppliedProject.create(:user_id => params[:user_id], :project_id => params[:project_id])
Mailer.applied_project(appliedproject).deliver
Mailer.run.applied_project(appliedproject)
end
#redirect_to project_path(params[:project_id])

View File

@ -110,7 +110,7 @@ class DocumentsController < ApplicationController
render_attachment_warning_if_needed(@document)
if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added')
Mailer.attachments_added(attachments[:files]).deliver
Mailer.run.attachments_added(attachments[:files])
end
redirect_to document_url(@document)
end

View File

@ -241,7 +241,7 @@ class FilesController < ApplicationController
render_attachment_warning_if_needed(container)
if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added')
Mailer.attachments_added(attachments[:files]).deliver
Mailer.run.attachments_added(attachments[:files])
end
# TODO: 临时用 nyan
@ -270,7 +270,7 @@ class FilesController < ApplicationController
attachments = Attachment.attach_filesex(@course, params[:attachments], params[:attachment_type])
if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added')
Mailer.attachments_added(attachments[:files]).deliver
Mailer.run.attachments_added(attachments[:files])
end
# TODO: 临时用 nyan

View File

@ -336,7 +336,7 @@ class ProjectsController < ApplicationController
def send_mail_to_member
if !params[:mail].blank? && User.find_by_mail(params[:mail].to_s).nil?
email = params[:mail]
Mailer.send_invite_in_project(email, @project, User.current).deliver
Mailer.run.send_invite_in_project(email, @project, User.current)
@is_zhuce =false
flash[:notice] = l(:notice_email_sent, :value => email)
else

View File

@ -553,7 +553,7 @@ class UsersController < ApplicationController
@user.pref.save
@user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information]
Mailer.run.account_information(@user, params[:user][:password]) if params[:send_information]
respond_to do |format|
format.html {
@ -620,9 +620,9 @@ class UsersController < ApplicationController
@user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
if was_activated
Mailer.account_activated(@user).deliver
Mailer.run.account_activated(@user)
elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
Mailer.account_information(@user, params[:user][:password]).deliver
Mailer.run.account_information(@user, params[:user][:password])
end
respond_to do |format|

View File

@ -23,7 +23,7 @@ module AccountHelper
token = Token.new(:user => user, :action => "register")
if user.save and token.save
UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0)
Mailer.register(token).deliver
Mailer.run.register(token)
#flash[:notice] = l(:notice_account_register_done)
#render action: 'email_valid', locals: {:mail => user.mail}
else
@ -51,7 +51,7 @@ module AccountHelper
if user.save
UserStatus.create(:user_id => user.id ,:changsets_count => 0, :watchers_count => 0)
# Sends an email to the administrators
Mailer.account_activation_request(user).deliver
Mailer.run.account_activation_request(user)
#account_pending
else
yield if block_given?

View File

@ -18,10 +18,7 @@
class CommentObserver < ActiveRecord::Observer
def after_create(comment)
if comment.commented.is_a?(News) && Setting.notified_events.include?('news_comment_added')
##by senluo
thread3=Thread.new do
Mailer.news_comment_added(comment).deliver
end
Mailer.run.news_comment_added(comment)
end
end
end

View File

@ -17,9 +17,6 @@
class DocumentObserver < ActiveRecord::Observer
def after_create(document)
##by senluo
thread2=Thread.new do
Mailer.document_added(document).deliver if Setting.notified_events.include?('document_added')
end
Mailer.run.document_added(document) if Setting.notified_events.include?('document_added')
end
end

View File

@ -36,9 +36,7 @@ class Forum < ActiveRecord::Base
end
def send_email
Thread.start do
Mailer.forum_add(self).deliver if Setting.notified_events.include?('forum_add')
end
Mailer.run.forum_add(self) if Setting.notified_events.include?('forum_add')
end
# Updates topic_count, memo_count and last_memo_id attributes for +board_id+
def self.reset_counters!(forum_id)

View File

@ -18,13 +18,11 @@
class IssueObserver < ActiveRecord::Observer
def after_create(issue)
Thread.start do
# 将跟踪者与本项目的其他成员都设为收件方,并去重,不在进行抄送,
recipients = issue.recipients - issue.watcher_recipients + issue.watcher_recipients
recipients.each do |rec|
Mailer.issue_add(issue,rec).deliver if Setting.notified_events.include?('issue_added')
Mailer.run.issue_add(issue,rec) if Setting.notified_events.include?('issue_added')
end
end
end
end

View File

@ -23,14 +23,12 @@ class JournalObserver < ActiveRecord::Observer
(Setting.notified_events.include?('issue_status_updated') && journal.new_status.present?) ||
(Setting.notified_events.include?('issue_priority_updated') && journal.new_value_for('priority_id').present?)
)
Thread.start do
# 将跟踪者与本项目的其他成员都设为收件方,并去重,不在进行抄送,
recipients = journal.recipients - journal.watcher_recipients + journal.watcher_recipients
recipients.each do |rec|
Mailer.issue_edit(journal,rec).deliver
Mailer.run.issue_edit(journal,rec)
end
end
end
end
end

View File

@ -1,9 +1,7 @@
# Added by young
class JournalsForMessageObserver < ActiveRecord::Observer
def after_create(journals_for_message)
thread1 = Thread.start do
Mailer.journals_for_message_add(User.current, journals_for_message).deliver
end
Mailer.run.journals_for_message_add(User.current, journals_for_message)
end
end

View File

@ -101,7 +101,7 @@ class MailHandler < ActionMailer::Base
end
add_user_to_group(@@handler_options[:default_group])
unless @@handler_options[:no_account_notice]
Mailer.account_information(@user, @user.password).deliver
Mailer.run.account_information(@user, @user.password)
end
else
if logger && logger.error

View File

@ -27,6 +27,24 @@ class Mailer < ActionMailer::Base
{ :host => Setting.host_name, :protocol => Setting.protocol }
end
class MailerProxy
def initialize(cls)
@target = cls
end
def method_missing(name, *args, &block)
if Setting.delayjob_enabled && Object.const_defined?('Delayed::Worker')
@target.delay.send(name, *args, &block)
else
@target.send(name, *args, &block).deliver
end
end
end
def self.run
MailerProxy.new(self)
end
# author: alan
# 发送邀请未注册用户加入项目邮件
# 功能: 在加入项目的同时自动注册用户

View File

@ -56,9 +56,7 @@ class Memo < ActiveRecord::Base
# }
def sendmail
thread1=Thread.new do
Mailer.forum_message_added(self).deliver if Setting.notified_events.include?('forum_message_added')
end
Mailer.run.forum_message_added(self) if Setting.notified_events.include?('forum_message_added')
end
def cannot_reply_to_locked_topic
@ -115,7 +113,7 @@ class Memo < ActiveRecord::Base
def send_notification
if Setting.notified_events.include?('message_posted')
Mailer.message_posted(self).deliver
Mailer.run.message_posted(self)
end
end

View File

@ -1,8 +1,6 @@
class MemoObserver < ActiveRecord::Observer
def after_create(memo)
thread1=Thread.new do
Mailer.forum_message_added(memo).deliver if Setting.notified_events.include?('forum_message_added')
end
Mailer.run.forum_message_added(memo) if Setting.notified_events.include?('forum_message_added')
end
end

View File

@ -17,9 +17,6 @@
class MessageObserver < ActiveRecord::Observer
def after_create(message)
##by senluo
thread5=Thread.new do
Mailer.message_posted(message).deliver if Setting.notified_events.include?('message_posted')
end
Mailer.run.message_posted(message) if Setting.notified_events.include?('message_posted')
end
end

View File

@ -17,9 +17,6 @@
class NewsObserver < ActiveRecord::Observer
def after_create(news)
##by senluo
thread6=Thread.new do
Mailer.news_added(news).deliver if Setting.notified_events.include?('news_added')
end
Mailer.run.news_added(news) if Setting.notified_events.include?('news_added')
end
end

View File

@ -159,7 +159,7 @@ class RelativeMemo < ActiveRecord::Base
def send_notification
if Setting.notified_events.include?('message_posted')
Mailer.message_posted(self).deliver
Mailer.run.message_posted(self)
end
end

View File

@ -17,18 +17,12 @@
class WikiContentObserver < ActiveRecord::Observer
def after_create(wiki_content)
##by senluo
thread7=Thread.new do
Mailer.wiki_content_added(wiki_content).deliver if Setting.notified_events.include?('wiki_content_added')
end
Mailer.run.wiki_content_added(wiki_content) if Setting.notified_events.include?('wiki_content_added')
end
def after_update(wiki_content)
if wiki_content.text_changed?
##by senluo
thread8=Thread.new do
Mailer.wiki_content_updated(wiki_content).deliver if Setting.notified_events.include?('wiki_content_updated')
end
Mailer.run.wiki_content_updated(wiki_content) if Setting.notified_events.include?('wiki_content_updated')
end
end
end

View File

@ -95,9 +95,7 @@ class UsersService
# create a new token for password recovery
token = Token.new(:user => user, :action => "recovery")
if token.save
Thread.new do
Mailer.lost_password(token).deliver
end
Mailer.run.lost_password(token)
return l(:notice_account_lost_email_sent,:locale => user.language)
end
end

View File

@ -60,6 +60,11 @@ per_page_options:
default: '25,50,100'
mail_from:
default: trustieforge@gmail.com
### delayjob for send email.
delayjob_enabled:
default: 1
bcc_recipients:
default: 1
plain_text_mail:

View File

@ -0,0 +1,22 @@
class CreateDelayedJobs < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, force: true do |table|
table.integer :priority, default: 0, null: false # Allows some jobs to jump to the front of the queue
table.integer :attempts, default: 0, null: false # Provides for retries, but still fail eventually.
table.text :handler, null: false # YAML-encoded string of the object that will do work
table.text :last_error # reason for last failure (See Note below)
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
table.datetime :locked_at # Set when a client is working on this object
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.string :queue # The name of the queue this job is in
table.timestamps null: true
end
add_index :delayed_jobs, [:priority, :run_at], name: "delayed_jobs_priority"
end
def self.down
drop_table :delayed_jobs
end
end

View File

@ -0,0 +1,5 @@
class AddDelayjobEnabledToSettings < ActiveRecord::Migration
def change
Setting.create(name: 'delayjob_enabled', value: 1 )
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20150328115230) do
ActiveRecord::Schema.define(:version => 20150331032810) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -422,6 +422,22 @@ ActiveRecord::Schema.define(:version => 20150328115230) do
add_index "custom_values", ["custom_field_id"], :name => "index_custom_values_on_custom_field_id"
add_index "custom_values", ["customized_type", "customized_id"], :name => "custom_values_customized"
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0, :null => false
t.integer "attempts", :default => 0, :null => false
t.text "handler", :null => false
t.text "last_error"
t.datetime "run_at"
t.datetime "locked_at"
t.datetime "failed_at"
t.string "locked_by"
t.string "queue"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
create_table "discuss_demos", :force => true do |t|
t.string "title"
t.text "body"

View File

@ -199,8 +199,7 @@ END_DESC
task :day => :environment do
users = User.where(mail_notification: 'day')
users.each do |user|
mailer = Mailer.send_for_user_activities(user, Date.today, 1)
mailer.deliver if mailer
Mailer.run.send_for_user_activities(user, Date.today, 1)
end
end
end

5
script/delayed_job Normal file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize