Moved message delivering into background process.
This commit is contained in:
parent
af3875d46b
commit
edadb7d8d1
3 changed files with 6 additions and 19 deletions
|
@ -12,15 +12,6 @@ class ApplicationController < ActionController::Base
|
||||||
Thread.current[:application_controller]
|
Thread.current[:application_controller]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Use this method to call a rake task,,
|
|
||||||
# e.g. to deliver mails after there are created.
|
|
||||||
def call_rake(task, options = {})
|
|
||||||
options[:rails_env] ||= Foodsoft.env
|
|
||||||
args = options.map { |n, v| "#{n.to_s.upcase}='#{v}'" }
|
|
||||||
system "/usr/bin/rake #{task} #{args.join(' ')} --trace 2>&1 >> #{Rails.root}/log/rake.log &"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
|
|
|
@ -14,7 +14,7 @@ class MessagesController < ApplicationController
|
||||||
def create
|
def create
|
||||||
@message = @current_user.send_messages.new(params[:message])
|
@message = @current_user.send_messages.new(params[:message])
|
||||||
if @message.save
|
if @message.save
|
||||||
@message.deliver #TODO: Put this into a background job
|
Message.delay.deliver(@message.id)
|
||||||
redirect_to messages_url, :notice => "Nachricht ist gespeichert und wird versendet."
|
redirect_to messages_url, :notice => "Nachricht ist gespeichert und wird versendet."
|
||||||
else
|
else
|
||||||
render :action => 'new'
|
render :action => 'new'
|
||||||
|
|
|
@ -21,6 +21,10 @@ class Message < ActiveRecord::Base
|
||||||
|
|
||||||
before_validation :clean_up_recipient_ids, :on => :create
|
before_validation :clean_up_recipient_ids, :on => :create
|
||||||
|
|
||||||
|
def self.deliver(message_id)
|
||||||
|
find(message_id).deliver
|
||||||
|
end
|
||||||
|
|
||||||
def clean_up_recipient_ids
|
def clean_up_recipient_ids
|
||||||
self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil?
|
self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil?
|
||||||
self.recipients_ids = User.all.collect(&:id) if sent_to_all == "1"
|
self.recipients_ids = User.all.collect(&:id) if sent_to_all == "1"
|
||||||
|
@ -73,20 +77,12 @@ class Message < ActiveRecord::Base
|
||||||
begin
|
begin
|
||||||
Mailer.foodsoft_message(self, user).deliver
|
Mailer.foodsoft_message(self, user).deliver
|
||||||
rescue
|
rescue
|
||||||
logger.warn "Deliver failed for #{user.nick}: #{user.email}"
|
Rails.logger.warn "Deliver failed for #{user.nick}: #{user.email}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
update_attribute(:email_state, 1)
|
update_attribute(:email_state, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sends all pending messages that are to be send as emails.
|
|
||||||
def self.send_emails
|
|
||||||
messages = Message.pending
|
|
||||||
for message in messages
|
|
||||||
message.deliver
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue