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

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' gem 'zip-zip'
end end
gem 'delayed_job_active_record'#, :group => :production
gem 'daemons'
gem 'grape', '~> 0.9.0' gem 'grape', '~> 0.9.0'
gem 'grape-entity' gem 'grape-entity'
gem 'seems_rateable', '~> 1.0.13' gem 'seems_rateable', '~> 1.0.13'

View File

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

View File

@ -110,7 +110,7 @@ class DocumentsController < ApplicationController
render_attachment_warning_if_needed(@document) render_attachment_warning_if_needed(@document)
if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added') 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 end
redirect_to document_url(@document) redirect_to document_url(@document)
end end

View File

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

View File

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

View File

@ -553,7 +553,7 @@ class UsersController < ApplicationController
@user.pref.save @user.pref.save
@user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) @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| respond_to do |format|
format.html { format.html {
@ -620,9 +620,9 @@ class UsersController < ApplicationController
@user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
if was_activated 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? 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 end
respond_to do |format| respond_to do |format|

View File

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

View File

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

View File

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

View File

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

View File

@ -18,13 +18,11 @@
class IssueObserver < ActiveRecord::Observer class IssueObserver < ActiveRecord::Observer
def after_create(issue) def after_create(issue)
Thread.start do
# 将跟踪者与本项目的其他成员都设为收件方,并去重,不在进行抄送, # 将跟踪者与本项目的其他成员都设为收件方,并去重,不在进行抄送,
recipients = issue.recipients - issue.watcher_recipients + issue.watcher_recipients recipients = issue.recipients - issue.watcher_recipients + issue.watcher_recipients
recipients.each do |rec| 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 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_status_updated') && journal.new_status.present?) ||
(Setting.notified_events.include?('issue_priority_updated') && journal.new_value_for('priority_id').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 = journal.recipients - journal.watcher_recipients + journal.watcher_recipients
recipients.each do |rec| recipients.each do |rec|
Mailer.issue_edit(journal,rec).deliver Mailer.run.issue_edit(journal,rec)
end end
end
end end
end end
end end

View File

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

View File

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

View File

@ -27,6 +27,24 @@ class Mailer < ActionMailer::Base
{ :host => Setting.host_name, :protocol => Setting.protocol } { :host => Setting.host_name, :protocol => Setting.protocol }
end 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 # author: alan
# 发送邀请未注册用户加入项目邮件 # 发送邀请未注册用户加入项目邮件
# 功能: 在加入项目的同时自动注册用户 # 功能: 在加入项目的同时自动注册用户

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -60,6 +60,11 @@ per_page_options:
default: '25,50,100' default: '25,50,100'
mail_from: mail_from:
default: trustieforge@gmail.com default: trustieforge@gmail.com
### delayjob for send email.
delayjob_enabled:
default: 1
bcc_recipients: bcc_recipients:
default: 1 default: 1
plain_text_mail: 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. # 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| create_table "activities", :force => true do |t|
t.integer "act_id", :null => false 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", ["custom_field_id"], :name => "index_custom_values_on_custom_field_id"
add_index "custom_values", ["customized_type", "customized_id"], :name => "custom_values_customized" 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| create_table "discuss_demos", :force => true do |t|
t.string "title" t.string "title"
t.text "body" t.text "body"

View File

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